使用PyQt5和Qt Designer创建Python GUI应用程序(四)
self.photo.setPixmap(GtGUI.QPixmap("linuxidc.jpg"))
# “linuxidc.jpg”是和我的python文件在同一个目录中的一张照片
def show_chemo(self):
self.photo.setPixmap(QtGUI.QPixmap("linuxidc.com.jpg"))
def show_meinv(self):
self.photo.setPixmap(QtGUI.Qpixmap("linuxidc.jpg"))
def setupUI(self):
...
self.meinv.clicked.connect(self.show_meinv)
self.chemo.clicked.connect(self.show_chemo)
完整代码
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.photo = QtWidgets.QLabel(self.centralwidget)
self.photo.setGeometry(QtCore.QRect(0, 0, 841, 511))
self.photo.setText("")
self.photo.setPixmap(QtGui.QPixmap("linuxidc.com.jpg"))
self.photo.setScaledContents(True)
self.photo.setObjectName("photo")
self.meinv = QtWidgets.QPushButton(self.centralwidget)
self.meinv.setGeometry(QtCore.QRect(0, 510, 411, 41))
self.meinv.setObjectName("meinv")
self.chemo = QtWidgets.QPushButton(self.centralwidget)
self.chemo.setGeometry(QtCore.QRect(410, 510, 391, 41))
self.chemo.setObjectName("chemo")
MainWindow.setCentralWidget(self.cntralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.chemo.clicked.connect(self.show_chemo)
self.meinv.clicked.connect(self.show_meinv)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Linux公社 www.linuxidc.com"))
self.meinv.setText(_translate("MainWindow", "车模"))
self.chemo.setText(_translate("MainWindow", "美女"))
def show_chemo(self):
self.photo.setPixmap(QtGui.QPixmap("linuxidc.jpg"))
def show_meinv(self):
self.photo.setPixmap(QtGui.QPixmap("linuxidc.com.jpg"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
在下一个教程中,我们将讨论像素图控件QPixmap。
使用PyQt5和Qt Designer创建Python
GUI应用程序 见 https://www.linuxidc.com/search.aspx?where=nkey&keyword=65658
Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx
本文永久更新链接地址:https://www.linuxidc.com/Linux/2020-05/163150.htm
微信赞赏支付宝赞赏