3

数か月前、私は車両データを含むファイルを読み取る Python GUI アプリを作成しました。次に、sendkeys モジュールを使用して、温室効果ガス モデル アプリにデータを入力します。pywinauto を使用するようにコードを変換し、pywinauto のアプリケーション モジュールを使用して温室効果ガス アプリを起動しました。

温室効果ガス アプリケーション ウィンドウをバックグラウンドで表示するか、データが送信されているときに最小化したいと考えています。温室効果ガスのウィンドウが表示されていると、ユーザーがウィンドウを閉じようとしたり、何が起こっているのか疑問に思ったりするのではないかと心配しています。

python win32gui モジュールで次の関数を使用して、温室効果ガス アプリ ウィンドウを最小化します。

hndl は、win32gui.EnumWindows() を使用して見つけた温室効果ガス ウィンドウのハンドルです。

win32gui.ShowWindow(hndl,win32con.SW_MINIMIZE)

フォーカスを設定する

win32gui.SetForegroundWindow(hndl)

上記のコードを追加すると、温室効果ガス アプリ ウィンドウが最小化されます。残念ながら、pywinauto の SendKeys モジュールは、最小化されたウィンドウに情報を送信しません。

これを回避する方法はありますか?ご協力ありがとうございました。

4

1 に答える 1

4

TypeKeys()またはSendKeysCtypes()(現在のバージョンでpywinautoは、このモジュールを使用してキー ストロークを送信します) モジュールは使用できません。これはSendInput、アクティブなアプリケーションでのみ機能する win API を使用しているためです (アプリケーションが最小化されている場合、アプリケーションはアクティブになりません。

とはいえ、次のことが役に立つかもしれません: 編集テキストを変更します:

app.Dialog.Edit.SetEditText("Your Text")

ボタンまたはチェックボックスなどをクリックします。

app.Dialog.ButtonEtc.Click()

これらは、バックグラウンドにあるアプリケーションで機能します。ウィンドウを非表示にしようとしましたが、上記は機能しませんでした:((デフォルトでは、ほとんどのpywinautoはウィンドウが表示されていることを確認しようとしているためです+ Enabled.

非表示のウィンドウを取得するには、次を使用できます。

hidden_win = app.window_(title = "Untitled - Notepad", visible_only = False)

子ウィンドウを取得するには、次のようにします。

edit_control = hidden_win.ChildWindow(visible_only = False, class_name = "Edit")

しかし、残念なことに、pywinauto を使用するときに少し行き詰まりますedit_control.SetEditText(...)(ウィンドウが表示されていることを確認するため:()

これは、編集テキストの値を設定するために多かれ少なかれ機能するハッキングされたコードです ( SetEditText と Select は両方とも pywinauto からコピーされ、次のようにわずかに変更されていることに注意してください。

  1. コントロールが表示されていることを確認しません

  2. それらはスタンドアロン関数です

コード:

import win32gui
import win32con
import sys
import os
sys.path.append(os.path.abspath('.'))
print sys.path[-1]
from pywinauto import application
from pywinauto import win32defines
from pywinauto import win32functions
from pywinauto.timings import Timings
import time
import ctypes

#-----------------------------------------------------------
def Select(win, start = 0, end = None):
    "Set the edit selection of the edit control"

    # if we have been asked to select a string
    if isinstance(start, basestring):
        string_to_select = start
        #
        start = win.TextBlock().index(string_to_select)

        if end is None:
            end = start + len(string_to_select)

    if end is None:
        end = -1

    win.SendMessageTimeout(win32defines.EM_SETSEL, start, end)

    # give the control a chance to catch up before continuing
    win32functions.WaitGuiThreadIdle(win)

    time.sleep(Timings.after_editselect_wait)

    # return this control so that actions can be chained.
    return win

#-----------------------------------------------------------
def SetEditText(win, text, pos_start = None, pos_end = None):
    "Set the text of the edit control"
    # allow one or both of pos_start and pos_end to be None
    if pos_start is not None or pos_end is not None:

        # if only one has been specified - then set the other
        # to the current selection start or end
        start, end = win.SelectionIndices()
        if pos_start is None:
            pos_start = start
        if pos_end is None:
            pos_end = end

        # set the selection if either start or end has
        # been specified
        win.Select(pos_start, pos_end)
    else:
        Select(win)

    # replace the selection with
    text = ctypes.c_wchar_p(unicode(text))
    win.SendMessageTimeout(win32defines.EM_REPLACESEL, True, text)

    win32functions.WaitGuiThreadIdle(win)
    time.sleep(Timings.after_editsetedittext_wait)

    # return this control so that actions can be chained.
    return win

# this may be useful if you just want to send a keydown/keyup combination
def keystroke(hwnd, key):
    win32gui.PostMessage(hwnd, win32con.WM_KEYDOWN, key, 0)
    win32gui.PostMessage(hwnd, win32con.WM_KEYUP, key, 3 < 30)


app = application.Application.start('notepad')

# hide the window
win32gui.ShowWindow(app.window_(title = "Untitled - Notepad", visible_only = False).handle, win32defines.SW_HIDE)
print "See it is hidden :)"
time.sleep(2)

# get tehe edit control and set it's text
edit = app.window_(title = "Untitled - Notepad", visible_only = False).ChildWindow(visible_only = False, class_name = "Edit")
SetEditText(edit, "The edit text has been set")

# show the window again
win32gui.ShowWindow(app.window_(title = "Untitled - Notepad", visible_only = False).handle, win32defines.SW_SHOW)

これは、pywinauto がどのように改善されるかを示す良い例です (このようなことをより簡単にする低レベルのライブラリを提供することによって)

于 2011-06-17T08:17:08.493 に答える