QtGui.QCheckBox

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.

#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
ZetCode PyQt4 tutorial 

In this example, a QtGui.QCheckBox widget
is used to toggle the title of a window.

author: Jan Bodnar
website: zetcode.com
last edited: September 2011
"""

import sys
from PyQt4 import QtGui, QtCore

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):      

        cb = QtGui.QCheckBox(‘Show title‘, self)
        cb.move(20, 20)
        cb.toggle()
        cb.stateChanged.connect(self.changeTitle)

        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle(‘QtGui.QCheckBox‘)
        self.show()

    def changeTitle(self, state):

        if state == QtCore.Qt.Checked:
            self.setWindowTitle(‘QtGui.QCheckBox‘)
        else:
            self.setWindowTitle(‘‘)

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

if __name__ == ‘__main__‘:
    main()

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

cb = QtGui.QCheckBox(‘Show title‘, self)

This is a QtGui.QCheckBox constructor.

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.

cb.stateChanged.connect(self.changeTitle)

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

def changeTitle(self, state):

    if state == QtCore.Qt.Checked:
        self.setWindowTitle(‘QtGui.QCheckBox‘)
    else:
        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

时间: 2025-02-01 08:44:23

QtGui.QCheckBox的相关文章

PyQt4单选框QCheckBox

PyQt4中的部件 部件是构建应用程序的基础元素.PyQt4工具包拥有大量的种类繁多的部件.比如:按钮,单选框,滑块,列表框等任何程序员在完成其工作时需要的部件. QCheckBox单选框 单选框具有两个状态:被选中或未被选中.它看起来像一个附加了附件的标签.当用户选择或取消选择时,单选框就会发送一个stateChanged()信号. #!/usr/bin/python # -*- coding: utf-8 -*- import sys from PyQt4 import QtGui, QtC

ZetCode PyQt4 tutorial widgets I

#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, a QtGui.QCheckBox widget is used to toggle the title of a window. author: Jan Bodnar website: zetcode.com last edited: September 2011 """ i

python临时文件之下载

# -*- coding: utf-8 -*- # python:2.x __author__ = 'Administrator' #自定义模块导入 from down import Ui_Form as Ui #导入内置/第三方模块 from PyQt4.QtCore import * from PyQt4.QtGui import * from PyQt4.Qt import * import datetime,time,sys,os import urllib,urllib2 import

sy【QTableWidget】【操作单元格控件】

from PyQt4 import QtGui,QtCore class Test(QtGui.QWidget): def __init__(self,items,parent=None): self.app=QtGui.QApplication([]) super(Test,self).__init__(parent) self.setGeometry(200,100,200,300) self.tw=QtGui.QTableWidget(len(items),1,self) self.tw.

关于Dialog----玲琅满目,任君挑选

下面来记录一下Qt中的形形色色的Dialg.... 直接给出PySide自带的example,因为我觉得我的改写也只是“依葫芦画瓢”...放心,Qt是开源的,真伟大.. #!/usr/bin/env python """PyQt4 port of the dialogs/standarddialogs example from Qt v4.x""" # This is only needed for Python v2 but is harmle

pyqt练习x5.0计算器

from __future__ import division # -*- coding: utf-8 -*- import sys,os,decimal,tempfile,math,time from PyQt4 import QtGui,QtCore,Qt class Jsj(QtGui.QWidget): #print'正在加入命令中' def __init__(self,parent=None): QtGui.QWidget.__init__(self) #创建窗口界面 self.wid

Pyqt 屏幕截图工具

从Pyqt的examples中看到一段截图代码, (路径:examplas\desktop\screenshot.py) 所以想自己UI下界面,手动练习下 通过UI生成的: Screenshot.py 1 # -*- coding: utf-8 -*- 2 3 # Form implementation generated from reading ui file 'Screenshot.ui' 4 # 5 # Created: Mon Mar 30 11:41:46 2015 6 # by:

Pyqt QSystemTrayIcon 实现托盘效果

pyqt的托盘效果很好实现,在Pyqt的demo中有个例子 路径:PyQt4\examples\desktop\systray.py 今天我就仿这个Tray效果做效果 一. 创建UI trayicon.ui文件: 1 <?xml version="1.0" encoding="UTF-8"?> 2 <ui version="4.0"> 3 <class>TrayIcon</class> 4 <

学习pyqt 基础 实践1

学习pyqt 基础 实践1 代码加截图 学习前面一点Qwidget,QWindow 然后实现了这个界面,并没有相应的处理方法,之后在写相应的处理函数. #! /usr/bin/python # -*- coding:utf8 -*- import sys from PyQt4 import QtCore, QtGui from PyQt4.QtCore import * from PyQt4.QtGui import * class MainWindow(QtGui.QWidget): def