PySide学习笔记第十章-窗体小部件(widget)-上篇

PySide (QT)有很多小部件,例如按钮,复选框,滑动条,列表框,进度条,日历

一、QtGui.QCheckBox 有两种状态 on / off ,通过切换状态,来达到一种效果,该示例通过☑该框 来取消显示Title

# -*- coding: utf-8 -*-

"""
ZetCode PySide 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: August 2011
"""

import sys
from PySide 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) # 将 复选框状态的改变信号和changeTitle函数槽绑定


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

    def changeTitle(self, state): # 定义槽函数,将复选框的状态作为参数

        if state == QtCore.Qt.Checked:
            self.setWindowTitle('Checkbox')
        else:
            self.setWindowTitle('')

def main():

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


if __name__ == '__main__':
    main()
二、触发器按钮

PySide没有触发器按钮,为了创建一个触发器按钮,我们在一个特定的模式下,用一个QtGui.QPushButton来实现触发器按钮 ,触发器按钮是一个有两种状态,按压住或者没按压住, 通过点击在状态之间切换,在很多情况下,这种触发器都挺好用的

# -*- coding: utf-8 -*-

"""
ZetCode PySide tutorial

In this example, we create three toggle buttons.
They will control the background color of a
QtGui.QFrame.

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

import sys
from PySide import QtGui, QtCore

class Example(QtGui.QWidget):

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

        self.initUI()

    def initUI(self):

        self.col = QtGui.QColor(0, 0, 0) # 为了创建触发器按钮,我们调用QtGui.QPushButton创建按钮,并且保调用setCheckable保证该按钮是可以选定的

        redb = QtGui.QPushButton('Red', self) # 创建3个按钮,分别设置好其位置,以及将按钮的点击信号绑定到setColor槽上
        redb.setCheckable(True)
        redb.move(10, 10)

        redb.clicked[bool].connect(self.setColor) # 将点击的信号(bool只是说明该信号类型是bool)绑定到self.setColor函数槽上

        greenb = QtGui.QPushButton('Green', self)
        greenb.setCheckable(True)
        greenb.move(10, 60)

        greenb.clicked[bool].connect(self.setColor)

        blueb = QtGui.QPushButton('Blue', self)
        blueb.setCheckable(True)
        blueb.move(10, 110)

        blueb.clicked[bool].connect(self.setColor)

        self.square = QtGui.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('Toggle button')
        self.show()

    def setColor(self, pressed):

        source = self.sender() # 虽然有3个按钮,但是一旦有新的信号都会更新到这里
        # print source

        if pressed:
            val = 255
        else: val = 0

        if source.text() == "Red":
            self.col.setRed(val)
        elif source.text() == "Green":
            self.col.setGreen(val)
        else:
            self.col.setBlue(val)

        self.square.setStyleSheet("QFrame { background-color: %s }" % # 更新颜色
            self.col.name())

def main():

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


if __name__ == '__main__':
    main()
三、QtGui.QSlider 滑动条

# -*- coding: utf-8 -*-

"""
ZetCode PySide tutorial

This example shows a QtGui.QSlider widget.

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

import sys
from PySide import QtGui, QtCore

class Example(QtGui.QWidget):

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

        self.initUI()

    def initUI(self):

        sld = QtGui.QSlider(QtCore.Qt.Horizontal, self)
        sld.setFocusPolicy(QtCore.Qt.NoFocus)
        sld.setGeometry(30, 40, 100, 30)
        sld.valueChanged[int].connect(self.changeValue)

        self.label = QtGui.QLabel(self)
        self.label.setPixmap(QtGui.QPixmap('mute.png'))
        self.label.setGeometry(160, 40, 80, 30)

        self.setGeometry(300, 300, 280, 170)
        self.setWindowTitle('QtGui.QSlider')
        self.show()

    def changeValue(self, value):

        if value == 0:
            self.label.setPixmap(QtGui.QPixmap('mute.png'))
        elif value > 0 and value <= 30:
            self.label.setPixmap(QtGui.QPixmap('min.png'))
        elif value > 30 and value < 80:
            self.label.setPixmap(QtGui.QPixmap('med.png'))
        else:
            self.label.setPixmap(QtGui.QPixmap('max.png'))

def main():

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


if __name__ == '__main__':
    main()

四、进度条在PySide工具箱中,有水平进度条和竖直进度条之分,进度条需要设定区间,默认为0-99

# -*- coding: utf-8 -*-

"""
ZetCode PySide tutorial

This example shows a QtGui.QProgressBar widget.

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

import sys
from PySide import QtGui, QtCore

class Example(QtGui.QWidget):

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

        self.initUI()

    def initUI(self):

        self.pbar = QtGui.QProgressBar(self) # 构造一个进度条对象
        self.pbar.setGeometry(30, 40, 200, 25)

        self.btn = QtGui.QPushButton('Start', self)
        self.btn.move(40, 80)
        self.btn.clicked.connect(self.doAction)

        self.timer = QtCore.QBasicTimer() # 为了激活进度条,我们构造一个计时器对象
        self.step = 0

        self.setGeometry(300, 300, 280, 170)
        self.setWindowTitle('QtGui.QProgressBar')
        self.show()

    def timerEvent(self, e): # QtGui.QWidget继承并重写timerEvent,用于按时间更新进度条

        if self.step >= 100:
            self.timer.stop()
            self.btn.setText('Finished')
            return
        self.step = self.step + 1
        self.pbar.setValue(self.step)

    def doAction(self): # 定义槽函数,用来终止和启动计时器

        if self.timer.isActive():
            self.timer.stop()
            self.btn.setText('Start')
        else:
            self.timer.start(100, self) # 用start()函数触发计时器,参数分别为超时时间和调用该函数的对象
            self.btn.setText('Stop')

def main():

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


if __name__ == '__main__':
    main()

五、日历 QtGui.QCalendarWidget

该示例包含一个日历和label,你在日历中选定的日期会直接显示在lable中

# -*- coding: utf-8 -*-

"""
ZetCode PySide tutorial

This example shows a QtGui.QCalendarWidget widget.

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

import sys
from PySide import QtGui, QtCore

class Example(QtGui.QWidget):

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

        self.initUI()

    def initUI(self):

        cal = QtGui.QCalendarWidget(self) # 构造日历对象
        cal.setGridVisible(True)
        cal.move(20, 20)
        cal.clicked[QtCore.QDate].connect(self.showDate) # 将选定日期的信号和显示该日期的函数槽联系起来

        self.lbl = QtGui.QLabel(self) # 构造一个label
        date = cal.selectedDate() # 用selectedDate()函数来拿到日期,然后转换为字符串并显示在lable上 

        self.lbl.setText(date.toString())
        self.lbl.move(130, 260)0

        self.setGeometry(300, 300, 350, 300)
        self.setWindowTitle('Calendar')
        self.show()

    def showDate(self, date): # 构造槽函数,以用本窗口中的lable显示时间
        self.lbl.setText(date.toString())

def main():

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


if __name__ == '__main__':
    main()

你可能感兴趣的:(python,code)