读Pyqt4教程,带你入门Pyqt4 _008

QCalendarWidget

QCalendarWidget 提供基于月份的日历窗口组件,它允许用户简单并且直观的选择日期。

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

# calendar.py

import sys
from PyQt4 import QtGui
from PyQt4 import QtCore


class Example(QtGui.QWidget):

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

        self.initUI()

    def initUI(self):

        self.cal = QtGui.QCalendarWidget(self)
        self.cal.setGridVisible(True)
        self.cal.move(20, 20)
        self.connect(self.cal, QtCore.SIGNAL('selectionChanged()'),
            self.showDate)


        self.label = QtGui.QLabel(self)
        date = self.cal.selectedDate()
        self.label.setText(str(date.toPyDate()))
        self.label.move(130, 260)

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

    def showDate(self):

        date = self.cal.selectedDate()
        self.label.setText(str(date.toPyDate()))


if __name__ == '__main__':

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

该例子中有一个日历窗口组件和一个标签。当前选择的日期显示在标签中。

self.cal = QtGui.QCalendarWidget(self)

构建日历窗口组件。

self.connect(self.cal, QtCore.SIGNAL('selectionChanged()'),
    self.showDate)

如果从日历上选择一个日期, selectionChanged() 信号将会发射。我们连接该方法到自定义的 showDate() 方法上。

def showDate(self):
    date = self.cal.selectedDate()
    self.label.setText(str(date.toPyDate()))

通过调用 selectedDate() 方法获得日期,然后转换日期对象到字符串并设置到标签上。

QPixmap

QPixmap 是处理图像的窗口组件之一,非常适合在屏幕上显示图像。在我们的代码示例里,我们使用 QPixmap 在窗口中显示图像。

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

# ZetCode PyQt4 tutorial
#
# In this example, we show
# an image on the window.
#
# author: Jan Bodnar
# website: zetcode.com
# last edited: December 2010


from PyQt4 import QtGui

class Example(QtGui.QWidget):

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

        self.initUI()

    def initUI(self):

        hbox = QtGui.QHBoxLayout(self)
        pixmap = QtGui.QPixmap("rotunda.jpg")

        label = QtGui.QLabel(self)
        label.setPixmap(pixmap)

        hbox.addWidget(label)
        self.setLayout(hbox)

        self.setWindowTitle("Rotunda in Skalica")
        self.move(250, 200)


def main():

    app = QtGui.QApplication([])
    exm = Example()
    exm.show()
    app.exec_()


if __name__ == '__main__':
    main()

该例子中,我们在窗口中显示图像。

pixmap = QtGui.QPixmap("rotunda.jpg")

创建一个 QPixmap 对象,用文件名作为参数。

label = QtGui.QLabel(self)
label.setPixmap(pixmap)

pixmap 放入 QLabel 窗口组件。

 

 

 

本站文章为 宝宝巴士 SD.Team 原创,转载务必在明显处注明:(作者官方网站: 宝宝巴士 
转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/4555022.html

 

 

原文地址:https://www.cnblogs.com/superdo/p/4555022.html