Pyside--python graphical interface Getting Started tutorial (i)
--Basic components and HelloWorld
Original link: http://pythoncentral.io/intro-to-pysidepyqt-basic-widgets-and-hello-world/
The first part of this tutorial will give you the basics of pyside, including the objects you use, and some small examples that will help you understand how the PYTHON/QT app is built.
First look at the basic Qt object. QT contains many classes to deal with things like XML, multimedia, databases, and networks, but we now focus on the elements of visualization-windows, dialogs, and controls. All of the visual elements of QT are called parts (original widgets, low translation level, so as not to mislead others, so follow widgets), and inherit from a common parent class qwidget. In this article we continue to use "widgets" as the name of the QT application visualization element.
First PYTHON/QT application: Hello,world
Let's start with a very simple app: a label that says "hello,wordl!" 's window. It's easy to understand, but it doesn't look good-we'll change it later.
#Allow access to command-line argumentsImportSYS#Import the core and GUI elements of Qt fromPyside.qtcoreImport* fromPyside.qtguiImport*#every Qt application must has one and only one Qapplication object;#it receives the command line arguments passed to the script, as they#can is used to customize the application ' s appearance and BehaviorQt_app =qapplication (SYS.ARGV)#Create a label widget with our textLabel = Qlabel ('Hello, world!.') #Show it as a standalone widgetlabel.show ()#Run The application ' s event loopQT_APP.EXEC_ ()
Let's summarize what the program has done:
1. Create a QT application
2. Create a Widget
3. Display as Window
4. Run the Application event loop
This is the basic architecture for all QT applications. Each application, no matter how many windows it opens, must have only one Qapplication object--used to initialize, handle control flow, event processing and application plane settings, and clean up when the app is closed.
A widget that does not have a father (parent, such as a Father window) created, indicating that it is displayed as a window, which is the application's start-up window. When it is shown, the Exec_ method of the Qapplication object is called and begins to enter the application's main event loop.
Some explanations for the examples:
1. Note that the Qapplication constructor receives SYS.ARGV as a parameter, allowing the user to customize the appearance of the command line behavior.
2. Our main widget is a qlabel that can display simple text. Any object that widget--inherits from Qwidget-can be displayed as a window.
Two basic wedgets
Let's take a look at the two most basic python/qt widgets. The first is the parent class of all widgets, Qwidget, and then one of the simplest widgets inherited from it.
Qwidget
A Qwideget constructor receives two parameters, the parent qwidget and the flags Qwindowflags, and all of its subclasses have these two parameters. The widget is owned by the parent Qwidget, and when the parent is destroyed, the widget is destroyed and the widget's location is restricted to its parent. If the parent parameter is None or not set, the widget is at the top level and is owned by the Qapplication object. The flags parameter controls various properties of the widget, such as whether it is a window or not, and usually the default of 0 is the correct choice.
You will often construct qwidget like this:
Widget = Qwidget ()
Or
Widget = Qwidget (some_parent)
A qwidget is often used to create top-level windows, like this:
1 qt_app = Qapplication (SYS.ARGV) Span style= "color: #008080;" > 2 3 # Create a widget widget = Qwidget () 5 6 # Show it as a standalone widget widget.show () 9 # Run the application ' event loop 10 qt_app.exec_ ()
The Qwidget class has many methods, and more commonly used methods are discussed in other widgets. The next use of the Setminimumsize method takes a qtcore.qsize as its argument, and Qsize represents two dimensions (width * height) to measure the widget's pixels.
Widget.setminimumsize (Qsize (800, 600))
Another method that is used by all widgets is Setwindowtitle, which sets its title bar if the widget is displayed as the topmost window.
Widget.setwindowtitle ('I Am A window! ')
Qlabel
We have used Qlabel in HelloWorld, which is often used to display simple text or rich text, even pictures, videos. (often non-interactive)
It has two similar constructors, one identical to Qwidget, and the other receives a Unicode string literal to display.
1 label = Qlabel (parent_widget)2 label = Qlabel ('Hello, world! ', Parent_widget)
The contents of the label are left-aligned by default, but Qlabel has a setalignment method that can change it to any PySide.QtCore.Qt.Alignment, like this:
Label.setalignment (Qt.aligncenter)
You can also use the Setindet method of Qlabel to set indents, which are calculated from the content-aligned edges, for example: if it is qt.alignright, the indentation will be counted from the right. Wrap text in Qlabel with Qlabel.setwordwrap (True); set to false to suppress line wrapping (note "Calling it with an argument of False
turns of Word-wrapping.word-wrapping "may be turns off). There are many ways to Qlabel, but these are the most basic.
A more advanced Hello,world.
Now that we have studied the Qwidget class and its subclass Qlabel, we can upgrade our "hello,world! "has been applied.
Before we created a global widget variable, this time we encapsulated our window by integrating Qlabel. This may seem a bit complicated, but we'll explain the principle later in the example.
1 #Remember that we ' re omitting the import2 #examples for brevity3 4 #Create the Qapplication object5Qt_app =qapplication (SYS.ARGV)6 7 classHelloworldapp (Qlabel):8 " "A Qt application that displays the text, "Hello, world!"" "9 def __init__(self):Ten #Initialize The object as a Qlabel OneQlabel.__init__(Self,"Hello, world!.") A - #Set the size, alignment, and title -Self.setminimumsize (Qsize (600, 400)) the self.setalignment (qt.aligncenter) -Self.setwindowtitle ('Hello, world!.') - - defRun (self): + " "Show the application window and start the main event loop" " - self.show () + qt_app.exec_ () A at #Create An instance of the application and run it -Helloworldapp (). Run ()
Well, to get to this point, we're ready for some dry stuff in the next example: more widgets, layout containers (layouts containers), slots and Signals--QT the way the app interacts with the user.
Reprint Please specify source: http://www.cnblogs.com/ascii0x03/p/5495035.html
by ascii0x03
Pyside--python graphical interface Getting Started tutorial (i)