在Qt GUI(Python)中按下按钮时显示图像
问题描述:
我在Qt Creator中绘制了一个GUI,带有一个按钮,一个滑块和一些标签。在Qt GUI(Python)中按下按钮时显示图像
我在试:当按钮被按下时,在终端和标签上打印修改后的滑块值并显示图像。正如许多网页建议的,我试图通过使用像素图方法将图像显示到标签中。这是我的整个代码(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中显示图像(使用python)在QT中?
预先感谢您
与工作:Ubuntu的14.04/QT版本4.8.6
我尝试阅读栈溢出所有类似的问题。似乎我的问题是重复的,但没有一个答案似乎解决了我的问题。
编辑:使用PRMoureu's syntax它的工作原理也当图像是同一个文件夹,如
image = QtGui.QImage(QtGui.QImageReader("./test.png").read())
现在显示的图像,并只进行重新调整。
答
你应该调用图像与另一个路径语法:
image = QtGui.QImage(QtGui.QImageReader("./images/test.png").read())
或
image = QtGui.QImage(QtGui.QImageReader("images/test.png").read())
它的工作!非常感谢你。 – marcoresk