2011-06-16 542 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

相關問題