zoukankan      html  css  js  c++  java
  • pyQT Dialog默认选中某一个选项问题的解决

    方法一:

    在新建ui文件时不要新建Dialog

    # -*- coding: utf-8 -*-
    
    # Form implementation generated from reading ui file 'D:pythonProjectspyqtUITestuntitled6.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界面会默认选中一个选项的问题。

  • 相关阅读:
    一个思绪凌乱的前端
    如何将你的github仓库部署到github pages(github.io博客)
    聊一聊前端系列篇
    Centos7升级gcc极简教程
    Pycharm报错连接linux服务器报错:Could not verify `ssh-rsa` host key with fingerprint
    基于Pytorch的简单小案例
    windows安装Pytorch报错:from torch._C import * ImportError: DLL load failed: 找不到指定的模块”解决方案
    scrapy抓取豆瓣电影相关数据
    scrapy结合selenium抓取武汉市环保局空气质量日报
    scrapy抓取国家社科基金项目数据库
  • 原文地址:https://www.cnblogs.com/foreverlin/p/10726567.html
Copyright © 2011-2022 走看看