PyQt4程序图标

程序图标就是一个小图片,通常显示在程序图标的左上角(ubuntu gnome在最上侧)。

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

import sys
from PyQt4 import QtGui

class Icon(QtGui.QWidget):
    def __init__(self, parent = None):
        QtGui.QWidget.__init__(self, parent)
        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('moonlight poet icon')
        self.setWindowIcon(QtGui.QIcon('ubuntu.png'))

app = QtGui.QApplication(sys.argv)
icon = Icon()
icon.show()
sys.exit(app.exec_())

效果:

(注:那个ubuntu gnome的图标就是程序图标)

class Icon(QtGui.QWidget):
    def __init__(self, parent = None):
        QtGui.QWidget.__init__(self, parent)

我们创建了一个名为Icon的新类,该类继承自QtGui.QWidget类。因此我们必须调用两个构造函数——Icon的构造函数和继承类QtGui。QWidget类的构造函数。

        self.setGeometry(300, 300, 250, 150)
        self.setWindowTitle('moonlight poet icon')
        self.setWindowIcon(QtGui.QIcon('ubuntu.png'))

setGeometry()方法完成两个功能——设置窗口在屏幕上的位置和设置窗口本身的大小。他的前两个参数是窗口在屏幕上的x和y坐标,后两个参数是窗口本身的宽和高。setWindowIcon()方法用来设置程序图标,它需要一个QIcon类型的对象作为参数。调用QIcon构造函数时,我们需要提供要显示的图标的路径(绝对路径或相对路径)。

posted @ 2016-03-27 20:26  月光诗人  阅读(1101)  评论(0编辑  收藏  举报