如何使用pywinauto使窗口(已在运行的任务)可见?

时间:2018-05-14 14:29:54

标签: python pywinauto

除了在本网站上寻找答案外,我还检查了

pywinauto.application module

Getting Started Guide

但我仍然难倒。

我手动启动记事本并希望以下代码的第一个while块使记事本窗口可见。第二个while块有效,但我对行

感到困惑
dlg_spec = app.UntitledNotepad

这里发生了什么?这是什么样的python方法?

问题:我如何获得第一个代码块使标题为

的窗口
Untitled - Notepad

可见?

#--------*---------*---------*---------*---------*---------*---------*---------*
# Desc: Set focus on a window
# #--------*---------*---------*---------*---------*---------*---------*---------*

import sys
import pywinauto


#                                  # Manually started Notepad
#                                  # Want to make it visible (windows focus)
#                                  # Program runs, but...
while 1:
    handle = pywinauto.findwindows.find_windows(title='Untitled - Notepad')[0] 
    app = pywinauto.application.Application()
    ac = app.connect(handle=handle)
    print(ac)
    topWin = ac.top_window_()
    print(topWin)
    sys.exit()

#                                  # Working Sample Code
while 0:
    app = pywinauto.Application().start('notepad.exe')
    # describe the window inside Notepad.exe process
#                                  # ?1: '.UntitledNotepad' - huh?
    dlg_spec = app.UntitledNotepad
    # wait till the window is really open
    actionable_dlg = dlg_spec.wait('visible')
    sys.exit()

为方便起见,这段代码可以解决问题:

#                                  # Manually started Notepad
#                                  # Want to make it visible (windows focus).
#                                  # 
#                                  # Two or three lines solution provided by
#                                  # Vasily Ryabov's overflow answer
#                                  # (wrapper ribbon and bow stuff).
while 1:
    app = pywinauto.application.Application().connect(title="Untitled - Notepad")
    dlg_spec = app.window(best_match="UntitledNotepad")
    dlg_spec.set_focus()
    sys.exit()

2 个答案:

答案 0 :(得分:1)

我建议您使用private void circularImageBar(ImageView iv2, int progess, int radiusDifference) { System.out.println("sowload percent-" + progess); final int width = iv2.getWidth(); final int height = iv2.getHeight(); final Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(b); final int strokeWidth = width / 10; final Paint paint = new Paint(); paint.setColor(Color.DKGRAY); paint.setStrokeWidth(strokeWidth); paint.setStyle(Paint.Style.STROKE); paint.setAntiAlias(true); final int min = Math.min(width, height); final float radius = (min / 2f) - radiusDifference - (strokeWidth / 2f); final float cx = width / 2f; final float cy = height / 2f; canvas.drawCircle(cx, cy, radius, paint); final int tintColor = Color.parseColor("#F81004"); paint.setColor(tintColor); final RectF oval = new RectF(cx - radius, cy - radius, cx + radius, cy + radius); canvas.drawArc(oval, 270, ((progess * 360) / 100f), false, paint); iv2.setImageBitmap(b); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { iv2.setForegroundGravity(View.TEXT_ALIGNMENT_CENTER); } iv2.requestLayout(); } 库完成此任务,如下所示:

win32gui

here

所示,import win32gui hwnd = win32gui.FindWindow(None, 'Notepad') win32gui.SetForegroundWindow(hwnd) win32gui.ShowWindow(hwnd, 9) 代表9

答案 1 :(得分:1)

嗯,应该使用除find_windows之外的相同方法重写第一个while循环(它是低级别,不建议直接使用)。您需要方法.set_focus()才能将窗口置于前台。

app = pywinauto.Application().connect(title="Untitled - Notepad")
app.UntitledNotepad.set_focus()

创建窗口规范dlg_spec = app.UntitledNotepad表示调用了app方法__getattribute__。最后这一行相当于dlg_spec = app.window(best_match="UntitledNotepad")。要查找实际的包装器,您需要拨打.wait(...).wrapper_object()

但是当你调用一个动作(比如.set_focus())时,Python可以隐式地为你进行wrapper_object()调用(动态访问属性set_focus)。