from PySide2.QtWidgets import QApplication
from PySide2.QtUiTools import QUiLoader
from PySide2.QtCore import QFile
from PySide2.QtWidgets import QRadioButton
from PySide2.QtWidgets import QTableWidgetItem
from PySide2.QtCore import Qt
from PySide2.QtWidgets import QMessageBox
class Control3():
def __init__(self):
ui_file = QFile("./uiDic/clickButton3.ui")
ui_file.open(QFile.ReadOnly)
ui_file.close()
# 从文件中加载UI定义
# 从 UI 定义中动态 创建一个相应的窗口对象
# 注意:里面的控件对象也成为窗口对象的属性了
# 比如 self.ui.button , self.ui.textEdit
self.ui = QUiLoader().load(ui_file)
def buttonGroupControl(self):
'''
单选按钮控件,处于一个按钮组或者窗口对象中的按钮时互斥的(仅能选一个),在按钮的QAbstractButton类中,勾选checked
按钮,可以设计按钮默认选中状态,下面buttonGroup为按钮组的对象名称
点击到对应的按钮组发出点击信号后执行handleButtonClicked方法。
:return:
'''
self.ui.buttonGroup.buttonClicked.connect(self.handleButtonClicked1)
def handleButtonClicked1(self):
'''
调用按钮组的checkedButton()方法,检查哪个按钮被选中,并使用text()方法获取文本
:return:
'''
val = self.ui.buttonGroup.checkedButton().text()
QMessageBox.about(self.ui, '选项', val) # 弹窗展示文本框输入的结果
def checkBoxControl(self):
'''
复选框按钮,使用按钮组的QButtonGroup中的 exclusive 属性,勾选表示复选框中只能选中一个
1.如果设置复选框为单选,则使用按钮组的heckedButton()方法来判定是哪个复选框被勾选了,并使用text()方法来获取文本
2.如果设置复选框为多选,则使用isChecked 方法来判定所有的按钮是否被勾线,然后使用text()方法来获取文本
:return:
'''
self.ui.buttonGroup_2.buttonClicked.connect(self.handleButtonClicked2)
def handleButtonClicked2(self):
'''
调用按钮组的checkedButton()方法,检查哪个按钮被选中,并使用text()方法获取文本
:return:
'''
val = self.ui.buttonGroup_2.checkedButton().text()
QMessageBox.about(self.ui, '选项', val) # 弹窗展示文本框输入的结果
def boxChange(self):
'''
整数计数器spinBox,使用valueChanged()方法来捕捉值的变化,将其连接到其他方法上去
:return:
'''
self.ui.spinBox.valueChanged.connect(self.boxNumber)
def boxNumber(self):
'''
整数计数器spinBox使用value()方法来获取值,另外使用setValue()方法来设置其默认值,使用setMinimum()来设置最小值,使用setMaximum()来设置最大值,
使用singleStep()方法来设置步长,另外QDoubleSpinBox和spinBox方法一样,只是一个处理整数,一个处理浮点数
:return:
'''
self.ui.textBrowser.append(str(self.ui.spinBox.value())) # 多行展示文本框末尾添加文本,不会自动换行
self.ui.textBrowser.ensureCursorVisible() # 在末尾添加了内容,自动翻滚到当前添加的这行,自动换行
QMessageBox.about(self.ui, '数字', str(self.ui.spinBox.value())) # 弹窗展示文本框输入的结果
def dateEditControl(self):
'''
获取时间控件dateEdit的值,先要实例化date()方法,然后使用year()、month()、day()获取“年、月、日”,使用toString()方法可以指定格式输出年月日
:return:
'''
qdate = self.ui.dateEdit.date() # 实例化一个date()对象
dateStr = qdate.toString('yyyy-MM-dd') # 可以转化为 指定格式的字符串
QMessageBox.about(self.ui, '数字', dateStr) # 弹窗展示文本框输入的结果
year = qdate.year() # 获取年份
month = qdate.month() # 获取月份
day = qdate.day() # 获取日
# print(year,month,day)
app = QApplication([])
stats = Control3()
# stats.buttonGroupControl()
stats.checkBoxControl()
# stats.buttonText()
stats.ui.show()
app.exec_()
QT Pyside2常用按钮控件方法(三)
于 2022-06-06 21:39:05 首次发布