gpt4 book ai didi

python - 在 PyQt 运行时自动更改 Pixmap 图像

转载 作者:行者123 更新时间:2023-12-04 18:05:52 25 4
gpt4 key购买 nike

我希望使用 while 循环自动更改显示在窗口中的像素图图像,该循环具有定义的延迟并使用存储在字符串列表中的图像位置。 我尝试过的所有可能性都会导致显示第一张图像但在运行时不会更改。任何更改只有在程序完成各种执行后才会反射(reflect)出来。

from PyQt4 import QtCore, QtGui
import time
import sys

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_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_())

最佳答案

按照我从您的代码中修改的代码,它运行良好:

原始代码来自 Changing Pixmap image automatically at runtime in PyQt

由 WonJong Kim (wjkim@etri.re.kr) 修改以解决问题

import sys
import time
import numpy
from PyQt4 import QtCore, QtGui
import PyQt4.Qwt5 as Qwt

numPoints=1000
xs=numpy.arange(numPoints)
ys=numpy.sin(3.14159*xs*10/numPoints) #this is our data

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_Form(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.setupUi(self)
self.index = 0

def setupUi(self, Form):
Form.setObjectName(_fromUtf8("Form"))
Form.resize(500, 300)
self.horizontalLayout = QtGui.QHBoxLayout(Form)
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.start_pulse = QtGui.QPushButton(Form)
self.start_pulse.setObjectName(_fromUtf8("start_pulse"))
self.stop_pulse = QtGui.QPushButton(Form)
self.stop_pulse.setObjectName(_fromUtf8("stop_pulse"))
self.horizontalLayout.addWidget(self.start_pulse)
self.horizontalLayout.addWidget(self.stop_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 refreshUi(self):
if self.index%10==0:
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("Images/10.jpg")))
if self.index%10==1:
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("Images/1.jpg")))
if self.index%10==2:
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("Images/2.jpg")))
if self.index%10==3:
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("Images/3.jpg")))
if self.index%10==4:
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("Images/4.jpg")))
if self.index%10==5:
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("Images/5.jpg")))
if self.index%10==6:
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("Images/6.jpg")))
if self.index%10==7:
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("Images/7.jpg")))
if self.index%10==8:
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("Images/8.jpg")))
if self.index%10==9:
self.label.setPixmap(QtGui.QPixmap(_fromUtf8("Images/9.jpg")))

def retranslateUi(self, Form):
Form.setWindowTitle(_translate("Form", "Form", None))
self.start_pulse.setText(_translate("Form", "start", None))
self.start_pulse.clicked.connect(self.start)
self.stop_pulse.setText(_translate("Form", "stop", None))
self.stop_pulse.clicked.connect(self.stop)

def start(self):
self.timer1.start(500.0) #set the interval (in ms)

def stop(self):
self.timer1.stop() #stop the timer

def change_index(self):
self.index += 1


if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
win_plot = Ui_Form()

win_plot.index = 0
win_plot.timer = QtCore.QTimer() #start a timer (to call replot events)
win_plot.timer.start(100.0) #set the interval (in ms)
win_plot.connect(win_plot.timer, QtCore.SIGNAL('timeout()'), win_plot.refreshUi)

win_plot.timer1 = QtCore.QTimer() #start a timer (to call replot events)
win_plot.connect(win_plot.timer1, QtCore.SIGNAL('timeout()'),
win_plot.change_index)

# show the main window
win_plot.show()
sys.exit(app.exec_())

关于python - 在 PyQt 运行时自动更改 Pixmap 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26851870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com