1. 程式人生 > >pyQT Dialog默認選中某一個選項問題的解決

pyQT Dialog默認選中某一個選項問題的解決

meta import pan gets tex continue 圖片 log 中一

方法一:

在新建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界面會默認選中一個選項的問題。

pyQT Dialog默認選中某一個選項問題的解決