首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >是否有可能将数据发送到最小化的窗口使用pywinauto?

是否有可能将数据发送到最小化的窗口使用pywinauto?
EN

Stack Overflow用户
提问于 2011-06-16 17:28:30
回答 1查看 4.6K关注 0票数 3

几个月前,我编写了一个Python应用程序,它读取一个包含车辆数据的文件。然后,它使用sendkey模块在一个温室气体模型应用程序中输入数据。我已经将代码转换为使用pywinauto,并使用pywinauto中的应用模块启动了温室气体应用程序。

我希望有温室气体申请窗口的背景或最小化,当数据被发送到它。我担心用户会试图关闭窗户,或者想知道如果显示了温室气体窗,会发生什么事情。

我在python win32gui模块中使用以下函数来最小化温室气体应用程序窗口:

hndl是我使用win32gui.EnumWindows()找到的温室气体窗口的句柄。

win32gui.ShowWindow(hndl,win32con.SW_MINIMIZE)

设定焦点

win32gui.SetForegroundWindow(hndl)

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

有办法绕过这件事吗?谢谢你的帮助。

EN

回答 1

Stack Overflow用户

发布于 2011-06-17 08:17:08

不能使用TypeKeys()SendKeysCtypes() (当前版本的pywinauto使用此模块发送键笔画)模块。这是因为他们使用的是SendInput win API,它只适用于活动应用程序(如果最小化应用程序,应用程序就不会是活动的)。

尽管如此,以下内容可能是有用的:更改编辑文本:

代码语言:javascript
运行
复制
app.Dialog.Edit.SetEditText("Your Text")

单击按钮或复选框等:

代码语言:javascript
运行
复制
app.Dialog.ButtonEtc.Click()

它们可以与处于后台的应用程序一起工作。我试图隐藏这个窗口--但是上面的这些都不起作用:(因为默认情况下,大多数pywinauto都试图确保窗口是可见的+启用的。

要获得隐藏窗口,可以使用:

代码语言:javascript
运行
复制
hidden_win = app.window_(title = "Untitled - Notepad", visible_only = False)

去找个孩子的窗户--就像:

代码语言:javascript
运行
复制
edit_control = hidden_win.ChildWindow(visible_only = False, class_name = "Edit")

但不幸的是,在使用pywinauto时,您遇到了一些问题(因为edit_control.SetEditText(...)将检查窗口是否可见:( )

下面是一些经过黑客攻击的代码,这些代码或多或少地会用来设置编辑文本的值(注意,SetEditText和Select都是从pywinauto复制的,并且略有更改,以便:

  1. 它们不检查控件是否是可见的
  2. 它们是独立函数

代码:

代码语言:javascript
运行
复制
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 (通过提供一个更低级别的库,这样做更容易)。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6376047

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档