Pyqt Qmessagebox died in the while loop

72 Views Asked by At

I want to use Hotkey to move the mouse into some position, It can be set by the user personally, these hotkeys are F1-F4, when pressed F11 to enter these hotkeys detect, It will check the position_list whether empty, If it's empty then run loop four times to the user setting position, but when the Qmessagebox executes in the loop, my Window has died, I don't understand what wrong.

This is my code:

from PySide6.QtWidgets import *
from PySide6.QtGui import *
import pyautogui

import sys
import keyboard

class Window_controller(QWidget):
    Hotkey = True
    position_list = []
    def __init__(self):
        super().__init__()
        self.layout = QVBoxLayout(self)
        self.label = QLabel(self)
        self.label.setText("123")
        self.label.setFont(QFont("",20))
        self.layout.addWidget(self.label)
        self.hotkey_active()

    def hotkey(self):
        if self.Hotkey == True:
            self.label.setText("Activated")
            while True:
                if keyboard.is_pressed("F1"):
                    self.F1()
                elif keyboard.is_pressed("F2"):
                    self.F2()
                elif keyboard.is_pressed("F3"):
                    self.F3()
                elif keyboard.is_pressed("F4"):
                    self.F4()
                elif keyboard.is_pressed("F12"):
                    self.Hotkey == True
                    self.label.setText("")
                    break

    def F1(self):
        if len(self.position_list) != 0:
            pyautogui.moveTo(x=self.position_list[0],y=self.position_list[1])
        else:
            self.set_position()

    def F2(self):
        if len(self.position_list) != 0:
            pyautogui.moveTo(x=self.position_list[2],y=self.position_list[3])
        else:
            self.set_position()

    def F3(self):
        if len(self.position_list) != 0:
            pyautogui.moveTo(x=self.position_list[4],y=self.position_list[5])
        else:
            self.set_position()

    def F4(self):
        if len(self.position_list) != 0:
            pyautogui.moveTo(x=self.position_list[6],y=self.position_list[7])
        else:
            self.set_position()
    def set_position(self):
        for key in ["F1", "F2", "F3", "F4"]:
            QMessageBox.information(self, str(key), str("Move you'r mouse to setting position"))
            self.position_list.append(pyautogui.position().x)
            self.position_list.append(pyautogui.position().y)
        print(self.position_list)

    def hotkey_active(self):
        keyboard.add_hotkey("F11", self.hotkey)
if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = Window_controller()
    win.show()
    sys.exit(app.exec())

When program run into for-loop stuck

1

There are 1 best solutions below

1
Dylan On

I found the solution finally.

If you need to use Qmessagebox to pause the program in the while loop, you can use Tkinter's Messagebox instead.

Example:

def messagebox_info(self, title, content):
    root = Tk()
    root.wm_attributes("-topmost", 1)
    root.withdraw()
    messagebox.showinfo(title, content)
    root.destroy()