Thursday, 15 September 2011

python - Changing Pixmap image automatically at runtime in PyQt -



python - Changing Pixmap image automatically at runtime in PyQt -

i wish alter image of pixmap beingness displayed in window automatically using while loop has defined delay , uses locations of images stored in string list. possibilities tried led first image beingness displayed not changing @ runtime. changes gets reflected after programme has completed kinds of execution.

from pyqt4 import qtcore, qtgui import time import sys try: _fromutf8 = qtcore.qstring.fromutf8 except attributeerror: def _fromutf8(s): homecoming s try: _encoding = qtgui.qapplication.unicodeutf8 def _translate(context, text, disambig): homecoming qtgui.qapplication.translate(context, text, disambig, _encoding) except attributeerror: def _translate(context, text, disambig): homecoming qtgui.qapplication.translate(context, text, disambig) class ui_form(qtgui.qwidget): def __init__(self): qtgui.qwidget.__init__(self) self.setupui(self) def setupui(self, form): form.setobjectname(_fromutf8("form")) form.resize(598, 555) self.horizontallayout = qtgui.qhboxlayout(form) self.horizontallayout.setobjectname(_fromutf8("horizontallayout")) self.start_pulse = qtgui.qpushbutton(form) self.start_pulse.setobjectname(_fromutf8("start_pulse")) self.horizontallayout.addwidget(self.start_pulse) self.label = qtgui.qlabel(form) self.label.setautofillbackground(true) self.label.settext(_fromutf8("")) self.label.setpixmap(qtgui.qpixmap(_fromutf8("gui.png"))) self.label.setscaledcontents(true) self.label.setobjectname(_fromutf8("label")) self.horizontallayout.addwidget(self.label) self.retranslateui(form) qtcore.qmetaobject.connectslotsbyname(form) def retranslateui(self, form): form.setwindowtitle(_translate("form", "form", none)) self.start_pulse.settext(_translate("form", "start", none)) self.start_pulse.clicked.connect(self.soumyajit) def soumyajit(self): j=0 while(1): print("change") if j==0: self.label.setpixmap(qtgui.qpixmap(_fromutf8("gui2.png"))) if j==1: self.label.setpixmap(qtgui.qpixmap(_fromutf8("gui.png"))) if j==2: self.label.setpixmap(qtgui.qpixmap(_fromutf8("gui2.png"))) if j==3: self.label.setpixmap(qtgui.qpixmap(_fromutf8("gui.png"))) j=j+1 #delay time.sleep(.5) if __name__=='__main__': app=qtgui.qapplication(sys.argv) ex = ui_form() ex.show() sys.exit(app.exec_())

you run loop in gui thread it's freeze app. bad approach. should utilize qtimer instead. utilize timeout() signal image changes within special slot. qtimer asynchronous not freeze app.

you can phone call processevents() within loop not solution, timer improve loop sleep.

python pyqt qt-designer

No comments:

Post a Comment