0
我在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())
現在顯示的圖像,並只進行重新調整。
它的工作!非常感謝你。 – marcoresk