python - Statusbar doesn't show message immediately and sometimes crash -


i created interface in pyqt4 designer, there button , status bar. want show message in status bar when click button. reason have put action in new thread.

but when click button, nothing happened. message didn't appear until drag edge of window. click action cause program crash:

pythonw.exe has stopped working. 

this happens frequently, when click fast.

here code:

from pyqt4 import qtcore, qtgui import threading  try:     _fromutf8 = qtcore.qstring.fromutf8 except attributeerror:     def _fromutf8(s):         return s  try:     _encoding = qtgui.qapplication.unicodeutf8     def _translate(context, text, disambig):         return qtgui.qapplication.translate(context, text, disambig, _encoding) except attributeerror:     def _translate(context, text, disambig):         return qtgui.qapplication.translate(context, text, disambig)  class ui_mainwindow(object):      def cal(self):         self.statusbar.showmessage('something')         def onbutton(self):          thread = threading.thread(target=self.cal)         thread.start()        def setupui(self, mainwindow):         mainwindow.setobjectname(_fromutf8("mainwindow"))         mainwindow.resize(510, 409)         self.centralwidget = qtgui.qwidget(mainwindow)         self.centralwidget.setobjectname(_fromutf8("centralwidget"))         self.pushbutton = qtgui.qpushbutton(self.centralwidget)         self.pushbutton.setgeometry(qtcore.qrect(170, 100, 171, 91))         self.pushbutton.setobjectname(_fromutf8("pushbutton"))         self.pushbutton.clicked.connect(self.onbutton)          mainwindow.setcentralwidget(self.centralwidget)         self.statusbar = qtgui.qstatusbar(mainwindow)         self.statusbar.setobjectname(_fromutf8("statusbar"))         mainwindow.setstatusbar(self.statusbar)          self.retranslateui(mainwindow)         qtcore.qmetaobject.connectslotsbyname(mainwindow)      def retranslateui(self, mainwindow):         mainwindow.setwindowtitle(_translate("mainwindow", "mainwindow", none))         self.pushbutton.settext(_translate("mainwindow", "pushbutton", none))   if __name__ == "__main__":     import sys     app = qtgui.qapplication(sys.argv)     mainwindow = qtgui.qmainwindow()     ui = ui_mainwindow()     ui.setupui(mainwindow)     mainwindow.show()     sys.exit(app.exec_()) 

the crash happens because interacting qt gui secondary thread. allowed interact gui main thread (see qt documentation).

you have 2 options:

  1. switch pyqt qthread, , emit signal thread. if signal connected slot in main thread, can gui interaction there. question/answer demonstrates approximately how this.

  2. use qapplication.postevent() send event python thread main thread, causes method run interacts gui. note people have concerns not ok (see this answer), i've had no issue it. i've wrapped in freely available library called qtutils (documentation here).


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -