我使用PyQt5写一个窗体(GUI)程序,我尝试逻辑代码与UI分离,结果第一次成功了,第二次换了个窗体,出错了。
错误报告:
D:\Program\Python\venv\Scripts\python.exe D:\Program\Python\Python日常编程\LifeReopensForGUI\begin.py
Traceback (most recent call last):
File "D:\Program\Python\Python日常编程\LifeReopensForGUI\begin.py", line 15, in <module>
mainMenuWindow.show()
TypeError: 'QPushButton' object is not callable
进程已结束,退出代码1
逻辑代码如下:
from PyQt5 import QtWidgets, QtGui, QtCore
import MainMenu # 这是窗体文件
import sys
class MainMenuWindow(QtWidgets.QMainWindow, MainMenu.Ui_MainMenu):
def __init__(self):
super(MainMenuWindow, self).__init__()
self.setupUi(self)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
mainMenuWindow = MainMenuWindow()
mainMenuWindow.show()
sys.exit(app.exec_())
UI文件代码如下:
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'MainMenu.ui'
#
# Created by: PyQt5 UI code generator 5.15.9
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from PyQt5.QtWidgets import QMainWindow
class Ui_MainMenu(object):
def setupUi(self, MainMenu):
MainMenu.setObjectName("MainMenu")
MainMenu.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainMenu)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(0, 0, 801, 271))
self.label.setText("")
self.label.setPixmap(QtGui.QPixmap(":/png/resource Files/人生重开.png"))
self.label.setScaledContents(True)
self.label.setObjectName("label")
self.verticalLayoutWidget = QtWidgets.QWidget(self.centralwidget)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(10, 280, 781, 261))
self.verticalLayoutWidget.setObjectName("verticalLayoutWidget")
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.verticalLayout.setObjectName("verticalLayout")
self.work = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.work.setObjectName("work")
self.verticalLayout.addWidget(self.work)
self.goToSchool = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.goToSchool.setObjectName("goToSchool")
self.verticalLayout.addWidget(self.goToSchool)
self.show = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.show.setObjectName("show")
self.verticalLayout.addWidget(self.show)
self.study = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.study.setObjectName("study")
self.verticalLayout.addWidget(self.study)
self.openBag = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.openBag.setObjectName("openBag")
self.verticalLayout.addWidget(self.openBag)
self.goToStore = QtWidgets.QPushButton(self.verticalLayoutWidget)
self.goToStore.setObjectName("goToStore")
self.verticalLayout.addWidget(self.goToStore)
MainMenu.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainMenu)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
self.menubar.setObjectName("menubar")
MainMenu.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainMenu)
self.statusbar.setObjectName("statusbar")
MainMenu.setStatusBar(self.statusbar)
self.retranslateUi(MainMenu)
QtCore.QMetaObject.connectSlotsByName(MainMenu)
def retranslateUi(self, MainMenu):
_translate = QtCore.QCoreApplication.translate
MainMenu.setWindowTitle(_translate("MainMenu", "主界面"))
self.work.setText(_translate("MainMenu", "工作"))
self.goToSchool.setText(_translate("MainMenu", "去学校"))
self.show.setText(_translate("MainMenu", "显示属性"))
self.study.setText(_translate("MainMenu", "学习"))
self.openBag.setText(_translate("MainMenu", "打开背包"))
self.goToStore.setText(_translate("MainMenu", "去商店"))
运行结果:
D:\Program\Python\venv\Scripts\python.exe D:\Program\Python\Python日常编程\LifeReopensForGUI\begin.py
Traceback (most recent call last):
File "D:\Program\Python\Python日常编程\LifeReopensForGUI\begin.py", line 15, in <module>
mainMenuWindow.show()
TypeError: 'QPushButton' object is not callable
进程已结束,退出代码1
我的解答思路和尝试过的方法:
我先在UI文件运行窗体,发现没问题.窗体正常显示.
我把窗体类继承的改为QMainWindow,还是有问题.