Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python UI freezing

I'm trying to make basic functionality after pressing "start" button start counter , after pressing stop button stop counter, but after I start process, it looks like only counting thread is working and it's not possible to press stop button

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
from PyQt4 import QtGui, QtCore
from test.test_sax import start
import time
from threading import Thread
import threading
class Example(QtGui.QWidget):
    x = 1
    bol = True
    def __init__(self):
        super(Example, self).__init__()


        self.qbtn = QtGui.QPushButton('Quit', self)

        self.qbtn.resize(self.qbtn.sizeHint())
        self.qbtn.move(50, 50)
        self.qbtn2 = QtGui.QPushButton('Start', self)

        self.qbtn2.resize(self.qbtn2.sizeHint())
        self.qbtn2.move(150, 50)

        self.qbtn.clicked.connect(self.stopCounter)
        self.qbtn2.clicked.connect(self.startUI)

        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('Quit button')
        self.show()
    def stopCounter(self):
        Example.bol = False

    def startUI(self):
        Example.bol = True
        thread = Thread(self.counterr())

    def counterr(self):
        x = 0
        while Example.bol:
            print x
            x += 1



if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    a = Example()
    sys.exit(app.exec_())

thx

like image 270
sala Avatar asked Mar 16 '26 01:03

sala


1 Answers

Now you call the slow function before you even create the thread. Try this instead:

thread = Thread(target=self.counterr)
thread.start()

In a Qt application you might also consider the QThread class that can run its own event loop and communicate with your main thread using signals and slots.

like image 81
Janne Karila Avatar answered Mar 18 '26 13:03

Janne Karila



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!