【发布时间】:2015-11-30 01:58:47
【问题描述】:
我必须编写一个带有从文件中打开图像的选项的程序。我必须使用QFileDialog 并在QLabel 中显示图像,使用QPixmap。我可以单独使用它们,但我没有设法将它们结合起来。
我想我需要从dlg.selectedFiles 中获取我的图像名称,但我不知道如何选择其中有有用数据的时刻。我是否需要在我的主程序中创建一个循环,并不断检查是否有图像要打开?我可以使用openAction.triggered.connect(...) 向我的标签发送信号吗?
from PyQt4 import QtGui
import sys
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent = None):
super(MainWindow, self).__init__(parent)
menubar = self.menuBar()
fileMenu = menubar.addMenu('File')
dlg = QtGui.QFileDialog(self)
openAction = QtGui.QAction('Open', self)
openAction.triggered.connect(dlg.open)
fileMenu.addAction(openAction)
#label = QtGui.QLabel(self)
#pixmap = QtGui.QPixmap('')
#label.setPixmap(pixmap)
def main():
app = QtGui.QApplication(sys.argv)
win = MainWindow()
win.show()
app.exec_()
if __name__ == '__main__':
sys.exit(main())
【问题讨论】:
标签: python pyqt4 qfiledialog