2011-06-16 541 views
1

几个月前,我编写了一个Python GUI应用程序,用于读取包含车辆数据的文件。然后它使用sendkeys模块在温室气体模型应用程序中输入数据。我已经将代码转换为使用pywinauto并使用pywinauto中的应用程序模块来启动温室气体应用程序。是否可以使用pywinauto将数据发送到最小化窗口?

我想在后台发送温室气体应用窗口,或者在数据发送到窗口时将其最小化。我担心,如果显示温室气体窗口,用户将尝试关闭窗户或想知道发生了什么。

我使用下列功能蟒蛇win32gui模块中,以尽量减少温室气体的应用程序窗口:

HNDL是句柄温室气体的窗口,我使用win32gui.EnumWindows发现()

win32gui .ShowWindow(HNDL,win32con.SW_MINIMIZE)

设置焦点

win32gui.SetForegroundWindow(HNDL)

当我添加上面的代码时,温室气体应用程序窗口被最小化。不幸的是,pywinauto中的SendKeys模块不会将信息发送到最小化窗口。

有没有办法解决这个问题?感谢您的帮助。

回答

3

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复制 - 并略微改变,因此:

  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如何可以改善的一些(通过提供一个较低级别的库,这将使它更容易做这样的东西)

+0

谢谢马克您的快速回复。 – Scott 2011-06-17 13:05:32

相关问题