Python文献调研:控件学习(1)

from PySide6.QtWidgets import QWidget, QApplication, QVBoxLayout, QHBoxLayout, QPushButton, QLabel, QLineEdit, \
    QComboBox, QCheckBox
from PySide6.QtCore import Qt


class MyWindow(QWidget):
    # 构造方法
    def __init__(self):
        super().__init__()
        # 添加布局
        self.mainLayout = QVBoxLayout()
        self.checkLayout = QHBoxLayout()
        # lineedit控件
        self.lineEdit1 = QLineEdit()
        self.lineEdit1.setPlaceholderText('请输入内容')
        self.lineEdit1.setEchoMode(QLineEdit.EchoMode.Password)
        self.lineEdit1.textChanged.connect(self.lineEdit1_textchanged)
        self.lineEdit1.returnPressed.connect(self.lineEdit1_returnPressed)
        # label控件
        self.label1 = QLabel('这是一个标签')
        self.label1.setAlignment(Qt.AlignmentFlag.AlignCenter)  # 文本中心对齐
        # QPushButton控件
        self.btn1 = QPushButton('按钮1')
        self.btn1.clicked.connect(self.btn1_clicked)
        # combobox控件
        self.comboBox1 = QComboBox()
        self.comboBox1.setPlaceholderText('请选择')
        self.comboBox1.addItems(['选项1', '选项2', '选项3'])
        self.comboBox1.removeItem(0)  # 删除第一个选项
        self.comboBox1.currentTextChanged.connect(self.cbTextChanged)  # 检测下拉框中文字改变的函数
        # checkbox控件
        self.checkBox1 = QCheckBox('复选框1')
        self.checkBox2 = QCheckBox('复选框2')
        self.checkBox1.setCheckable(False)  # 复选框1不可以被选中
        self.checkBox2.setChecked(True)  # 复选框2默认被选中
        self.checkBox2.stateChanged.connect(self.checkboxchanged)  # 复选框状态改变信号
        # 添加控件
        self.mainLayout.addWidget(self.label1)
        self.mainLayout.addWidget(self.btn1)
        self.mainLayout.addWidget(self.lineEdit1)
        self.mainLayout.addWidget(self.comboBox1)
        self.checkLayout.addWidget(self.checkBox1)
        self.checkLayout.addWidget(self.checkBox2)
        self.mainLayout.addLayout(self.checkLayout)
        self.setLayout(self.mainLayout)

    # 添加响应函数
    def btn1_clicked(self):
        self.label1.setText('标签被修改了')

    def lineEdit1_textchanged(self, text):
        print(f'文本内容变成了{text}')

    def lineEdit1_returnPressed(self):
        print('回车被按下')

    def cbTextChanged(self, text):
        print(f'下拉框内容变成了: {text}')

    def checkboxchanged(self, index):
        print(f'复选框2状态改变,当前状态为: {self.checkBox2.isChecked()},当前index为: {index}')  # 选中index为2,未选中index为0


# 主程序部分
if __name__ == '__main__':
    app = QApplication([])
    window = MyWindow()
    window.show()
    app.exec()
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蜜汁博哥

我是大学生,给钱

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值