在 Qt GUI 中按下按钮时显示图像 (Python)
Display an image when pressing a button in Qt GUI (Python)
我在 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
我尝试阅读堆栈溢出中的所有类似问题。看来我的问题重复了,但是 none 的答案似乎解决了我的问题。
编辑:使用它也适用于图像在同一文件夹中的情况,例如
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())
我在 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
我尝试阅读堆栈溢出中的所有类似问题。看来我的问题重复了,但是 none 的答案似乎解决了我的问题。
编辑:使用
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())