• QPushButton、QRadioButton


    QPushButton
    '''
    
    按钮控件(QPushButton)
    
    QAbstractButton
    
    QPushButton
    AToolButton
    QRadioButton
    QCheckBox
    
    
    '''
    
    
    import sys
    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    from PyQt5.QtWidgets import *
    
    class QPushButtonDemo(QDialog) :
        def __init__(self):
            super(QPushButtonDemo,self).__init__()
            self.initUI()
    
        def initUI(self):
            self.setWindowTitle('QPushButton Demo')
    
            layout = QVBoxLayout()
    
            self.button1 = QPushButton('第1个按钮')
            self.button1.setText('First Button1')
            self.button1.setCheckable(True)
            self.button1.toggle()
            self.button1.clicked.connect(self.buttonState)
            self.button1.clicked.connect(lambda :self.whichButton(self.button1))
    
            layout.addWidget(self.button1)
    
            # 在文本前面显示图像
    
            self.button2 = QPushButton('图像按钮')
            self.button2.setIcon(QIcon(QPixmap('./images/python.png')))
            self.button2.clicked.connect(lambda:self.whichButton(self.button2))
            layout.addWidget(self.button2)
    
            self.button3 = QPushButton('不可用的按钮')
            self.button3.setEnabled(False)
            layout.addWidget(self.button3)
    
            self.button4 = QPushButton('&MyButton')
            self.button4.setDefault(True)
            self.button4.clicked.connect(lambda:self.whichButton(self.button4))
            layout.addWidget(self.button4)
    
            self.setLayout(layout)
            self.resize(400,300)
    
        def buttonState(self):
            if self.button1.isChecked():
                print('按钮1已经被选中')
            else:
                print('按钮1未被选中')
    
        def whichButton(self,btn):
            print('被单击的按钮是<' + btn.text() + '>')
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        main = QPushButtonDemo()
        main.show()
        sys.exit(app.exec_())
    
    image

    QRadioButton
    '''
    
    单选按钮控件(QRadioButton)
    
    '''
    
    import sys
    from PyQt5.QtCore import *
    from PyQt5.QtGui import *
    from PyQt5.QtWidgets import *
    
    class QRadioButtonDemo(QWidget):
        def __init__(self):
            super(QRadioButtonDemo,self).__init__()
            self.initUI()
        def initUI(self):
            self.setWindowTitle('QRadioButton')
            layout = QHBoxLayout()
            self.button1 = QRadioButton('单选按钮1')
            self.button1.setChecked(True)
    
            self.button1.toggled.connect(self.buttonState)
            layout.addWidget(self.button1)
    
            self.button2 = QRadioButton('单选按钮2')
            self.button2.toggled.connect(self.buttonState)
            layout.addWidget(self.button2)
            self.setLayout(layout)
    
    
        def buttonState(self):
            radioButton = self.sender()
    
            if radioButton.isChecked() == True:
                print('<' + radioButton.text() + '> 被选中')
            else:
                print('<' + radioButton.text() + '> 被取消选中状态')
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        main = QRadioButtonDemo()
        main.show()
        sys.exit(app.exec_())
    image

    天道酬勤 循序渐进 技压群雄
  • 相关阅读:
    scrapy 链接数据库创表语句
    工作问题总结
    插入排序
    centos6.5 安装python2.7.5
    冒泡排序
    [Python笔记]第十篇:模块续
    [Python笔记]第九篇:re正则表达式
    [Python笔记]第八篇:模块
    [Python笔记]第六篇:文件处理
    [Python笔记]第五篇:递归
  • 原文地址:https://www.cnblogs.com/wuyuan2011woaini/p/14821129.html
Copyright © 2020-2023  润新知