D
D
Danila Rumyantsev2021-08-20 16:59:40
Python
Danila Rumyantsev, 2021-08-20 16:59:40

I wrote a simple anti-afk based on pydirectinput and pyqt5, when the start button is pressed, the application crashes, what should I do?

used import pydirectinput as pyd

def add_functionts(self):
        self.btn_start.clicked.connect(self.start)
        self.btn_end.clicked.connect(self.end)
        self.gag= True
        self.start_thead=threading.Thread(target=self.start)
        self.start_thead.start()


    def start(self):
        print('start')
        while self.gag==True:
            pyd.press('w')




    def end(self):
        print('The end of the programm')
        self.gag =False
        return self.gag

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vindicar, 2021-08-21
@Bubunduc

The application simply does not respond after pressing the 'Start' button (start function)

Well, that is logical. Read how window applications are arranged, the same basics work with any GUI.
In short, they are based on a loop that receives messages from the OS (key pressed, mouse moved, etc.) and processes them.
PyQT implements this loop itself, you are only dealing with event handlers - but the loop does not disappear from this. Everything happens in one thread, so while the event handler is running, the loop is standing, the next messages are waiting in the queue.
So when you do while self.gag==True: , your handler will never exit the while loop, never exit the window loop, and the click on the end button will never be handled (kk or any other event).
What to do?
a) start starts the second thread, end stops it. Smoke the documentation for the threading module.
b) see if there is a way in PyQt to schedule a function call on the next iteration of the window loop / some time later. In this case, you can schedule a call to your function over and over again, which implements one iteration (for example, sends one button click). Since it will be called by means of the window cycle, and it will work out quickly, this will not interfere with the window cycle.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question