我正在尝试在 QMainWindow 类中显示图片:
from PyQt5.QtWidgets import QLabel, QMainWindow, QApplication
from PyQt5.QtGui import QPixmap
import sys
class Menu(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Title")
label = QLabel(self)
pixmap = QPixmap('capture.png')
label.setPixmap(pixmap)
self.resize(pixmap.width(), pixmap.height())
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Menu()
sys.exit(app.exec_())
但它不显示图像,只是打开窗口。我坚持使用 QMainWindow
类,因为我正在尝试编写类似绘画应用程序的东西,这样我就可以编写菜单,并且可以在图片上书写.
如有任何建议,我们将不胜感激。
谢谢。
最佳答案
QMainWindow.setCentralWidget(widget)
Sets the given widget to be the main window’s central widget.
from PyQt5.QtWidgets import QLabel, QMainWindow, QApplication, QWidget, QVBoxLayout
from PyQt5.QtGui import QPixmap
import sys
class Menu(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Title")
self.central_widget = QWidget()
self.setCentralWidget(self.central_widget)
lay = QVBoxLayout(self.central_widget)
label = QLabel(self)
pixmap = QPixmap('logo.png')
label.setPixmap(pixmap)
self.resize(pixmap.width(), pixmap.height())
lay.addWidget(label)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Menu()
sys.exit(app.exec_())
关于python - PyQt5 - 如何在 QMainWindow 类中显示图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51430586/