Python Qt学习(四)Radio Button

代码

复制代码
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'D:\Works\Python\Qt\qt_radiobutton.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
from PyQt5.QtWidgets import QApplication
import sys


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(287, 349)
        self.radioButton = QtWidgets.QRadioButton(MainWindow)
        self.radioButton.setGeometry(QtCore.QRect(30, 50, 115, 19))
        self.radioButton.setChecked(True)
        self.radioButton.setAutoRepeat(False)
        self.radioButton.setObjectName("radioButton")

        self.radioButton_2 = QtWidgets.QRadioButton(MainWindow)
        self.radioButton_2.setGeometry(QtCore.QRect(30, 80, 115, 19))
        self.radioButton_2.setObjectName("radioButton_2")

        self.pushButton = QtWidgets.QPushButton(MainWindow)
        self.pushButton.setGeometry(QtCore.QRect(20, 170, 191, 28))
        self.pushButton.setObjectName("pushButton")
        self.pushButton.clicked.connect(self.getCurrentRadioButton)

        self.pushButton_2 = QtWidgets.QPushButton(MainWindow)
        self.pushButton_2.setGeometry(QtCore.QRect(20, 220, 191, 28))
        self.pushButton_2.setObjectName("pushButton_2")
        self.pushButton_2.clicked.connect(self.setRadioButton1Check)

        self.pushButton_3 = QtWidgets.QPushButton(MainWindow)
        self.pushButton_3.setGeometry(QtCore.QRect(20, 270, 191, 28))
        self.pushButton_3.setObjectName("pushButton_3")
        self.pushButton_3.clicked.connect(self.setRadioButton2Check)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.radioButton.setText(_translate("MainWindow", "RadioButton 1"))
        self.radioButton_2.setText(_translate("MainWindow", "RadioButton 2"))
        self.pushButton.setText(_translate("MainWindow", "Get Current Radio Button"))
        self.pushButton_2.setText(_translate("MainWindow", "Set Radio Button 1"))
        self.pushButton_3.setText(_translate("MainWindow", "Set Radio Button 2"))

    def show(self, Form):
        Form.show()

    def getCurrentRadioButton(self):        
        if self.radioButton.isChecked()==True:
            self.messageBox("Radio Button 1 Checked")
        if self.radioButton_2.isChecked()==True:
            self.messageBox("Radio Button 2 Checked")

    def setRadioButton1Check(self):
        self.radioButton.setChecked(True)

    def setRadioButton2Check(self):
        self.radioButton_2.setChecked(True)
    
    def messageBox(self,msg):
        msgBox = QtWidgets.QMessageBox()
        msgBox.setIcon(QtWidgets.QMessageBox.Icon.Information)
        msgBox.setWindowTitle("Qt Message Box")
        msgBox.setText(msg)
        msgBox.setStandardButtons(QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Cancel)
        returnValue = msgBox.exec()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    Form=QtWidgets.QWidget()    
    main_win = Ui_MainWindow()    
    main_win.setupUi(Form)
    main_win.show(Form)

    sys.exit(app.exec())    

运行截图:

相关推荐
Learn-Python22 分钟前
MongoDB-only方法
python·sql
小途软件1 小时前
用于机器人电池电量预测的Sarsa强化学习混合集成方法
java·人工智能·pytorch·python·深度学习·语言模型
扫地的小何尚2 小时前
NVIDIA RTX PC开源AI工具升级:加速LLM和扩散模型的性能革命
人工智能·python·算法·开源·nvidia·1024程序员节
wanglei2007082 小时前
生产者消费者
开发语言·python
清水白石0083 小时前
《从零到进阶:Pydantic v1 与 v2 的核心差异与零成本校验实现原理》
数据库·python
昵称已被吞噬~‘(*@﹏@*)’~3 小时前
【RL+空战】学习记录03:基于JSBSim构造简易空空导弹模型,并结合python接口调用测试
开发语言·人工智能·python·学习·深度强化学习·jsbsim·空战
2501_941877983 小时前
从配置热更新到运行时自适应的互联网工程语法演进与多语言实践随笔分享
开发语言·前端·python
酩酊仙人3 小时前
fastmcp构建mcp server和client
python·ai·mcp
且去填词4 小时前
DeepSeek API 深度解析:从流式输出、Function Calling 到构建拥有“手脚”的 AI 应用
人工智能·python·语言模型·llm·agent·deepseek
rgeshfgreh4 小时前
Python条件与循环实战指南
python