gpt4 book ai didi

image - 如何在 PyQT GUI 中显示图像?

转载 作者:行者123 更新时间:2023-12-05 03:11:16 25 4
gpt4 key购买 nike

我想在 PyQT 中使用文件选择器在 GUI 中显示图像。到目前为止,我设法打开图像文件,但完全不知道如何在 GUI 中显示图像。

import sys
from PyQt4 import QtGui, QtCore


class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(100, 100, 500, 300)
self.setWindowTitle("PyQT Show Image")

openFile = QtGui.QAction("&File", self)
openFile.setShortcut("Ctrl+O")
openFile.setStatusTip("Open File")
openFile.triggered.connect(self.file_open)

self.statusBar()

mainMenu = self.menuBar()

fileMenu = mainMenu.addMenu('&File')
fileMenu.addAction(openFile)

self.home()

def home(self):
self.show()

def file_open(self):
name = QtGui.QFileDialog.getOpenFileName(self, 'Open File')
file = open(name, 'r')

def run():
app = QtGui.QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())

run()

请在这里指导我如何在 GUI 中显示图像。谢谢

最佳答案

这个怎么样?

import sys
from PyQt4 import QtGui, QtCore


class Window(QtGui.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(100, 100, 500, 300)
self.setWindowTitle("PyQT Show Image")

openFile = QtGui.QAction("&File", self)
openFile.setShortcut("Ctrl+O")
openFile.setStatusTip("Open File")
openFile.triggered.connect(self.file_open)

self.statusBar()

mainMenu = self.menuBar()

fileMenu = mainMenu.addMenu('&File')
fileMenu.addAction(openFile)

self.lbl = QtGui.QLabel(self)
self.setCentralWidget(self.lbl)

self.home()

def home(self):
self.show()

def file_open(self):
name = QtGui.QFileDialog.getOpenFileName(self, 'Open File')

pixmap = QtGui.QPixmap(name)
self.lbl.setPixmap(pixmap.scaled(self.lbl.size()))


def run():
app = QtGui.QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())

run()

关于image - 如何在 PyQT GUI 中显示图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37711316/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com