Tuesday, 15 February 2011

python - Problems with the order execution functions when use PyQT4 -



python - Problems with the order execution functions when use PyQT4 -

i have problem pyqt4 python. there label text , button connected function. function alter text of label first, phone call other function. there problem it: function executed firts, alter text of label.

code:

# -*- coding: utf-8 -*- import time import sys pyqt4 import qtcore, qtgui def timesleep(): print("start sleep") time.sleep(5) print("stop sleep") class anywidget(qtgui.qwidget): def __init__(self,*args): qtgui.qwidget.__init__(self,*args) self.setwindowtitle("pethard") boxlay = qtgui.qhboxlayout(self) frame = qtgui.qframe(self) # Фрейм frame.setframeshape(qtgui.qframe.styledpanel) frame.setframeshadow(qtgui.qframe.raised) gridlay = qtgui.qgridlayout(frame) # Менеджер размещения элементов во фрейме label = qtgui.qlabel(u"welcome",frame) # Текстовая метка. global glabel glabel = label gridlay.addwidget(label,0,0) button1 = qtgui.qpushbutton(u"load mc", frame) self.connect(button1, qtcore.signal("clicked()"), self.ts) gridlay.addwidget(button1,1,0) boxlay.addwidget(frame) def ts(self): global glabel glabel.settext(u"waiting...") timesleep() if __name__=="__main__": app = qtgui.qapplication(sys.argv) aw = anywidget() aw.show() sys.exit(app.exec_())

help me please prepare problem.

you never want tie-up gui long-running function. label not update 1 manifestation of problem. if label update before function called, still "freeze" gui -- no widget respond user until long-running function completes.

if have run such function, see if there way break little pieces each relinquish command gui, or consider using separate thread run function:

import time import sys pyqt4 import qtcore, qtgui class timesleep(qtcore.qthread): def __init__(self, parent=none): qtcore.qthread.__init__(self, parent) self.parent = parent def run(self): print("start sleep") time.sleep(5) print("stop sleep") self.parent.glabel.settext(u"done") class anywidget(qtgui.qwidget): def __init__(self, *args): qtgui.qwidget.__init__(self, *args) self.setwindowtitle("pethard") boxlay = qtgui.qhboxlayout(self) frame = qtgui.qframe(self) # Фрейм frame.setframeshape(qtgui.qframe.styledpanel) frame.setframeshadow(qtgui.qframe.raised) gridlay = qtgui.qgridlayout( frame) # Менеджер размещения элементов во фрейме label = qtgui.qlabel(u"welcome", frame) # Текстовая метка. self.glabel = label gridlay.addwidget(label, 0, 0) button1 = qtgui.qpushbutton(u"load mc", frame) self.connect(button1, qtcore.signal("clicked()"), self.ts) gridlay.addwidget(button1, 1, 0) boxlay.addwidget(frame) def ts(self): self.glabel.settext(u"waiting...") self.thread = timesleep(parent=self) self.thread.start() if __name__ == "__main__": app = qtgui.qapplication(sys.argv) aw = anywidget() aw.show() sys.exit(app.exec_())

here wiki page on how deal long-running functions.

python pyqt4

No comments:

Post a Comment