返回顶部
首页 > 资讯 > 后端开发 > Python >PyQt5--QToggleButton
  • 483
分享到

PyQt5--QToggleButton

QToggleButton 2023-01-30 22:01:11 483人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

1 # -*- coding:utf-8 -*- 2 ''' 3 Created on Sep 20, 2018 4 5 @author: SaShuangYiBing 6 7 Comment: 8 '' 9 i

 1 # -*- coding:utf-8 -*-
 2 '''
 3 Created on Sep 20, 2018
 4 
 5 @author: SaShuangYiBing
 6 
 7 Comment: 
 8 ''
 9 import sys 
10 from PyQt5.QtGui import QColor
11 from PyQt5.QtWidgets import QApplication,QWidget,QPushButton,QFrame
12 
13 class New_test(QWidget):
14     def __init__(self):
15         super().__init__()
16         self.initUI()
17     
18     def initUI(self):
19         self.color = QColor(0,0,0)
20         self.redbtn = QPushButton('Red',self)
21         self.redbtn.setCheckable(True)
22         self.redbtn.move(10,10)
23         self.redbtn.clicked[bool].connect(self.setColor)
24         
25         self.greenbtn = QPushButton('Green',self)
26         self.greenbtn.setCheckable(True)
27         self.greenbtn.move(10,60)
28         self.greenbtn.clicked[bool].connect(self.setColor)
29         
30         self.bluebtn = QPushButton('Blue',self)
31         self.bluebtn.setCheckable(True)
32         self.bluebtn.move(10,110)
33         self.bluebtn.clicked[bool].connect(self.setColor)
34         
35         self.square = QFrame(self)
36         self.square.setGeometry(150,20,100,100)
37         self.square.setStyleSheet("QWidget {background-color: %s}" %self.color.name())
38         
39         self.setGeometry(300,300,280,170)
40         self.setWindowTitle('Toggle button')
41         self.show()
42         
43     def setColor(self,pressed):
44         source = self.sender()
45         if source.text() == 'Red':
46             self.color.setGreen(0)
47             self.color.setRed(255)
48             self.color.setBlue(0)
49 
50         elif source.text() == 'Green':
51             self.color.setRed(0)
52             self.color.setGreen(255)            
53             self.color.setBlue(0)
54 
55         else:
56             self.color.setRed(0)
57             self.color.setGreen(0)
58             self.color.setBlue(255)
59         self.square.setStyleSheet("QFrame {background-color: %s}" %self.color.name())
60         
61 if __name__ == '__main__':
62     app = QApplication(sys.argv)
63     ex = New_test()
64     sys.exit(app.exec_())
65         

 

启动后:

 

点击红色按钮

 

点击绿色按钮

 

点击蓝色按钮

 

知识点说明:

 1 Qt中设置按钮或QWidget的外观是,可以使用QT Style Sheets来进行设置,非常方便。
 2 可以用setStyleSheet("font: bold; font-size:20px; color: rgb(241, 70, 62); background-color: green");来进行设置,其他的样式介绍如下:
 3 font: bold; 是否粗体显示
 4 border-image:""; 用来设定边框的背景图片。
 5 border-radius:5px; 用来设定边框的弧度。可以设定圆角的按钮
 6 border-width: 1px; 边框大小
 7 
 8 
 9 font-family:""; 来设定字体所属家族,
10 font-size:20px; 来设定字体大小
11 font-style:""; 来设定字体样式
12 font-weight:20px; 来设定字体深浅
13 background-color: green; 设置背景颜色
14 background:transparent; 设置背景为透明
15 color:rgb(241, 70, 62); 设置前景颜色
16 selection-color:rgb(241, 70, 62); 用来设定选中时候的颜色
17 
18 
19 可以使用border-top,border-right,border-bottom,border-left分别设定按钮的上下左右边框,
20 同样有border-left-color, border-left-style, border-left-width.等分别来设定他们的颜色,样式和宽度

 

--结束END--

本文标题: PyQt5--QToggleButton

本文链接: https://lsjlt.com/news/179676.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
  • PyQt5--QToggleButton
    1 # -*- coding:utf-8 -*- 2 ''' 3 Created on Sep 20, 2018 4 5 @author: SaShuangYiBing 6 7 Comment: 8 '' 9 i...
    99+
    2023-01-30
    QToggleButton
  • mac python3.9安装pyqt5、qt5、pyqt5-tools
    仅供参考,需要根据自己实际修改 !!!arm 架构建议直接使用pyqt6,不折腾pyqt5 pyqt6安装参考 PyQT6:看这一篇就够了 python PyQt6 常用操作以及常见问题解决 pych...
    99+
    2023-09-25
    qt macos linux python
  • PyQt5--MenuBar
    1 # -*- coding:utf-8 -*- 2 ''' 3 Created on Sep 13, 2018 4 5 @author: SaShuangYiBing 6 ''' 7 import sys 8 fro...
    99+
    2023-01-30
    MenuBar
  • PyQt5--GridLayoutMul
    1 # -*- coding:utf-8 -*- 2 ''' 3 Created on Sep 13, 2018 4 5 @author: SaShuangYiBing 6 ''' 7 import sys 8 fro...
    99+
    2023-01-30
    GridLayoutMul
  • PyQt5--GridLayout
    1 # -*- coding:utf-8 -*- 2 ''' 3 Created on Sep 13, 2018 4 5 @author: SaShuangYiBing 6 ''' 7 import sys 8 fro...
    99+
    2023-01-30
    GridLayout
  • PyQt5--QSplitter
    1 # -*- coding:utf-8 -*- 2 ''' 3 Created on Sep 20, 2018 4 5 @author: SaShuangYiBing 6 7 Comment: 8 ''' 9 ...
    99+
    2023-01-30
    QSplitter
  • PyQt5--QPixmap
    1 # -*- coding:utf-8 -*- 2 ''' 3 Created on Sep 20, 2018 4 5 @author: SaShuangYiBing 6 7 Comment: 8 ''' 9 ...
    99+
    2023-01-30
    QPixmap
  • PyQt5--QLineEdit
    1 # -*- coding:utf-8 -*- 2 ''' 3 Created on Sep 20, 2018 4 5 @author: SaShuangYiBing 6 7 Comment: 8 ''' 9 ...
    99+
    2023-01-30
    QLineEdit
  • PyQt5--QCalendar
    1 # -*- coding:utf-8 -*- 2 ''' 3 Created on Sep 20, 2018 4 5 @author: SaShuangYiBing 6 7 Comment: 8 ''' 9 ...
    99+
    2023-01-30
    QCalendar
  • PyQt5--QFileDiaglog
    1 # -*- coding:utf-8 -*- 2 ''' 3 Created on Sep 17, 2018 4 5 @author: SaShuangYiBing 6 7 Comment: 8 ''' 9 ...
    99+
    2023-01-30
    QFileDiaglog
  • PyQt5--QCheckBox
    1 # -*- coding:utf-8 -*- 2 ''' 3 Created on Sep 20, 2018 4 5 @author: SaShuangYiBing 6 7 Comment: 8 ''' 9 ...
    99+
    2023-01-30
    QCheckBox
  • PyQt5--QProgressBar
    1 # -*- coding:utf-8 -*- 2 ''' 3 Created on Sep 20, 2018 4 5 @author: SaShuangYiBing 6 7 Comment: 8 ''' 9 ...
    99+
    2023-01-30
    QProgressBar
  • PyQt5--QFontDiaglog
    1 # -*- coding:utf-8 -*- 2 ''' 3 Created on Sep 17, 2018 4 5 @author: SaShuangYiBing 6 7 Comment: 8 ''' 9 ...
    99+
    2023-01-30
    QFontDiaglog
  • PyQt5--QColorDiaglog
    1 # -*- coding:utf-8 -*- 2 ''' 3 Created on Sep 17, 2018 4 5 @author: SaShuangYiBing 6 7 Comment: 8 ''' 9 ...
    99+
    2023-01-30
    QColorDiaglog
  • PyQt5--QSlide
    1 # -*- coding:utf-8 -*- 2 ''' 3 Created on Sep 20, 2018 4 5 @author: SaShuangYiBing 6 7 Comment: 8 ''' 9 ...
    99+
    2023-01-30
    QSlide
  • PyQt5--TextDrag
    1 # -*- coding:utf-8 -*- 2 ''' 3 Created on Sep 21, 2018 4 5 @author: SaShuangYiBing 6 7 Comment: 8 ''' 9 ...
    99+
    2023-01-30
    TextDrag
  • PyQt5--QComboBox
    1 # -*- coding:utf-8 -*- 2 ''' 3 Created on Sep 20, 2018 4 5 @author: SaShuangYiBing 6 7 Comment: 8 ''' 9 ...
    99+
    2023-01-30
    QComboBox
  • pyQt5安装
    目录 安装设置环境变量pyCharm添加pyqt5创建UI文件 安装 ​ pip install PyQt5 -i https://pypi.douban.com/simple 使用h...
    99+
    2023-09-03
    qt python 开发语言
  • python3.6+pyQt5+QtDe
    1. python 官网下载安装python3.6并配置好环境;2.cmd下 运行:pip install PyQt5   安装PyQt库;3.cmd下运行:pip3.6 install PyQt5-tools 安装QtDesigner4...
    99+
    2023-01-31
    QtDe
  • 【Python】PyQt5入门
    文章目录 0 前言1 PyQt5及其基本模块2 开发方式3 UI界面设计(Qt Designer)4 逻辑代码的基本结构5 常用控件及其使用方法5.1 QTableView //2023.4....
    99+
    2023-10-20
    python qt pyqt5 qt designer TableView刷新数据
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作