-->

pywinauto qwidget click triggers no action

2019-06-13 15:55发布

问题:

It is visibly that a QWidget button is pressed and released, unfortunately no action is triggered.

The environment is similar to find qwidget object text by using pywinauto. The code has resorted to use of absolute coordinates from How to click a 'next' button of a window using python.

Once the program runs, the target window will get focus. The cursor starts to be somewhere close to the center of the target window. Then it moves to the button, presses the button since the color changes to the down-color. Then it releases the button, and the cursor moves back to the center of window. Only one thing missing, no action is taken from the target window application.

There is one fix: If I move the mouse to the title of the window and click at the right time, the automatic click on the button will trigger a real action. This makes me puzzled as to why this is happening, and unable to think about a solution for it.

app1 = Application()
app1.connect(title_re='^Quartus Prime Programmer.*$')
app1.QWidget.print_control_identifiers()

win1 = app1.QWidget
rect1 = win1.rectangle()
coor1 = (rect1.left+64,rect1.top+350) # 64, 350: "add file"
win11 = win1.TopLevelParent()
for i in range(3):
    win11.SetFocus()
    win11.PressMouseInput(coords=coor1)
    time.sleep(1.3)
    win11.ReleaseMouseInput()
    time.sleep(3.0)

The code repeats it three time so a human intervention can be easily inserted into the process.

回答1:

Qt apps can be automated using Application(backend='uia'). Please read the Getting Started Guide written specially for pywinauto 0.6.0+ with the new backend. It explains the core concept and many useful tricks.

P.S. I've downloaded this Lite edition app and will try to make an example for you later.



回答2:

I still prefer a solution in python or pywinauto. Here is a temporary workaround using a java robot, just to get it going. The python will call it through a command line java -jar TestQuartusClick.jar 64 350 1, where 64 350 1 are the coordinates and call reference number.

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.util.logging.Level;
import java.util.logging.Logger;

public class TestQuartusClick {
    public static void main(String[] args){
        int x = 0, y = 0, fn = 0;
        if ( args.length == 3 ) {
            x = Integer.parseInt(args[0]);
            y = Integer.parseInt(args[1]);
            fn = Integer.parseInt(args[2]); /* reference */
        } else {
            System.out.println("java error args size not 3");
            return;
        }
        System.out.println(String.format(
                "java input %d %d %d", x, y, fn));
        Robot bot;
        try {
            bot = new Robot();
            int mask = InputEvent.BUTTON1_DOWN_MASK;
            bot.mouseMove(x, y);
            bot.mousePress(mask);
            bot.mouseRelease(mask);
            System.out.println(String.format(
                    "java clicked %d %d %d", x, y, fn));
        } catch (AWTException ex) {
            System.out.println(String.format(
                    "java error click %d %d %d", x, y, fn));
            //Logger.getLogger(TestQuartusClick.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}