我正在为3D打印机开发应用程序。我只有python的基本知识,因此我在参考了一些教程后决定使用Qt Designer。在一个教程中(他在不使用设计器的情况下使用QtPy开发了GUI)中,教师添加了一个按钮,单击后将弹出一个对话框,要求您进行确认。我试图在应用程序中复制它。首先我遵循他的编码
这是代码
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Window(QtWidgets.QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setGeometry(50, 50, 500, 300)
self.setWindowTitle("test")
self.setWindowIcon(QtGui.QIcon('clay.png'))
self.home()
def home(self):
#push button
btn = QtWidgets.QPushButton("Quit", self)
btn.clicked.connect(self.close_application)
btn.resize(btn.minimumSizeHint())
btn.move(0, 100)
self.show()
def close_application(self):
#message box
choice = QtWidgets.QMessageBox.question(self, "extract!", "get inside!!!",QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
if choice == QtWidgets.QMessageBox.Yes:
print("extracting")
sys.exit()
else:
pass
def run():
app = QtWidgets.QApplication(sys.argv)
GUI = Window()
sys.exit(app.exec_())
run()
然后我尝试将其添加到Qt设计器开发的代码中,
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# 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.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(70, 90, 93, 28))
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
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)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
self.pushButton.clicked.connect(self.close_app)
def close_app(self):
#message box
choice = QtWidgets.QMessageBox.question(self, "Exit", "Are you sure?",QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
if choice == QtWidgets.QMessageBox.Yes:
print("extracting")
sys.exit()
else:
pass
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_())
但是显示错误
回溯(最近通话最近一次):
File "C:\Users\New User\Desktop\test\test - Copy.py", line 40, in close_app
choice = QtWidgets.QMessageBox.question(self, "Exit", "Are you sure?",QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
TypeError: question(QWidget, str, str, buttons: Union[QMessageBox.StandardButtons, QMessageBox.StandardButton] = QMessageBox.StandardButtons(QMessageBox.Yes|QMessageBox.No), defaultButton: QMessageBox.StandardButton = QMessageBox.NoButton): argument 1 has unexpected type 'Ui_MainWindow'