The application icon is a small image which are usually displayed in the top left corner of the titlebar. In the following example we'll show how we did it in PYQT4. We'll also introduce some new methods.
#!/usr/bin/python#-*-coding:utf-8-*-"" "Zetcode PyQt4 Tutorial This example shows an Iconin the titlebar of the window. Author:jan Bodnarwebsite:zetcode.com last Edited:october "" "Import sysfrom PyQt4 import Qtguiclass Example (QTGUI.QW Idget): def __init__ (self): super (Example, self). __init__ () Self.initui () def initui (self): self.setwindowtitle (' Icon ') self.setwindowicon (' Self.setgeometry ') ( 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 is 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 is classes, data, and methods. Here we create a new class called Example . The Example class inherits from QtGui.QWidget class. This means, we call, Constructors:the first one for the class and the second one for the Example 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.
The creation of the GUI is delegated to the initUI() method.
Self.setgeometry (Self.setwindowtitle (' Icon ') Self.setwindowicon (Qtgui.qicon (' web.png '))
All three methods has been inherited from the QtGui.QWidget class. The setGeometry() does and the things. It locates the window on the screens and sets its size. The first and the parameters are the X and Y positions of the window. The third is the width and the fourth are the height of the window. In fact, it combines the and methods in one resize() move() method. The last method sets the application icon. To does this, we have created a QtGui.QIcon object. The receives the path to our icon to be QtGui.QIcon 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.
Figure:icon
An application icon