zoukankan      html  css  js  c++  java
  • An application icon

    The application icon is a small image which is usually displayed in the top left corner of the titlebar. In the following example we will show how we do it in PyQt4. We will also introduce some new methods.

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    """
    ZetCode PyQt4 tutorial 
    
    This example shows an icon
    in the titlebar of the window.
    
    author: Jan Bodnar
    website: zetcode.com 
    last edited: October 2011
    """
    
    import sys
    from PyQt4 import QtGui
    
    
    class Example(QtGui.QWidget):
        
        def __init__(self):
            super(Example, self).__init__()
            
            self.initUI()
            
            
        def initUI(self):
            
            self.setGeometry(300, 300, 250, 150)
            self.setWindowTitle('Icon')
            self.setWindowIcon(QtGui.QIcon('web.png'))        
        
            self.show()
            
            
    def main():
        
        app = QtGui.QApplication(sys.argv)
        ex = Example()
        sys.exit(app.exec_())
    
    
    if __name__ == '__main__':
        main()    
    

    The previous example was coded in a procedural style. Python programming language supports both procedural and object oriented programming styles. Programming in PyQt4 means programming in OOP.

    class Example(QtGui.QWidget):
        
        def __init__(self):
            super(Example, self).__init__()
            ...
    

    The three most important things in object oriented programming are classes, data, and methods. Here we create a new class called Example. The Example class inherits from QtGui.QWidget class. This means that we call two constructors: the first one for the Example class and the second one for the inherited class. The super() method returns the parent object of the Example class and we call its constructor. The __init__() method is a constructor method in Python.

    self.initUI() 
    

    The creation of the GUI is delegated to the initUI() method.

    self.setGeometry(300, 300, 250, 150)
    self.setWindowTitle('Icon')
    self.setWindowIcon(QtGui.QIcon('web.png'))  
    

    All three methods have been inherited from the QtGui.QWidget class. The setGeometry() does two things. It locates the window on the screen and sets its size. The first two parameters are the x and y positions of the window. The third is the width and the fourth is the height of the window. In fact, it combines the resize() and move() methods in one method. The last method sets the application icon. To do this, we have created a QtGui.QIcon object. The QtGui.QIcon receives the path to our icon to be displayed.

    def main():
        
        app = QtGui.QApplication(sys.argv)
        ex = Example()
        sys.exit(app.exec_())
    
    
    if __name__ == '__main__':
        main()   
    

    The startup code has been placed in the main() method. This is a Python idiom.

    IconFigure: Icon

  • 相关阅读:
    Visula Studio 2013 初始化静态浮点型数据在C++类内
    catkin_make与gtest出现冲突的问题与解决
    用Visual studio2012在Windows8上开发内核驱动监视线程创建
    用Visual studio2012在Windows8上开发内核驱动监视进程创建
    TEA加密算法的C/C++实现
    说说某游戏保护驱动中驱动黑名单的具体实现
    [Windows驱动开发](四)内存管理
    [Windows驱动开发](三)基础知识——驱动例程
    [Windows驱动开发](二)基础知识——数据结构
    [Windows驱动开发](一)序言
  • 原文地址:https://www.cnblogs.com/hushaojun/p/4434378.html
Copyright © 2011-2022 走看看