QtGui.QCheckBox is a widget that has two states: on and off. It is a box with a label. Check boxes are typically used to represent features in an application that can be enabled or disabled.

  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. """
  5. ZetCode PyQt4 tutorial
  6.  
  7. In this example, a QtGui.QCheckBox widget
  8. is used to toggle the title of a window.
  9.  
  10. author: Jan Bodnar
  11. website: zetcode.com
  12. last edited: September 2011
  13. """
  14.  
  15. import sys
  16. from PyQt4 import QtGui, QtCore
  17.  
  18. class Example(QtGui.QWidget):
  19.  
  20. def __init__(self):
  21. super(Example, self).__init__()
  22.  
  23. self.initUI()
  24.  
  25. def initUI(self):
  26.  
  27. cb = QtGui.QCheckBox('Show title', self)
  28. cb.move(20, 20)
  29. cb.toggle()
  30. cb.stateChanged.connect(self.changeTitle)
  31.  
  32. self.setGeometry(300, 300, 250, 150)
  33. self.setWindowTitle('QtGui.QCheckBox')
  34. self.show()
  35.  
  36. def changeTitle(self, state):
  37.  
  38. if state == QtCore.Qt.Checked:
  39. self.setWindowTitle('QtGui.QCheckBox')
  40. else:
  41. self.setWindowTitle('')
  42.  
  43. def main():
  44.  
  45. app = QtGui.QApplication(sys.argv)
  46. ex = Example()
  47. sys.exit(app.exec_())
  48.  
  49. if __name__ == '__main__':
  50. main()

In our example, we will create a checkbox that will toggle the window title.

  1. cb = QtGui.QCheckBox('Show title', self)

This is a QtGui.QCheckBox constructor.

  1. cb.toggle()

We have set the window title, so we must also check the checkbox. By default, the window title is not set and the checkbox is unchecked.

  1. cb.stateChanged.connect(self.changeTitle)

We connect the user defined changeTitle() method to the stateChanged signal. The changeTitle()method will toggle the window title.

  1. def changeTitle(self, state):
  2.  
  3. if state == QtCore.Qt.Checked:
  4. self.setWindowTitle('QtGui.QCheckBox')
  5. else:
  6. self.setWindowTitle('')

The state of the widget is given to the changeTitle() method in the state variable. If the widget is checked, we set a title of the window. Otherwise, we set an empty string to the titlebar.

Figure: QtGui.QCheckBox

QtGui.QCheckBox的更多相关文章

  1. PyQt4单选框QCheckBox

    PyQt4中的部件 部件是构建应用程序的基础元素.PyQt4工具包拥有大量的种类繁多的部件.比如:按钮,单选框,滑块,列表框等任何程序员在完成其工作时需要的部件. QCheckBox单选框 单选框具有 ...

  2. Pyqt 获取打包二进制文件中的资源

    记得有一次打开一个单独exe程序,点击btn中的一个帮助说明按钮,在同级目录下就多出一个help.chm 文件并自动打开. 那这个exe肯定是把help.chm 打包到exe中,当我触发“帮助”按钮的 ...

  3. Pyqt 屏幕截图工具

    从Pyqt的examples中看到一段截图代码, (路径:examplas\desktop\screenshot.py) 所以想自己UI下界面,手动练习下 通过UI生成的: Screenshot.py ...

  4. Pyqt Smtplib实现Qthread多线程发送邮件

    一. smtplib 的介绍 smtplib.SMTP([host[, port[, local_hostname[, timeout]]]])   SMTP类构造函数,表示与SMTP服务器之间的连接 ...

  5. Pyqt QSystemTrayIcon 实现托盘效果

    pyqt的托盘效果很好实现,在Pyqt的demo中有个例子 路径:PyQt4\examples\desktop\systray.py 今天我就仿这个Tray效果做效果 一. 创建UI trayicon ...

  6. pyqt小例子 音乐盒

    源代码1: # -*- coding: utf-8 -*- import sys,time,os import ctypes from PyQt4 import QtCore, QtGui,Qt fr ...

  7. PYQT4 : QSystemTrayIcon练习

    照着demo自己做了一遍,练练手 import sys from PyQt4 import QtGui from PyQt4 import QtCore class SysTray(QtGui.QDi ...

  8. Python&MySQL&PyQt

    环境: Python2.7+MySQL5.6+PyQt4 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/40 ...

  9. PyQt4 的部件 -- CheckBox 单选框

    单选框具有两种状态:被选中或未被选中. 当用户选择或者取消选择时,单选框就会发射一个stateChanged()信号 # QCheckBox 单选框 # 本例创建一个用来改变窗口标题的单选框 impo ...

随机推荐

  1. android bitmap recycle

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 不需要位图的时候,就可以手动调用recycle. 2.3之前 位图对象 存在 java的 ...

  2. manacher算法求最长回文子序列

    一:背景 给定一个字符串,求出其最长回文子串.例如: s="abcd",最长回文长度为 1: s="ababa",最长回文长度为 5: s="abcc ...

  3. [BZOJ4009][HNOI2015]接水果(整体二分)

    [HNOI2015]接水果 时间限制:60s      空间限制:512MB 题目描述 风见幽香非常喜欢玩一个叫做 osu!的游戏,其中她最喜欢玩的模式就是接水果. 由于她已经DT FC 了The b ...

  4. 【树形DP】BZOJ1040-[ZJOI2008]骑士

    [题目大意] 有n个骑士,给出他们的能力值和最痛恨的一位骑士.选出一个骑士军团,使得军团内没有矛盾的两人(不存在一个骑士与他最痛恨的人一同被选入骑士军团的情况),并且,使得这支骑士军团最具有战斗力,求 ...

  5. 更换mac电脑后证书过期的解决办法

    http://stackoverflow.com/questions/32821189/xcode-7-error-missing-ios-distribution-signing-identity- ...

  6. jsp和servlet有哪些相同点和不同点,它们之间的联系是什么?

    1.jsp经编译后就变成了servlet(jsp本质就是servlet,jvm只能识别java的类,不能识别jsp代码,web容器将jsp的代码编译成jvm能够识别的java类) 2.jsp更擅长表现 ...

  7. bzoj2200道路和航线

    试题描述 Farmer John 正在一个新的销售区域对他的牛奶销售方案进行调查.他想把牛奶送到 T (1≤T≤2.5×10^4)个城镇 ,编号为 1 到 T.这些城镇之间通过 R 条道路(编号为 1 ...

  8. yii/helper/Html

    1.生成标签: <?=Html::tag('标签',‘标签中的内容’,[‘标签属性’])?> 举例: <?=Html::tag('p','HelloWorld',['id'=> ...

  9. ExtJS中实现嵌套表格

    先看效果: 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...

  10. SQL Server需要监控哪些计数器 ---指尖流淌

    http://www.cnblogs.com/zhijianliutang/p/4174697.html