Python tutorial detailed Pyqt-designer generated UI code assembly "reprint"

Source: Internet
Author: User

This article turns from: http://www.52ij.com/jishu/432.html, reprint please keep the link

I said it before. Pyqt-designer the way to generate a UI file to a py file. Bogota tried it and got stuck in the assembly to his own project. My note at the end also said that, to use, but also a little need to change. Write it up today while you're free on the self-test project.

Just make a UI, here I drag a Calendar control directly over.

Generate the Py code, as previously stated.

Create a new project. Create a new SRC package that will automatically generate a __init__.py.

Copy the test.py, which is the UI-generated code.

Here are a few of the areas where there are no changes to the code that need attention.

The box (object) indicates that the class inherits from object. This is changed to Qtgui.qmainwindow. As for the reason, it is also very simple. When I was doing the experiment, I found two ways to generate

Setupui and Retranslateui need to pass in a parameter, that is, the red line drawn out, MainWindow, I think, rather let the class itself inherit from Qmainwindow, then call the auto-generated method to initialize the window, the need to put itself, That is, self can pass in.

The code after the change is as follows, adding a __init__ method, which is automatically called when a Window object is created in a moment, like a Java constructor.

In __init__, the constructor of the parent class Qmainwindow is called First, and then the auto-generated two methods are called, noting that self is passed, because he is already qmainwindow.

There is a need to call Super (XXX,YYY). __init__ (), in fact, a little bit to try out, at the beginning of the Java habit to do, without this call, the result of an error, only to find that it does not automatically call the parent class construction method.

Below, in the auto-generated __init__.py file, write the following code.

The first sentence is to create a PYQT encapsulated Qapplication object, when created, the system parameters are passed in. As the name implies, this sentence creates an application object

The second sentence is to create a window that we generate

The third sentence is the display window

The next sentence is to pass the return value of the Exec_ () method of the Application object to Sys.exit (). When you click the Close window, the Exec_ () method gives the Sys.exit () a return value to close the program

Here to say more, the above analysis may have inappropriate places, and then further study, back to see the time will change.

In addition, after writing the 4th, I have a flash, think app.exec_ () This method, should be a blocking method, which maintains the entire program running, when the exit operation is received, will return a value, and then Sys.exit () to the return value for the entire program to exit.

This is what I have been thinking, PyQt no tkinter. Mainloop () Similar method, how to let the program keep running up. It's so deep ...

I changed a few UI and made several gadgets, so the above steps were repeated several times, but I didn't think about it as much as I did two times before, just to feel like I could run. Know today, try again, have a little in-depth experience. So, more use, more thinking is really important ah ...

Right-click on the file and run it ...

Code to paste it out:

src/__init__.py

From PYQT4 import Qtcore, Qtgui

Import Sys

Import Test

App=qtgui.qapplication (SYS.ARGV)

Win=test. Ui_mainwindow ()

Win.show ()

Sys.exit (App.exec_ ())

src/test.py

#-*-Coding:utf-8-*-

# Form implementation generated from reading UI file ' D:\Program Files\python2.7\lib\site-packages\pyqt4\test.ui '

#

# Created:mon Mar 04 13:03:57 2013

# BY:PYQT4 UI code generator 4.9.6

#

# warning! All changes made in this file would be lost!

From PYQT4 import Qtcore, Qtgui

Try

_fromutf8 = QtCore.QString.fromUtf8

Except Attributeerror:

def _fromutf8 (s):

return s

Try

_encoding = QtGui.QApplication.UnicodeUTF8

def _translate (context, text, disambig):

Return QtGui.QApplication.translate (context, text, disambig, _encoding)

Except Attributeerror:

def _translate (context, text, disambig):

Return QtGui.QApplication.translate (context, text, disambig)

Class Ui_mainwindow (Qtgui.qmainwindow):

def __init__ (self):

Super (Ui_mainwindow,self). __init__ ()

Self.setupui (self);

Self.retranslateui (self)

def setupui (self, MainWindow):

Mainwindow.setobjectname (_fromutf8 ("MainWindow"))

Mainwindow.resize (440, 200)

Self.centralwidget = Qtgui.qwidget (MainWindow)

Self.centralwidget.setObjectName (_fromutf8 ("Centralwidget"))

Self.calendarwidget = Qtgui.qcalendarwidget (self.centralwidget)

Self.calendarWidget.setGeometry (qtcore.qrect (0, 0, 441, 181))

Self.calendarWidget.setObjectName (_fromutf8 ("Calendarwidget"))

Mainwindow.setcentralwidget (Self.centralwidget)

Self.statusbar = Qtgui.qstatusbar (MainWindow)

Self.statusbar.setObjectName (_fromutf8 ("StatusBar"))

Mainwindow.setstatusbar (Self.statusbar)

Self.retranslateui (MainWindow)

QtCore.QMetaObject.connectSlotsByName (MainWindow)

def retranslateui (self, MainWindow):

Mainwindow.setwindowtitle (_translate ("MainWindow", "MainWindow", None))

At the end, the interface ran, but there was no response at all. How to put the callback function (event handler function) up, this, next time to write, involving PYQT signal and slot.

Summarize:

1. Create an SCR folder, save the target UI file to this directory, copy "C:\Python27\Lib\site-packages\PyQt4\uic\pyuic.py" to the directory, new Transform.bat file, write Pyuic.py-o test.py Test.ui, double-click to run this file to generate test.py

2. Open test.py Modify the inside class parameter to Qtgui.qmainwindow, and define the constructor:

def __init__ (self):

Self.super (ui_mainwindow,self). __init__ ()

Self.setupui (self)

Self.retranslateui (self)

3. Create the package flag file in src __init__.py, open it on the inside to write:

From PYQT4 import Qtcore, Qtgui

Import Sys

Import Test

App = Qtcore.qapplication (SYS,ARGV)

Win = test. Ui_mainwindow ()

Win = Show ()

Sys.exit (App_exec ())

Python tutorial detailed Pyqt-designer generated UI code assembly "reprint"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.