PYQT4 is a toolkit for writing graphical interface programs (GUI applications). PYQT4 is used as a Python module, it has 440 classes and more than 6000 functions and methods. It is also a cross-platform toolkit that can run on almost all major operating systems, such as the Unix,windows,mac OS.
The PyQt4 class library can be divided into the following modules:
- Qtcore
- Qtgui
- Qtnetwork
- Qtxml
- Qtsvg
- Qtopengl
- Qtsql
Qtcore contains the core part of the PYQT non-GUI function module, which is used to process time, files and directories, different data types, streams, URLs, media types of resources, threads, and processes.
Qtgui contains graphics-related components and class libraries, including buttons (button), Windows (window), Status bar, toolbar (toolbar), Sliders (slider), bitmaps (bitmap), color (colors), and fonts (font And so on "the English language of these nouns is often used in our programming."
The qtnetwork contains network programming related modules. These class libraries help with TCP/IP programming and Client & Server-side UDP programming, making network programming easier and lighter.
The qtxml contains the class library that processes the XML file. This module provides an implementation of the SAM and Dom interfaces.
Qtsvg provides a class library that displays SVG files. SVG, the full name Scalable Vector graphics, can be scaled vector graphic, is an XML-based description of two-dimensional graphics and image application file format.
Qtopengl is a module that uses OpenGL libraries to render 2D, 3D images. It allows the QT GUI library and OpenGL library to seamlessly engage in "great looks".
Finally, the Qtsql module provides a class library for working with the database.
other GUI frameworks
Python programmers who write GUI programs can choose between these three frameworks: Pyqt,pygtk and Wxpython.
Example: Creating a blank window
1 ImportSYS2 fromPyQt4ImportQtgui3 4 defMain ():5app=qtgui.qapplication (SYS.ARGV)6w=Qtgui.qwidget ()7W.resize (250,150)8W.move (300,300)9W.setwindowtitle (' Simple')Ten w.show () One Sys.exit (App.exec_ ()) A - if __name__=='__main__': -Main ()
W = Qtgui.qwidget ()
Qtgui.qwidget is the underlying class library in the PYQT4 all user interface objects. Here we call the default constructor for Qtgui.qwidget, which has no parent object. We call a widget (widget) without a parent object (window).
Example: Buttons, control events, control hints, form display to the middle of the screen, MessageBox
ImportSYS fromPyQt4ImportQtgui,qtcoreclassWinForm (qtgui.qwidget):def __init__(self): Super (winform,self).__init__() Self.initui ()defInitui (self): QtGui.QToolTip.setFont (Qtgui.qfont ('Sansserif', 10)) Self.settooltip ('This is a <b>QWidget</b> widget') btn=qtgui.qpushbutton ('Button', self) btn.settooltip ('This is a <b>QPushButton</b> widget') Btn.resize (Btn.sizehint ()) Btn.move (50,50) btn1=qtgui.qpushbutton ('Quit', self) btn1.clicked.connect (QtCore.QCoreApplication.instance (). Quit) Btn1.resize (Btn1.sizehint ()) Btn1.move (150,50) Self.setgeometry (300,300,250,150) Self.setwindowtitle ('ToolTips')
Self.setwindowicon (Qtgui.qicon ()) Self.center () self.show ()defcloseevent (self,event): Res=qtgui.qmessagebox.question (Self,'Info', "are you sure you want to quit? ", QtGui.QMessageBox.Yes |QtGui.QMessageBox.No, QTGUI.QMESSAGEBOX.N O)ifres==QtGui.QMessageBox.Yes:event.accept ()Else: Event.ignore ()defCenter (self): QR=Self.framegeometry () CP=qtgui.qdesktopwidget (). Availablegeometry (). Center () qr.movecenter (CP) Self.move (Qr.topleft ()) defMain (): App=qtgui.qapplication (SYS.ARGV) ex=winForm () sys.exit (App.exec_ ())if __name__=='__main__': Main ()
Here is the effect of the program, the control hints are shown in turn, the MessageBox pop-up window
The above code is explained below:
class WinForm (qtgui.qwidget): def __init__ (self): super (winform,self). __init__ ()
Here we create a new class called WinForm, and the qtgui.qwidget in parentheses indicate that the example class inherits from the Qtgui.qwidget class. This means that we need to call the constructor of the parent class when we write the constructor for the new class. Super (Example, self) Returns the parent object of Example (that is, qtgui.qwidget), and then we call the parent object's constructor. Note that __init__ this is a constructor in Python.
Note: You must call the constructor of the parent class, or a run error will occur:
Runtimeerror:super-class __init__ () of type WinForm was never called
QtGui.QToolTip.setFont (Qtgui.qfont ('sansserif', ten)) Self.settooltip ( ' This is a <b>QWidget</b> widget ' ) btn=qtgui.qpushbutton ('Button', self) btn.settooltip ( 'This isa <b>QPushButton</b> widget')
We call SetToolTip () This method, and we can also use HTML tags! It's unbelievable. I suddenly want to be free to try to add div tags, merry tags and hyperlink tags to try.
btn.resize (Btn.sizehint ()) Btn.move (50,50)
We set the size and position of the button. where the Sizehint () method returns a recommended size
Btn1=qtgui.qpushbutton ('Quit', self) btn1.clicked.connect ( QtCore.QCoreApplication.instance (). Quit) btn1.resize (Btn1.sizehint ()) Btn1.move (150,50)
This code is called Quit button, it is a push button, click on the program to exit.
The constructor prototype of the Qtgui.qpushbutton that we want to use in the example is this:
Qpushbutton (string text, qwidget parent = None)
Where the text parameter is the text displayed on the button. The parent parameter is a part of the parents object, and here is where we want to put the button on what, in this case a qtgui.qwidget "is actually a window"
Here we create a button (push button), which is an instance of the Qtgui.qpushbutton class. The first argument is the word ' Quit ' on the button, the second argument is the parent object, and here is the WinForm we created, self, which inherits from Qtgui. Qwidget Class "Example no parent object, is a window, remember?"
The event processing system in PYQT4 is implemented by the signal slot mechanism (signals and slots). If we click on this button, the "clicked" Signal will be issued. Qtcore.qcoreapplication This thing contains the main loop of the program, which handles and dispatches all events, while the instance () method returns the current instance (insatnce). Notice that Qtcore.qcoreapplication is created with the creation of qtgui.qapplication, and because we use the Connect () function here to contact the "clicked" Event and the Quit () function that can terminate the application (connect ) together, so click on the button and the app terminates. This communication is done between two objects: the sender and the recipient, where the sender is the button and the recipient is the application itself.
Self.setgeometry (300,300,250,150) self.setwindowtitle ('tooltips') Self.setwindowicon (Qtgui.qicon ()) self.center () self.show ()
This code sets the form's display position, size, title, program icon, and finally displays the form. There is also a custom function center, which is used to center the form. (You can annotate self.center () to see what's different)
Since we are inheriting the Qtgui.qwidget class, our new class WinForm is actually a widget (widget) with all the widgets, all three of which are widgets.
Setgeometry This method, it does two things: positioning the part and setting its size "is actually the resize and move of the mixed function." The first two parameters are the x, Y coordinates of the part relative to the parent element "This is actually a window, no parent element, remember?" So it's the x, Y coordinates on the screen. ", the latter two parameters are the width and height of the part
Setwindowicon This method, it sets the icon for the app. To do this, we create a Qtgui.qion object, which is the path to the icon we want when we create it.
def Center (self): qr=self.framegeometry () cp=qtgui.qdesktopwidget (). Availablegeometry (). Center () Qr.movecenter (CP) Self.move (Qr.topleft ())
Custom Function Center for form Center display
Where qtgui.qdesktopwidget This class provides information about the user's desktop, including the screen size.
The Framegeometry method obtains the rectangle frame QR of the main window
Qtgui.qdesktopwidget (). Availablegeometry (). Center () These methods to get the screen resolution and eventually get the coordinates of the middle point of the screen CP
Qr.movecenter (CP) moves the rectangular frame to the center of the screen, unchanged in size
Self.move (Qr.topleft ()) Finally, we move the application window to the upper-left corner of the rectangular frame so that the application window is centered on the screen "note that the move of the part is moved to a point in the upper-left corner."
def closeevent (self,event): res=qtgui.qmessagebox.question (self,'info' , " you want to be sure to quit?" ", QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No) if res== QtGui.QMessageBox.Yes: event.accept () Else: event.ignore ()
This code out of the popup window, the user can confirm or cancel the operation.
Depending on the class definition, Qtgui.qwidget,qtgui.qcloseevent will execute if it is closed. So in order to achieve our goal, we need to re-customize Closeevent () This event handle (handler). Appears to be similar to overriding virtual functions in C #.
Python PyQt4 Learning Note 1