Python Qt GUI设计:QSlider滑动条类(基础篇—16)

InfoQ 2021-11-25 18:25:22
Python Qt 设计 GUI qslider

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"QSlider控件提供了一个垂直或水平的滑动条,滑动条是一个用于控制有界值的典型控件,它允许用户沿水平或垂直方向在某一范围内移动滑块,并将滑块所在的位置转换成一个合法范围内的整数值。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有时候这种方式比输入数字或者使用","attrs":{}},{"type":"link","attrs":{"href":"https://handsome-man.blog.csdn.net/article/details/120681665","title":null,"type":null},"content":[{"type":"text","text":"SpinBox(计数器)","attrs":{}}]},{"type":"text","text":"更加自然,只需要在槽函数中对滑块所在位置的处理相当于从整数之间的最小值和最高值进行取值。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"QSlider类中的常用方法如下表所示:","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/20/20f7c3eb4c41c974077c6783030cd8ef.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"QSlider类中的常用信号如下表所示:","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/c2/c280f536fd2e21a497a6954db49245d2.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一个滑块条控件可以以水平或垂直的方式显示,在构造函数中进行设置如下所示:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"self.sp=QSlider(Qt.Horizontal) \nself.sp=QSlider(Qt.Vertical)","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"来看看QSlider滑动条类的示例,效果如下所示:","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f7/f7db43f35f22b2f0811b58376427d75b.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这里示例中,在PyQt 5的窗口中使用QSlider滑动条控件,随着滑动条的移动,标签的字号大小也随着发生变化。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"将一个标签和一个水平滑动条放置在一个垂直布局管理器中。将滑块的valueChanged信号连接到valuechange()函数。槽函数valuechange()读取滑块的当前值,并将其作为字号的大小。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"使用setTickInterval(5)设置刻度间隔后,在绘制刻度时就会有9个刻度点(从10开始,每隔步长5绘制一个点,到50处结束),也就是说,刻度点个数=(最大值-最小值刻度间隔+1,本例中的刻度点个数为:(50-10)/5 +1=9。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"实现代码如下所示:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":"text"},"content":[{"type":"text","text":"import sys\nfrom PyQt5.QtCore import *\nfrom PyQt5.QtGui import *\nfrom PyQt5.QtWidgets import *\n \nclass SliderDemo(QWidget):\n\tdef __init__(self, parent=None):\n\t\tsuper(SliderDemo, self).__init__(parent)\n\t\tself.setWindowTitle(\"QSlider 例子\") \n\t\tself.resize(300, 100)\n \n\t\tlayout = QVBoxLayout()\n\t\tself.l1 = QLabel(\"Hello PyQt5\")\n\t\tself.l1.setAlignment(Qt.AlignCenter)\n\t\tlayout.addWidget(self.l1)\n # 水平方向\n\t\tself.sl = QSlider(Qt.Horizontal)\n #设置最小值\n\t\tself.sl.setMinimum(10)\n\t\t#设置最大值\n\t\tself.sl.setMaximum(50)\n\t\t# 步长\n\t\tself.sl.setSingleStep( 3 ) \n\t\t# 设置当前值\n\t\tself.sl.setValue(20)\n\t\t# 刻度位置,刻度在下方\n\t\tself.sl.setTickPosition(QSlider.TicksBelow)\n # 设置刻度间隔\n\t\tself.sl.setTickInterval(5)\n\t\tlayout.addWidget(self.sl)\n # 连接信号槽\n\t\tself.sl.valueChanged.connect(self.valuechange)\n\t\tself.setLayout(layout)\n \n\tdef valuechange(self):\n\t\tprint('current slider value=%s' % self.sl.value() )\n\t\tsize = self.sl.value()\n\t\tself.l1.setFont(QFont(\"Arial\",size))\n \nif __name__ == '__main__':\n\tapp = QApplication(sys.argv)\n\tdemo = SliderDemo()\n\tdemo.show()\n\tsys.exit(app.exec_())","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}
版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://xie.infoq.cn/article/12aad7c7a2f7615e103be075f

  1. [learning notes] Python - pyecarts
  2. Python automated operation and maintenance -- actual combat (I)
  3. Python 多分支语句的三种结构
  4. Python 输出指定范围的闰年
  5. Fatal Python error: init_ fs_ encoding: failed to get the Python codec of the filesystem encoding
  6. Using Python to obtain a video address is as simple as
  7. #yyds干货盘点# 6. Python 元组,不可变的列表,滚雪球学 Python
  8. Yyds Dry Inventory # 6. Python tuples, invariant List, snowball Learning Python
  9. You cannot enter the registration page using Django registration redux
  10. 笨办法学Python第十八天:更多文件操作
  11. Python编辑代码,帮个忙呗
  12. pandas转换object为int失败了,有人能解答吗
  13. python解释题,解释这串,所代表的意思
  14. 用python用while语句写1000以内能被5和9整除的数和个数
  15. #python 我写了一个“饭店”程序
  16. Comprehensive application of Python foundation -- Development
  17. Écrivez en python le nombre et le nombre d'entiers qui peuvent être divisés par 5 et 9 jusqu'à 1000 dans une instruction while
  18. python3.7 FileNotFoundError: [WinError 2] 系统找不到指定的文件。
  19. 最全面的Python重点知识汇总
  20. You can learn Python articles without reading online classes (day 4)
  21. You can learn Python articles without reading online classes (the third day)
  22. python中 我想爬取一个网页,我在cmd中pip list中已经把requests 以及bs4 弄好了 但是他还是提示我没有那个模块
  23. You can learn Python articles without reading online classes (the next day)
  24. You can learn Python articles without reading online classes (the first day)
  25. 在Python和Go项目之后使用Rust的经验分享 - scaleway
  26. 使用PyO3从Python调用 Rust:加速Python
  27. Python 3.7 filenotfounderror: [winerror 2] le système n'a pas pu trouver le fichier spécifié.
  28. Python 一切皆对象
  29. 【Pandas学习笔记01】强大的分析结构化数据的工具集
  30. 求各位亲们可以解答一下嘛,为什么跑不出来呀,大学Python
  31. 【Pandas学习笔记01】强大的分析结构化数据的工具集
  32. python飞机大战如何让飞机始终在界面中,不能跑出界面
  33. Python代码阅读(第63篇):数字奇偶性
  34. python建立⼀個圖形介⾯應⽤(GUI app)
  35. python接口自动化的html格式报告以邮件形式发送展示的原文很丑怎么处理
  36. 针对初学者,我建议你学这 3 个 Python AutoEDA 工具包
  37. #yyds干货盘点#Python-爬虫_urlib一个类型和六个方法
  38. Pour les débutants, je vous recommande d'apprendre ces 3 kits autoeda Python
  39. 我想问python怎么入门嘞?求各路人士
  40. Summary of today's Django lessons: data reverse query triggers error field 'score_ jieshoufankui‘ expected a number but got ‘‘-20211109
  41. 关于#python_while循环的写法#的问题,如何解决?
  42. Python异常处理中异常的种类有哪些?你知道几个?
  43. Python异常處理中异常的種類有哪些?你知道幾個?
  44. Quels sont les types d'exceptions dans la gestion des exceptions python? Combien en savez - vous?
  45. À propos de # Python Comment résoudre le problème de l'écriture de la Boucle while?
  46. Python如何操作system.data.sqlite数据库
  47. python数字游戏,让你欲罢不能
  48. Python中的可迭代对象、迭代器、For循环工作机制、生成器
  49. 一个从没接触过编程的人,如何自学进入Python行业?
  50. 一份超级实用的 Python ”技巧“清单
  51. Python 程序员给上路初学者的3点忠告
  52. 3 conseils pour les débutants sur la route par les programmeurs Python
  53. Une liste de conseils Python super pratiques
  54. Comment quelqu'un qui n'a jamais été en contact avec la programmation peut - il apprendre à entrer dans l'industrie python?
  55. Objets itérables, itérateurs, pour le mécanisme de travail circulaire, générateurs en python
  56. Les Jeux de chiffres Python vous font vous arrêter
  57. Comment Python fonctionne avec la base de données system.data.sqlite
  58. Python之html与markdown互相转换
  59. Python之html與markdown互相轉換
  60. Conversion HTML et markdown de Python