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