博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PyGt5控件学习
阅读量:3898 次
发布时间:2019-05-23

本文共 9164 字,大约阅读时间需要 30 分钟。

PyQt5提供各种各样的控件,包括按钮、复选框、滑动条、列表框等

QCheckBox复选框控件

import sysfrom PyQt5.QtWidgets import QWidget, QCheckBox, QApplicationfrom PyQt5.QtCore import Qtclass Example(QWidget):    def __init__(self):        super().__init__()        self.initUI()    def initUI(self):        cb = QCheckBox('Show title', self)        cb.move(20, 20)        cb.toggle()        cb.stateChanged.connect(self.changeTitle)        self.setGeometry(300, 300, 250, 150)        self.setWindowTitle('QCheckBox')        self.show()    def changeTitle(self, state):        if state == Qt.Checked:            self.setWindowTitle('QCheckBox')        else:            self.setWindowTitle('Python')if __name__ == '__main__':    app = QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())

在这里插入图片描述

ToggleButton是QPushButton的一种特殊模式。它是一个有两种状态的按钮:按下与未按下

import sysfrom PyQt5.QtWidgets import (QWidget, QPushButton,                             QFrame, QApplication)from PyQt5.QtGui import QColorclass Example(QWidget):    def __init__(self):        super().__init__()        self.initUI()    def initUI(self):        self.col = QColor(0, 0, 0)        redb = QPushButton('Red', self)        redb.setCheckable(True)        redb.move(10, 10)        redb.clicked[bool].connect(self.setColor)        greenb = QPushButton('Green', self)        greenb.setCheckable(True)        greenb.move(10, 60)        greenb.clicked[bool].connect(self.setColor)        blueb = QPushButton('Blue', self)        blueb.setCheckable(True)        blueb.move(10, 110)        blueb.clicked[bool].connect(self.setColor)        self.square = QFrame(self)        self.square.setGeometry(150, 20, 100, 100)        self.square.setStyleSheet("QWidget { background-color: %s }" %                                  self.col.name())        self.setGeometry(300, 300, 280, 170)        self.setWindowTitle('Toggle button')        self.show()    def setColor(self, pressed):        source = self.sender()        if pressed:        #这里是一个复位,避免颜色混合            self.col = QColor(0, 0, 0)            val = 255        else:            val = 0        if source.text() == "Red":            self.col.setRed(val)        elif source.text() == "Green":            self.col.setGreen(val)        else:            self.col.setBlue(val)        self.square.setStyleSheet("QFrame { background-color: %s }" %                                  self.col.name())if __name__ == '__main__':    app = QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())

在这里插入图片描述

QSlider是一个带有简单滑块的控件

import sysfrom PyQt5.QtWidgets import (QWidget, QSlider,                             QLabel, QApplication)from PyQt5.QtCore import Qtfrom PyQt5.QtGui import QPixmapclass Example(QWidget):    def __init__(self):        super().__init__()        self.initUI()    def initUI(self):        sld = QSlider(Qt.Horizontal, self)        #sld.setFocusPolicy(Qt.NoFocus)        sld.setGeometry(30, 40, 100, 30)        sld.valueChanged[int].connect(self.changeValue)        self.label = QLabel(self)        self.label.setPixmap(QPixmap('audio.ico'))        self.label.setGeometry(160, 40, 80, 30)        self.setGeometry(300, 300, 280, 170)        self.setWindowTitle('QSlider')        self.show()            def changeValue(self, value):        if value == 0:            self.label.setPixmap(QPixmap('audio.ico'))        elif value > 0 and value <= 30:            self.label.setPixmap(QPixmap('min.ico'))        elif value > 30 and value < 80:            self.label.setPixmap(QPixmap('med.ico'))        else:            self.label.setPixmap(QPixmap('max.ico'))if __name__ == '__main__':    app = QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())

进度条QProgressBar

import sysfrom PyQt5.QtWidgets import (QWidget, QProgressBar,                             QPushButton, QApplication)from PyQt5.QtCore import QBasicTimerclass Example(QWidget):    def __init__(self):        super().__init__()        self.initUI()    def initUI(self):        self.pbar = QProgressBar(self)        self.pbar.setGeometry(30, 40, 200, 25)        self.btn = QPushButton('Start', self)        self.btn.move(30, 80)        self.btn.clicked.connect(self.doAction)        self.btn1 = QPushButton('Restart', self)        self.btn1.move(120, 80)        self.btn1.clicked.connect(self.doAction1)        self.timer = QBasicTimer()        self.step = 0        self.setGeometry(300, 300, 280, 170)        self.setWindowTitle('QProgressBar')        self.show()    def timerEvent(self, e):        if self.step >= 100:            self.timer.stop()            self.btn.setText('Finished')            return        self.step = self.step + 1        self.pbar.setValue(self.step)    def doAction1(self):        self.step = 0        self.pbar.setValue(self.step)    def doAction(self):        if self.timer.isActive():            self.timer.stop()            self.btn.setText('Start')        else:            #设置速率100ms            self.timer.start(100, self)            self.btn.setText('Stop')if __name__ == '__main__':    app = QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())

在这里插入图片描述

日历控件 QCalendarWidget

