PyQt5每天必学之切换按钮
作者:我的世界你曾经来过 时间:2023-06-14 09:07:06
切换按钮是QPushButton的特殊模式。它是一个具有两种状态的按钮:按压和未按压。我们通过这两种状态之间的切换来修改其它内容。
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
PyQt5 教程
在这个例子中,我们创建三个切换按钮。
他们将控制一个QFrame的背景颜色。
作者:我的世界你曾经来过
博客:http://blog.csdn.net/weiaitaowang
最后编辑:2016年8月3日
"""
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QFrame
from PyQt5.QtGui import QColor
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.col = QColor(0, 0, 0)
redb = QPushButton('红', self)
redb.setCheckable(True)
redb.move(10, 10)
greenb = QPushButton('绿', self)
greenb.setCheckable(True)
greenb.move(10, 60)
blueb = QPushButton('蓝', self)
blueb.setCheckable(True)
blueb.move(10, 110)
redb.clicked[bool].connect(self.setColor)
greenb.clicked[bool].connect(self.setColor)
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('切换按钮')
self.show()
def setColor(self, pressed):
source = self.sender()
if pressed:
val = 255
else:
val = 0
if source.text() == '红':
self.col.setRed(val)
elif source.text() == '绿':
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_())
在我们的例子中,我们创建了三个切换按钮和一个QWidget。我们设置QWidget的背景色为黑色。切换按钮将切换颜色值的红,绿和蓝色部分。背景颜色将取决于切换。
self.col = QColor(0, 0, 0)
初始颜色值为黑色。
redb = QPushButton('红', self)
redb.setCheckable(True)
redb.move(10, 10)
创建一个切换按钮。我们通过使用QPushButton 创建一个按钮,并设置其setCheckable()方法为真。
redb.clicked[bool].connect(self.setColor)
当我们点击切换按钮时一个信号连接到我们定义的方法。我们使用一个布尔值操作点击信号。
source = self.sender()
我们得到切换按钮的信息(也就是点击了哪个按钮)。
if source.text() == '红':
self.col.setRed(val)
如果是红色按钮,我们相应地更新颜色的红色部分。
self.square.setStyleSheet('QFrame { background-color:%s }' %
self.col.name())
我们使用样式表来改变背景颜色。
程序执行后
来源:https://blog.csdn.net/weiaitaowang/article/details/52103582
标签:PyQt5,切换按钮
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
vue中的循环对象属性和属性值用法
2024-04-28 09:25:36
![](https://img.aspxhome.com/file/2023/8/133028_0s.jpg)
Python搭建代理IP池实现获取IP的方法
2023-01-05 05:16:35
![](https://img.aspxhome.com/file/2023/5/105615_0s.png)
基于jQuery的自动完成插件
2011-02-05 10:55:00
Python requests模块用法详解
2022-02-09 23:00:13
Python基于callable函数检测对象是否可被调用
2022-01-07 20:12:38
Python OpenCV图像模糊处理介绍
2023-05-20 14:20:17
![](https://img.aspxhome.com/file/2023/5/75135_0s.png)
django admin后台添加导出excel功能示例代码
2023-10-19 10:43:48
![](https://img.aspxhome.com/file/2023/9/62309_0s.png)
PyQt5入门之基于QListWidget版本实现图片缩略图列表功能
2023-07-15 08:32:52
![](https://img.aspxhome.com/file/2023/3/70123_0s.jpg)
matplotlib绘制多子图共享鼠标光标的方法示例
2023-06-15 13:45:19
![](https://img.aspxhome.com/file/2023/7/61227_0s.png)
利用box-sizing实现div仿框架
2009-12-08 15:45:00
![](https://img.aspxhome.com/file/UploadPic/200912/8/01-32s.gif)
pyqt5移动鼠标显示坐标的方法
2023-06-09 22:37:30
Python办公自动化Word转Excel文件批量处理
2022-06-24 21:02:23
![](https://img.aspxhome.com/file/2023/0/97410_0s.jpg)
pytorch-gpu安装的经验与教训
2022-01-11 20:23:36
![](https://img.aspxhome.com/file/2023/5/102805_0s.jpg)
jQuery中each方法的使用详解
2024-04-09 19:47:22
超大数据量存储常用数据库分表分库算法总结
2024-01-28 01:44:38
CSS资料中常见英语词语翻译整理
2008-08-29 12:50:00
CentOS7.2安装MySql5.7并开启远程连接授权的教程
2024-01-16 15:40:58
![](https://img.aspxhome.com/file/2023/6/75066_0s.png)
python3中str(字符串)的使用教程
2021-07-19 00:28:47
Ubuntu16.04/树莓派Python3+opencv配置教程(分享)
2023-03-30 12:38:59
JavaScript中的this指针用法
2007-08-26 17:29:00