【发布时间】:2018-01-06 22:01:08
【问题描述】:
我在 Qt Creator 中绘制了一个 GUI,带有一个按钮、一个滑块和一些标签。
我正在尝试什么:按下按钮时,在终端上打印滑块的修改值并在标签中显示图像。正如许多网页所建议的那样,我正在尝试使用 pixmap 方法将图像显示到标签中。这是我的全部代码(GUI的结构在导入的mainwindow.ui文件中)
import sys
from PyQt4 import QtCore, QtGui, uic
qtCreatorFile = "mainwindow.ui"
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
class myownGUI(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
#button
self.Do_button.clicked.connect(self.action)
#slider
self.SLIDER.valueChanged[int].connect(self.SLIDER_update)
#"global" variable init. by callback
self.SLIDER_update()
#The button callback
def action(self):
print "DOING ACTION!"
print self.Slider
#trying to display the image in the Image_label
image = QtGui.QImage(QtGui.QImageReader(":/images/test.png").read())
self.Image_label.setPixmap(QtGui.QPixmap(image))
#self.Image_label.show() #unuseful command?
#Slider update callback
def SLIDER_update(self):
self.Slider= self.SLIDER.value()
if (self.Slider % 2 == 0): #even
self.Slider = self.Slider + 1
self.Slider_label.setText(str(self.Slider))
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = myownGUI()
window.show()
sys.exit(app.exec_())
代码运行,它没有显示错误,但没有显示图像。 我尝试了JPG和PNG图像。当图像位于同一文件夹中时,我也尝试了简单的图像名称。
我的代码有什么问题? 还有另一种方法可以在 GUI 内的 QT 中显示图像(使用 python)?
提前谢谢你
使用:Ubuntu 14.04 / Qt 版本 4.8.6
我尝试阅读堆栈溢出中的所有类似问题。看来我的问题是重复的,但似乎没有一个答案能解决我的问题。
编辑:使用PRMoureu's syntax 也适用于图像位于同一文件夹时,例如
image = QtGui.QImage(QtGui.QImageReader("./test.png").read())
现在图像已显示,只需重新缩放。
【问题讨论】:
标签: python qt user-interface pyqt pyqt4