我试图在PyQt5上编写一个应用程序,通过QFileDialog打开一个图像并将其显示在主窗口上。代码如下:import sys
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QIcon
from PyQt5.QtWidgets import (QWidget, QLabel, QHBoxLayout, QVBoxLayout,
QApplication, QPushButton, QSlider,
QFileDialog, QAction)
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
path = 'image.png'
original_img_label = QLabel('Original')
processed_img_label = QLabel('Processed')
orig_img = QPixmap(path)
global orig_lbl
orig_lbl = QLabel(self)
orig_lbl.setPixmap(orig_img)
proc_img = QPixmap(path)
proc_lbl = QLabel(self)
proc_lbl.setPixmap(proc_img)
select_image_btn = QPushButton('Select image')
select_image_btn.clicked.connect(self.getImage)
clean_image_btn = QPushButton('Clean image')
thresh1_sld = QSlider(Qt.Horizontal, self)
thresh2_sld = QSlider(Qt.Horizontal, self)
v_orig_lay = QVBoxLayout()
v_orig_lay.addWidget(original_img_label)
v_orig_lay.addWidget(orig_lbl)
v_proc_lay = QVBoxLayout()
v_proc_lay.addWidget(processed_img_label)
v_proc_lay.addWidget(proc_lbl)
h_img_lay = QHBoxLayout()
h_img_lay.addStretch(1)
h_img_lay.addLayout(v_orig_lay)
h_img_lay.addStretch(1)
h_img_lay.addLayout(v_proc_lay)
h_img_lay.addStretch(1)
h_btn_lay = QHBoxLayout()
h_btn_lay.addStretch(1)
h_btn_lay.addWidget(select_image_btn)
h_btn_lay.addWidget(clean_image_btn)
h_btn_lay.addStretch(1)
v_main_lay = QVBoxLayout()
v_main_lay.addLayout(h_img_lay)
v_main_lay.addStretch(1)
v_main_lay.addWidget(thresh1_sld)
v_main_lay.addWidget(thresh2_sld)
v_main_lay.addLayout(h_btn_lay)
self.setLayout(v_main_lay)
self.setGeometry(300, 300, 350, 300)
self.setWindowTitle('Review')
self.show()
def getImage(self):
filter = "Images (*.png *.jpg)"
image_obj = QFileDialog.getOpenFileName(self, 'Open image', 'Desktop', filter)
self.orig_lbl.setPixmap(QPixmap(image_obj))
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
getImage函数由select_image_btn按钮调用,希望通过setPixmap将image_obj(图像路径)放在orig_lbl上。在
一切正常,直到我选择了一个图像并在文件对话框中单击“确定”。执行此操作时,应用程序将关闭。在