使用来自用户输入的一些字符串传递函数中的参数

时间:2016-11-20 05:14:14

标签: python connect pywinauto

我想使用Application().Connect()中的pywinauto连接到正在运行的窗口。

该行:

app = Application().Connect(title=u'Untitled - Notepad', class_name='Notepad')

下面的代码中的

完全按照我想要的方式工作。但是,我想让用户输入标题名称和类名。我要求用户首先键入标题名称和类名,然后尝试修改app = Application().Connect()参数中传递的信息,但无法正确。我已经尝试通过连接传递标题名称和类名,但是也不起作用。有人可以帮帮我吗?

from pywinauto
import Application
import win32gui
import win32con
import win32api
from time
import sleep
import win32api, win32con, win32gui, win32ui, win32service, os, time

print "Title Name "
titlename = raw_input()
print "Class Name "
classname = raw_input()
titlename = "title=u'" + titlename + "'"
classname = "class_name='" + classname + "'"
print titlename
print classname
app = Application().Connect(titlename, classname)# app = Application().Connect(title = u 'Untitled - Notepad', class_name = 'Notepad')
dlg = app['Untitled - Notepad']# Untitled - Notepad
dlg = app.top_window()

def f_click(pycwnd):
    x = 300
y = 300
lParam = y << 15 | x
pycwnd.SendMessage(win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, lParam);
pycwnd.SendMessage(win32con.WM_LBUTTONUP, 0, lParam);

def get_whndl():
    whndl = win32gui.FindWindowEx(0, 0, None, 'Untitled - Notepad')# Untitled - Notepad
return whndl

def make_pycwnd(hwnd):
    PyCWnd = win32ui.CreateWindowFromHandle(hwnd)
return PyCWnd

def send_input_hax(pycwnd, msg):
    f_click(pycwnd)
for c in msg:
    if c == "\n":
    pycwnd.SendMessage(win32con.WM_KEYDOWN, win32con.VK_RETURN, 0)
pycwnd.SendMessage(win32con.WM_KEYUP, win32con.VK_RETURN, 0)
else :
    pycwnd.SendMessage(win32con.WM_CHAR, ord(c), 0)
pycwnd.UpdateWindow()

whndl = get_whndl()

def callback(hwnd, hwnds):
    if win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd):
    hwnds[win32gui.GetClassName(hwnd)] = hwnd
return True
hwnds = {}
win32gui.EnumChildWindows(whndl, callback, hwnds)
print hwnds
whndl = hwnds['Edit']
pycwnd = make_pycwnd(whndl)
msg = "It works !\n"
send_input_hax(pycwnd, msg)

0 个答案:

没有答案