方法一:
在新建ui文件时不要新建Dialog
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'D:\pythonProjects\pyqtUITest\untitled6.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
from PyQt5.QtCore import * class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(188, 250)
self.exit_game = QtWidgets.QPushButton(Form)
self.exit_game.setGeometry(QtCore.QRect(0, 200, 189, 51))
self.exit_game.setObjectName("exit_game")
self.new_game = QtWidgets.QPushButton(Form)
self.new_game.setGeometry(QtCore.QRect(0, 50, 189, 51))
self.new_game.setObjectName("new_game")
self.return_to_mainMenu = QtWidgets.QPushButton(Form)
self.return_to_mainMenu.setGeometry(QtCore.QRect(0, 150, 189, 51))
self.return_to_mainMenu.setObjectName("return_to_mainMenu")
self.continue_button = QtWidgets.QPushButton(Form)
self.continue_button.setGeometry(QtCore.QRect(0, 0, 189, 51))
self.continue_button.setObjectName("continue_button")
self.ranking_list = QtWidgets.QPushButton(Form)
self.ranking_list.setGeometry(QtCore.QRect(0, 100, 189, 51))
self.ranking_list.setObjectName("ranking_list") Form.setWindowFlags(Qt.FramelessWindowHint) self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Form"))
self.exit_game.setText(_translate("Form", "退出游戏"))
self.new_game.setText(_translate("Form", "新游戏"))
self.return_to_mainMenu.setText(_translate("Form", "返回主菜单"))
self.continue_button.setText(_translate("Form", "继续游戏"))
self.ranking_list.setText(_translate("Form", "排行榜")) if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())
方法二:在建的Dialog模板中传入MainWindow的参数
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'Sub_menu.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
# from PyQt5.QtCore import *
# from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(189, 250)
# self.setWindowFlags(Qt.FramelessWindowHint)
# self.setWindowFlags(Qt.FramelessWindowHint)
self.pushButton_3 = QtWidgets.QPushButton(Dialog)
self.pushButton_3.setGeometry(QtCore.QRect(0, 0, 189, 51))
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_4 = QtWidgets.QPushButton(Dialog)
self.pushButton_4.setGeometry(QtCore.QRect(0, 50, 189, 51))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_6 = QtWidgets.QPushButton(Dialog)
self.pushButton_6.setGeometry(QtCore.QRect(0, 100, 189, 51))
self.pushButton_6.setObjectName("pushButton_6")
self.pushButton_7 = QtWidgets.QPushButton(Dialog)
self.pushButton_7.setGeometry(QtCore.QRect(0, 150, 189, 51))
self.pushButton_7.setObjectName("pushButton_7")
self.pushButton_8 = QtWidgets.QPushButton(Dialog)
self.pushButton_8.setGeometry(QtCore.QRect(0, 200, 189, 51))
self.pushButton_8.setObjectName("pushButton_8") Dialog.setWindowFlags(Qt.FramelessWindowHint)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog) def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.pushButton_3.setText(_translate("Dialog", "继续游戏"))
self.pushButton_4.setText(_translate("Dialog", "新游戏"))
self.pushButton_6.setText(_translate("Dialog", "排行榜"))
self.pushButton_7.setText(_translate("Dialog", "返回主菜单"))
self.pushButton_8.setText(_translate("Dialog", "退出游戏")) if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_Dialog()
ui.setupUi(MainWindow) #这里传入了一个MainWindow的参数
MainWindow.show()
sys.exit(app.exec_())
这里我喜欢使用第一种方法解决Dialog界面会默认选中一个选项的问题。