TypeKeys()
或SendKeysCtypes()
(當前版本的pywinauto
使用此模塊發送擊鍵)模塊不能使用。這是因爲他們使用的SendInput
勝利的API,它剛剛與活動的應用程序的工作原理(如果它最小化應用程序不會被激活
認爲以下可能是有用的: 修改編輯文本:
。
app.Dialog.Edit.SetEditText("Your Text")
點擊一個按鈕或複選框等:
app.Dialog.ButtonEtc.Click()
這些可以在後臺應用程序是工作,我試圖隱藏窗口 - 不過上面並沒有(因爲默認情況下,大多數工作:(pywi的nauto正試圖確保窗口可見+啓用。
爲了得到一個隱藏的窗口,你可以使用:
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(...)
將檢查窗口是否可見:()
這裏有一些黑客一起代碼,將或多或少的工作來設置編輯的值EXT(注意SetEditText &選擇都是從pywinauto複製 - 並略微改變,因此:
他們不檢查控件可見
他們是獨立的功能
代碼:
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如何可以改善的一些(通過提供一個較低級別的庫,這將使它更容易做這樣的東西)
謝謝馬克您的快速回復。 – Scott 2011-06-17 13:05:32