PySide学习教程.docx

上传人:hw****26 文档编号:2127536 上传时间:2019-04-30 格式:DOCX 页数:149 大小:856.35KB
下载 相关 举报
PySide学习教程.docx_第1页
第1页 / 共149页
PySide学习教程.docx_第2页
第2页 / 共149页
PySide学习教程.docx_第3页
第3页 / 共149页
PySide学习教程.docx_第4页
第4页 / 共149页
PySide学习教程.docx_第5页
第5页 / 共149页
点击查看更多>>
资源描述

1、PySide tutorialThis is PySide tutorial. The tutorial is suited for beginners and intermediate programmers. After reading this tutorial, you will be able to program nontrivial PySide applications.Table of contents Introduction First programs Menus and toolbars Layout management Events and signals Dia

2、logs Widgets Widgets II Drag & drop Drawing Custom widgets The Tetris gamePySidePySide is Python library to create cross-platform graphical user interfaces. It is a Python binding to the Qt framework. Qt library is one of the most powerful GUI libraries. It is developed by Digia and Qt Project.Tweet

3、Related tutorialsThere is a full Python tutorial on ZetCode. Tutorials for other GUI Python bindings include PyQt4 tutorial, wxPython tutorial, PyGTK tutorial and Tkinter tutorial.Introduction to PySide toolkitThis is an introductory PySide tutorial. The purpose of this tutorial is to get you starte

4、d with the PySide toolkit. The tutorial has been created and tested on Linux.About PySidePySide is Python library to create cross-platform graphical user interfaces. It is a Python binding to the Qt framework. Qt library is one of the most powerful GUI libraries. The official home site for PySide is

5、 qt-project.org/wiki/PySide. The installation instructions can be found atpypi.python.org/pypi/PySide.PySide is implemented as a set of Python modules. Currently it has 15 modules. These modules provide powerful tools to work with GUI, multimedia, XML documents, network or databases. In our tutorial

6、, we will work with two of these modules. The QtGui and the QtCore modules.The QtCore module contains the core non GUI functionality. This module is used for working with time, files and directories, various data types, streams, URLs, mime types, threads or processes. The QtGui module contains the g

7、raphical components and related classes. These include for example buttons, windows, status bars, toolbars, sliders, bitmaps, colours, fonts etc.PySide has been released after Nokia, the owner of the Qt toolkit, failed to reach an agreement with Riverbank Computing to include the LGPL as an alternat

8、ive license. PySide has a high API compatibility wit PyQt4, so migrating to PySide is not difficult.PythonPython is a general-purpose, dynamic, object-oriented programming language. The design purpose of the Python language emphasizes programmer productivity and code readability. Python was initiall

9、y developed by Guido van Rossum. It was first released in 1991. Python was inspired by ABC, Haskell, Java, Lisp, Icon and Perl programming languages. Python is a high level, general purpose, multiplatform, interpreted language. Python is a minimalistic language. One of its most visible features is t

10、hat it does not use semicolons nor brackets. Python uses indentation instead. There are two main branches of Python currently. Python 2.x and Python 3.x. Python 3.x breaks backward compatibility with previous releases of Python. It was created to correct some design flaws of the language and make th

11、e language more clean. The most recent version of Python 2.x is 2.7.1, and of Python 3.x 3.1.3. This tutorial covers Python 2.x versions. Most of the code is written in Python 2.x versions. It will take some time till the software base and programmers will migrate to Python 3.x. Today, Python is mai

12、ntained by a large group of volunteers worldwide. Python is open source software.Python is an ideal start for those, who want to learn programming.Python programming language supports several programming styles. It does not force a programmer to a specific paradigm. Python supports object oriented a

13、nd procedural programming. There is also a limited support for functional programming.The official web site for the Python programming language is python.orgPython ranks among the most popular programming languages. According to the , Python ranks on the 6. place. The TIOBE index puts Python on the

14、8. place. On , a popular repository of software projects, Python is the third most popular language, having 9% share of all projects hosted.Python toolkitsFor creating modern graphical user interfaces, Python programmers can choose among these decent options: PySide, PyQt4, Python/Gnome (former PyGT

15、K) and wxPython.This chapter was an introduction to PySide toolkit.First programs in PySideIn this part of the PySide tutorial we will learn some basic functionality.Simple exampleThe code example is very simplistic. It only shows a small window. Yet we can do a lot with this window. We can resize i

16、t, maximise it or minimise it. This requires a lot of coding. Someone already coded this functionality. Because it repeats in most applications, there is no need to code it over again. So it has been hidden from a programmer. PySide is a high level toolkit. If we would code in a lower level toolkit,

17、 the following code example could easily have dozens of lines.#!/usr/bin/python# -*- coding: utf-8 -*-# simple.pyimport sysfrom PySide import QtGuiapp = QtGui.QApplication(sys.argv)wid = QtGui.QWidget()wid.resize(250, 150)wid.setWindowTitle(Simple)wid.show()sys.exit(app.exec_()The above code shows a