import sysfrom PyQt5.QtWidgets import (QWidget, QCalendarWidget,                             QLabel, QApplication)from PyQt5.QtCore import QDateclass Example(QWidget):    def __init__(self):        super().__init__()        self.initUI()    def initUI(self):        cal = QCalendarWidget(self)        cal.setGridVisible(True)        cal.move(20, 20)        cal.clicked[QDate].connect(self.showDate)        self.lbl = QLabel(self)        #初始显示        date = cal.selectedDate()        self.lbl.setText(date.toString())        self.lbl.move(130, 260)        self.setGeometry(300, 300, 350, 300)        self.setWindowTitle('Calendar')        self.show()    def showDate(self, date):        self.lbl.setText(date.toString())if __name__ == '__main__':    app = QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())

在这里插入图片描述

QPixmap是用于处理图像的控件

import sysfrom PyQt5.QtWidgets import (QWidget, QHBoxLayout,                             QLabel, QApplication)from PyQt5.QtGui import QPixmapclass Example(QWidget):    def __init__(self):        super().__init__()        self.initUI()    def initUI(self):        hbox = QHBoxLayout(self)        pixmap = QPixmap("python.jpg")        lbl = QLabel(self)        lbl.setPixmap(pixmap)        hbox.addWidget(lbl)        self.setLayout(hbox)        self.move(300, 200)        self.setWindowTitle('Red Rock')        self.show()if __name__ == '__main__':    app = QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())

在这里插入图片描述

QLineEdit是用于输入或编辑单行文本的控件

import sysfrom PyQt5.QtWidgets import (QWidget, QLabel,                             QLineEdit, QApplication)class Example(QWidget):    def __init__(self):        super().__init__()        self.initUI()    def initUI(self):        self.lbl = QLabel(self)        qle = QLineEdit(self)        qle.move(60, 100)        self.lbl.move(60, 40)        qle.textChanged[str].connect(self.onChanged)        self.setGeometry(300, 300, 280, 170)        self.setWindowTitle('QLineEdit')        self.show()    def onChanged(self, text):        self.lbl.setText(text)        self.lbl.adjustSize()if __name__ == '__main__':    app = QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())

在这里插入图片描述

QSplitter拖动子控件边界来调整子控件的尺寸

import sysfrom PyQt5.QtWidgets import (QWidget, QHBoxLayout, QFrame,                             QSplitter, QStyleFactory, QApplication)from PyQt5.QtCore import Qtclass Example(QWidget):    def __init__(self):        super().__init__()        self.initUI()    def initUI(self):        hbox = QHBoxLayout(self)        topleft = QFrame(self)        topleft.setFrameShape(QFrame.StyledPanel)        topright = QFrame(self)        topright.setFrameShape(QFrame.StyledPanel)        bottom = QFrame(self)        bottom.setFrameShape(QFrame.StyledPanel)        splitter1 = QSplitter(Qt.Horizontal)        splitter1.addWidget(topleft)        splitter1.addWidget(topright)        splitter2 = QSplitter(Qt.Vertical)        splitter2.addWidget(splitter1)        splitter2.addWidget(bottom)        hbox.addWidget(splitter2)        self.setLayout(hbox)        self.setGeometry(300, 300, 300, 200)        self.setWindowTitle('QSplitter')        self.show()    def onChanged(self, text):        self.lbl.setText(text)        self.lbl.adjustSize()if __name__ == '__main__':    app = QApplication(sys.argv)    ex = Example()    sys.exit(app.exec_())

在这里插入图片描述

QComboBox是允许用户从下拉列表中进行选择的控件。

import sysfrom PyQt5.QtWidgets import (QWidget, QLabel,                             QComboBox, QApplication)class Example(QWidget):    def __init__(self):        super().__init__()        self.initUI()    def initUI(self):        self.lbl = QLabel("Ubuntu", self)        combo = QComboBox(self)        combo.addItem("Ubuntu")        combo.addItem("Mandriva")        combo.addItem("Fedora")        combo.addItem("Arch")        combo.addItem("Gentoo")        combo.move(50, 50)        self.lbl.move(50, 150)        combo.activated[str].connect(self.onActivated)        self.setGeometry(300, 300, 300, 200)        self.setWindowTitle('QComboBox')        self.show()    def onActivated(self, text):        self.lbl.setText(text)        self.lbl.adjustSize()

在这里插入图片描述

转载地址:http://haben.baihongyu.com/

你可能感兴趣的文章
[小技巧] svn: 不能解析 URL
查看>>
USB_ModeSwitch 介绍
查看>>
大公司和小公司的抢人战,孰胜孰负?
查看>>
通过make编译多文件的内核模块
查看>>
如何调试Javascript代码
查看>>
皮克斯宣布开源Universal Scene Description
查看>>
复盘:一个创业项目的失败之路
查看>>
阿里巴巴宣布加入Linux基金会
查看>>
为什么你应该尝试 “全栈”
查看>>
程序员什么时候该考虑辞职
查看>>
如何写一本书?
查看>>
加班能体现编程的热情吗?
查看>>
Hadley Wickham:一个改变了R的人
查看>>
glibc 指导委员会解散声明
查看>>
Linux创始者托瓦兹谈及IoT --「安全在其次」
查看>>
传感器数据分析(Sensor Data Analytics)是什么?
查看>>
智能硬件开发如何选择低功耗MCU?
查看>>
阿里感悟(十)如何写好简历
查看>>
阿里感悟(十一)如何准备面试
查看>>
软件架构入门
查看>>