• Pyqt4学习笔记-窗口组件(更新ing)


    QCheckBox:复选框

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    import sys
    from PyQt4 import QtGui
    from PyQt4 import QtCore
    
    
    class CheckBoxExample(QtGui.QWidget):
    
        def __init__(self):
            super(CheckBoxExample, self).__init__()
    
            self.initUI()
    
        def initUI(self):
    
            self.setGeometry(300, 300, 250, 150)
            self.setWindowTitle('Checkbox')
    
            self.cb = QtGui.QCheckBox('Show title', self)
            # 初始化复选框
            self.cb.setFocusPolicy(QtCore.Qt.NoFocus)
            self.cb.move(10, 10)
            self.cb.toggle()
            # 切换复选框状态
            self.connect(self.cb, QtCore.SIGNAL('stateChanged(int)'),
                         self.changeTitle)
    
        def changeTitle(self, value):
    
            if self.cb.isChecked():
                self.setWindowTitle('Checkbox')
            else:
                self.setWindowTitle('')
    
    
    if __name__ == '__main__':
    
        app = QtGui.QApplication(sys.argv)
        ex = CheckBoxExample()
        ex.show()
        app.exec_()

    切换按钮

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    import sys
    from PyQt4 import QtGui
    from PyQt4 import QtCore
    
    
    class ToggleButtonExample(QtGui.QWidget):
    
        def __init__(self):
            super(ToggleButtonExample, self).__init__()
    
            self.initUI()
    
        def initUI(self):
    
            self.color = QtGui.QColor(0, 0, 0)
            # 黑色
    
            self.red = QtGui.QPushButton('Red', self)
            self.red.setCheckable(True)
            self.red.move(10, 10)
    
            self.connect(self.red, QtCore.SIGNAL('clicked()'), self.setColor)
    
            self.green = QtGui.QPushButton('Green', self)
            self.green.setCheckable(True)
            # setCheckable(true)为属性,表示可以选中
            self.green.move(10, 60)
    
            self.connect(self.green, QtCore.SIGNAL('clicked()'), self.setColor)
    
            self.blue = QtGui.QPushButton('Blue', self)
            self.blue.setCheckable(True)
            self.blue.move(10, 110)
    
            self.connect(self.blue, QtCore.SIGNAL('clicked()'), self.setColor)
    
            self.square = QtGui.QWidget(self)
            self.square.setGeometry(150, 20, 100, 100)
            self.square.setStyleSheet("QWidget { background-color: %s }" %
                                      self.color.name())
    
            self.setWindowTitle('ToggleButton')
            self.setGeometry(300, 300, 280, 170)
    
    
        def setColor(self):
    
            source = self.sender()
    
            if source.text() == "Red":
                if self.red.isChecked():
                    # 判断是否被选中
                    self.color.setRed(255)
                else: self.color.setRed(0)
    
            elif source.text() == "Green":
                if self.green.isChecked():
                    self.color.setGreen(255)
                else: self.color.setGreen(0)
    
            else:
                if self.blue.isChecked():
                    self.color.setBlue(255)
                else: self.color.setBlue(0)
    
            self.square.setStyleSheet("QWidget { background-color: %s }" %
                                      self.color.name())
    
    
    
    if __name__ == '__main__':
    
        app = QtGui.QApplication(sys.argv)
        ex = ToggleButtonExample()
        ex.show()
        app.exec_()

    蓝色的表示被选中。

    QSlider:滑块

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    import sys
    from PyQt4 import QtGui
    from PyQt4 import QtCore
    
    
    class SliderExample(QtGui.QWidget):
    
        def __init__(self):
            super(SliderExample, self).__init__()
            self.initUI()
    
        def initUI(self):
    
            slider = QtGui.QSlider(QtCore.Qt.Horizontal, self)
            slider.setFocusPolicy(QtCore.Qt.NoFocus)
            slider.setGeometry(30, 40, 100, 30)
            self.connect(slider, QtCore.SIGNAL('valueChanged(int)'),
                         self.changeValue)
    
            self.label = QtGui.QLabel(self)
            self.label.setPixmap(QtGui.QPixmap('images/mute.png'))
            self.label.setGeometry(160, 40, 300, 200)
    
            self.setWindowTitle('Slider')
            self.setGeometry(300, 300, 250, 150)
    
    
        def changeValue(self, value):
    
            if value == 0:
                self.label.setPixmap(QtGui.QPixmap('images/mute.png'))
            elif value > 0 and value <= 30:
                self.label.setPixmap(QtGui.QPixmap('images/min.png'))
            elif value > 30 and value < 80:
                self.label.setPixmap(QtGui.QPixmap('images/med.png'))
            else:
                self.label.setPixmap(QtGui.QPixmap('images/max.png'))
    
    
    if __name__ == '__main__':
    
        app = QtGui.QApplication(sys.argv)
        ex = SliderExample()
        ex.show()
        app.exec_()

    图片随着滑块的滚动改变。

     QProgressbar:滚动条

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    import sys
    from PyQt4 import QtGui
    from PyQt4 import QtCore
    
    
    class ProgressBarExample(QtGui.QWidget):
    
        def __init__(self):
            super(ProgressBarExample, self).__init__()
    
            self.initUI()
    
        def initUI(self):
    
            self.pbar = QtGui.QProgressBar(self)
            self.pbar.setGeometry(30, 40, 200, 25)
    
            self.button = QtGui.QPushButton('Start', self)
            self.button.setFocusPolicy(QtCore.Qt.NoFocus)
            self.button.move(40, 80)
    
            self.connect(self.button, QtCore.SIGNAL('clicked()'),
                         self.doAction)
    
            self.timer = QtCore.QBasicTimer()
            self.step = 0
    
            self.setWindowTitle('ProgressBar')
            self.setGeometry(300, 300, 250, 150)
    
    
        def timerEvent(self, event):
    
            if self.step >= 100:
                self.timer.stop()
                self.button.setText('Restart')
                return
    
            self.step = self.step + 1
            self.pbar.setValue(self.step)
    
        def doAction(self):
    
            if self.timer.isActive():
               # timer.started的状态
                self.timer.stop()
                self.button.setText('Continue')
            else:
                if self.step >= 100:
                    self.step = 0
                    # 清零
                self.timer.start(100, self)
                # start之后触发timerEvent
                self.button.setText('Stop')
    
    
    if __name__ == '__main__':
    
        app = QtGui.QApplication(sys.argv)
        ex = ProgressBarExample()
        ex.show()
        app.exec_()

    通过调用 start() 方法加载定时器事件,该方法有两个参数,超时时间( timeout )和接受事件的对象( object )。

     QLineEdit:单行编辑文本的组件,有撤销复制粘贴等基本功能

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    from PyQt4 import QtGui
    from PyQt4 import QtCore
    
    
    class QLineEditExample(QtGui.QWidget):
        def __init__(self, parent=None):
            QtGui.QWidget.__init__(self, parent)
    
            self.initUI()
    
    
        def initUI(self):
    
            self.label = QtGui.QLabel(self)
            edit = QtGui.QLineEdit(self)
    
            edit.move(60, 100)
            self.label.move(60, 40)
    
            self.connect(edit, QtCore.SIGNAL('textChanged(QString)'),
                         self.onChanged)
    
            self.setWindowTitle('QLineEdit')
            self.setGeometry(250, 200, 350, 250)
    
    
        def onChanged(self, text):
            self.label.setText(text)
            # 让label的text和编辑处的text同步
            self.label.adjustSize()
            # 让label自适应text的大小
    
    
    def main():
    
        app = QtGui.QApplication([])
        exm = QLineEditExample()
        exm.show()
        app.exec_()
    
    
    if __name__ == '__main__':
        main()

    QSplitter:分离器

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    from PyQt4 import QtGui, QtCore
    
    
    class SplitterExample(QtGui.QWidget):
    
        def __init__(self):
            super(SplitterExample, self).__init__()
    
            self.initUI()
    
    
        def initUI(self):
    
            hbox = QtGui.QHBoxLayout(self)
    
            topleft = QtGui.QFrame(self)
            topleft.setFrameShape(QtGui.QFrame.StyledPanel)
            # 为了看见边框设置有样式的框体
    
            topright = QtGui.QFrame(self)
            topright.setFrameShape(QtGui.QFrame.StyledPanel)
    
            bottom = QtGui.QFrame(self)
            bottom.setFrameShape(QtGui.QFrame.StyledPanel)
    
            splitter1 = QtGui.QSplitter(QtCore.Qt.Horizontal)
            splitter1.addWidget(topleft)
            splitter1.addWidget(topright)
    
            splitter2 = QtGui.QSplitter(QtCore.Qt.Vertical)
            splitter2.addWidget(splitter1)
            splitter2.addWidget(bottom)
    
            hbox.addWidget(splitter2)
            self.setLayout(hbox)
    
            self.setWindowTitle('QSplitter')
            QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('Cleanlooks'))
            # 设置分离器样式,某些样式是不可见的
            self.setGeometry(250, 200, 350, 250)
    
    
    def main():
    
        app = QtGui.QApplication([])
        ex = SplitterExample()
        ex.show()
        app.exec_()
    
    
    if __name__ == '__main__':
        main()

    可拖动的效果。

    QComboBox:下拉列表清单

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    from PyQt4 import QtGui, QtCore
    
    
    class ComboBoxExample(QtGui.QWidget):
    
        def __init__(self):
            super(ComboBoxExample, self).__init__()
    
            self.initUI()
    
    
        def initUI(self):
    
            self.label = QtGui.QLabel("Ubuntu", self)
    
            combo = QtGui.QComboBox(self)
            # 初始化,添加元素
            combo.addItem("Ubuntu")
            combo.addItem("Mandriva")
            combo.addItem("Fedora")
            combo.addItem("Red Hat")
            combo.addItem("Gentoo")
    
            combo.move(50, 50)
            self.label.move(50, 150)
    
            self.connect(combo, QtCore.SIGNAL('activated(QString)'),
                         self.onActivated)
            # 选项被选择时调用OnActivated
    
            self.setGeometry(250, 200, 350, 250)
            self.setWindowTitle('QComboBox')
    
        def onActivated(self, text):
    
            self.label.setText(text)
            self.label.adjustSize()
    
    
    def main():
    
        app = QtGui.QApplication([])
        ex = ComboBoxExample()
        ex.show()
        app.exec_()
    
    
    if __name__ == '__main__':
        main()

  • 相关阅读:
    独家干货!两种低代码模型驱动开发技术对比
    GeneXus低代码产品版本大更新,助力打造专业开发团队,加速企业数字化转型!
    企业级低代码平台应具备哪些要素?(下)
    如何选择低代码平台?GeneXus低代码平台
    C语言结构体的“继承”
    C语言实现wake on lan(网络唤醒)
    Linux驱动中的异步通知
    Linux网络设备驱动概述
    Linux下的广播程序
    DCMA86P2网卡成功显示802.11p
  • 原文地址:https://www.cnblogs.com/shadow-ccos/p/5210497.html
Copyright © 2020-2023  润新知