checkbox(PyQt55控件使用2Radio ButtonCheck Box)

被浏览:6696

关注者:399

最佳回答:

千呼万唤使出来,抽个空更新一下PyQt5系列。

本期练习Radio Button和Check Box,Combo Box下期再来吧。

UI设计如下:

alt="PyQt5-5.控件使用2(Radio Button、Check Box)" >

UI设计界面

本次演示蓝色框内,增加了两个Radio Button和一个Check Box。演示功能为

预运行如下:

alt="PyQt5-5.控件使用2(Radio Button、Check Box)" >

预运行界面

补充一下:

Check Box有以下三种状态:未选中0,半选中1,选中2,其中从UI界面操作只有选中和未选中两种状态,即0和2,1状态可以在后端用代码显示。

alt="PyQt5-5.控件使用2(Radio Button、Check Box)" >

Check Box三种状态

运行演示1:

alt="PyQt5-5.控件使用2(Radio Button、Check Box)" >

直接相加,无平方

运行演示2:

alt="PyQt5-5.控件使用2(Radio Button、Check Box)" >

相加后平方

运行演示3:

alt="PyQt5-5.控件使用2(Radio Button、Check Box)" >

相减,不平方

运行演示4:

alt="PyQt5-5.控件使用2(Radio Button、Check Box)" >

相减后平方

tips:因为L ine Edit获得的是str,所以需要做一下格式转换。

附代码:

from PyQt5.QtWidgets import *from PyQt5.QtCore import *from PyQt5.QtGui import *from pyqt1 import Ui_MainWindowimport sysclass MainWindow(QMainWindow, Ui_MainWindow): def __init__(self, parent=None): super(MainWindow, self).__init__(parent) self.setupUi(self) @pyqtSlot() def on_pushButton1_clicked(self): self.getLabelcontent = self.lineEdit1.text() if len(self.getLabelcontent) == 0: self.label2.setText('未输入信息!') else: self.label2.setText('输入信息为:%s'%self.getLabelcontent) @pyqtSlot() def on_pushButton_clicked(self): c = 0 try: a = int(self.lineEdit_a.text()) except: self.lineEdit_c.setText('a输入数据异常') try: b = int(self.lineEdit_b.text()) except: self.lineEdit_c.setText('b输入数据异常') try: if self.RB_plus.isChecked(): c = a + b if self.RB_subtracte.isChecked(): c = a - b if self.checkBox.checkState() > 0: c = c ** 2 self.lineEdit_c.setText(str(c)) except: self.lineEdit_c.setText('计算异常')if __name__ == "__main__": app = QApplication(sys.argv) ui = MainWindow() ui.show() sys.exit(app.exec_())

获赞数:213

收藏数:57

回答时间:2024-04-22 19:14:32