18、 small window on the screen.import sysfrom PySide import QtGuiHere we provide the necessary imports. The basic GUI widgets are located in QtGui module.app = QtGui.QApplication(sys.argv)Every PySide application must create an application object. The application object is located in the QtGui module.

19、The sys.argv parameter is a list of arguments from the command line. Python scripts can be run from the shell. It is a way, how we can control the startup of our scripts.wid = QtGui.QWidget()The QWidget widget is the base class of all user interface objects in PySide. We provide the default construc

20、tor for QWidget. The default constructor has no parent. A widget with no parent is called a window.wid.resize(250, 150)The resize() method resizes the widget. It is 250px wide and 150px high.wid.setWindowTitle(Simple)Here we set the title for our window. The title is shown in the titlebar.wid.show()

21、The show() method displays the widget on the screen.sys.exit(app.exec_()Finally, we enter the mainloop of the application. The event handling starts from this point. The mainloop receives events from the window system and dispatches them to the application widgets. The mainloop ends if we call the e

22、xit() method or the main widget is destroyed. The sys.exit()method ensures a clean exit. The environment will be informed, how the application ended.You wonder why the exec_() method has an underscore? Everything has a meaning. This is obviously because the exec is a Python keyword. And thus, exec_(

23、) was used instead.Figure: SimpleAn application iconThe application icon is a small image, which is usually displayed in the top left corner of the titlebar. It is also visible in the taskbar. In the following example we will show, how we do it in PySide. We will also introduce some new methods.#!/u

24、sr/bin/python# -*- coding: utf-8 -*-“ZetCode PySide tutorial This example shows an iconin the titlebar of the window.author: Jan Bodnarwebsite: last edited: August 2011“import sysfrom PySide import QtGuiclass Example(QtGui.QWidget):def _init_(self):super(Example, self)._init_()self.initUI()def init

25、UI(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 la

26、nguage supports both procedural and object oriented programming styles. Programming in PySide 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 crea

27、te a new class called Example. The Example class inherits from QtGui.QWidget class. This means that we must call two constructors. The first one for the Example class and the second one for the inherited class. The second one is called with the super() method.self.setGeometry(300, 300, 250, 150)self

28、.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 the size of the window. The first two parameters are the x and y positions of the window. T

29、he third is the width and the fourth is the height of the window. 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_()

30、if _name_ = _main_:main()We put the startup code inside the main() method. This is a Python idiom.Figure: IconAn icon is visible in the top-left corner of the window.Showing a tooltipWe can provide a balloon help for any of our widgets.#!/usr/bin/python# -*- coding: utf-8 -*-“ZetCode PySide tutorial

31、 This example shows a tooltip on a window and a buttonauthor: Jan Bodnarwebsite: last edited: August 2011“import sysfrom PySide import QtGuiclass Example(QtGui.QWidget):def _init_(self):super(Example, self)._init_()self.initUI()def initUI(self):QtGui.QToolTip.setFont(QtGui.QFont(SansSerif, 10)self.

32、setToolTip(This is a QWidget widget)btn = QtGui.QPushButton(Button, self)btn.setToolTip(This is a QPushButton widget)btn.resize(btn.sizeHint()btn.move(50, 50) self.setGeometry(300, 300, 250, 150)self.setWindowTitle(Tooltips) self.show()def main():app = QtGui.QApplication(sys.argv)ex = Example()sys.e

33、xit(app.exec_()if _name_ = _main_:main()In this example, we show a tooltip for a two PySide widgets.QtGui.QToolTip.setFont(QtGui.QFont(SansSerif, 10)This static method sets a font used to render tooltips. We use a 10px SansSerif font.self.setToolTip(This is a QWidget widget)To create a tooltip, we c

34、all the setTooltip() method. We can use rich text formatting.btn = QtGui.QPushButton(Button, self)btn.setToolTip(This is a QPushButton widget)We create a button widget and set a tooltip for it.btn.resize(btn.sizeHint()btn.move(50, 50) The button is being resized and moved on the window. The sizeHint() method gives a recommended size for the button.Figure: TooltipsClosing a windowThe obvious way how to close a window is to click on the x mark on the titlebar. In the next example, we will show, how we can programatically close our window. We will briefly touch signals and slots.

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育教学资料库 > 课程笔记

Copyright © 2018-2021 Wenke99.com All rights reserved

工信部备案号浙ICP备20026746号-2  

公安局备案号:浙公网安备33038302330469号

本站为C2C交文档易平台,即用户上传的文档直接卖给下载用户,本站只是网络服务中间平台,所有原创文档下载所得归上传人所有,若您发现上传作品侵犯了您的权利,请立刻联系网站客服并提供证据,平台将在3个工作日内予以改正。