PySide学习教程_计算机软件及应用_IT计算机_专业资料.pdf
《PySide学习教程_计算机软件及应用_IT计算机_专业资料.pdf》由会员分享,可在线阅读,更多相关《PySide学习教程_计算机软件及应用_IT计算机_专业资料.pdf(143页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、PySide tutorialThis is PySide tutorial.The tutorial is suited for beginners and intermediateprogrammers.After reading this tutorial,you will be able to programnontrivial PySide applications.Table of contents Inlroduction First programs Menus and toolbars Layout management Events and signals Dialogs
2、Widgels Widgets H Drag&drop Drawing Custom widgets The Tetris gamePySidePySide is Python library to create cross-platform graphical user interfaces.Itis a Python binding to the Qt framework.Qt library is one of the mostpowerful GUI libraries.It is developed by Digia and Qt Project.TweetRelated tutor
3、ialsThere is a full Python tutorial on ZetCode.Tutorials for other GUI Pythonbindings include PyOt4 tutorial,wxPython tutorial,PyGTKtutorial and Tkinter tutorial.Introduction to PySide toolkitThis is an introductory PySide tutorial.The purpose of this tutorial is to getyou started with the PySide to
4、olkit.The tutorial has been created and testedon Linux.About PySidePySide is Python library to create cross-platform graphical user interfaces.Itis a Python binding to the Qt framework.Qt library is one of the mostpowerful GUI libraries.The official home site for PySideis ul-Droject.org/wiki/PySide.
5、The installation instructions can be foundatpyDi.python.org/pypi/PySide.PySide is implemented as a set of Python modules.Currently it has 15modules.These modules provide powerful tools to work with GUI,multimedia,XML documents,network or databases.In our tutorial,we will work with twoof these module
6、s.The QtGui and the QtCore modules.The QtCore module contains the core non GUI functionality.This module isused for working with time,files and directories,various data types,streams,URLs,mime types,threads or processes.The QtGui module contains thegraphical components and related classes.These incl
7、ude 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 toreach an agreement with Riverbank Computing to include the LG PL as analternative license.PySide has a high API compatibility wit PyQt4,som
8、igrating to PySide is not difficult.PythonPython is a general-purpose,dynamic,object-oriented programminglanguage.The design purpose of the Python language emphasizesprogrammer productivity and code readability.Python was initially developedby Guido van Rossum.It was first released in 1991.Python wa
9、s inspired byABC,Haskell,Java,Lisp,Icon and Perl programming languages.Python is ahigh level,general purpose,multiplatform,interpreted language.Python is aminimalistic language.One of its most visible features is that it does not usesemicolons nor brackets.Python uses indentation instead.There are t
10、wo mainbranches of Python currently.Python 2.x and Python 3.x.Python 3.x breaksbackward compatibility with previous releases of Python.It was created tocorrect some design flaws of the language and make the language more clean.The most recent version of Python 2.x is 2.7.1,and of Python 3.x 3.1.3.Th
11、istutorial covers Python 2.x versions.Most of the code is written in Python 2.xversions.It will take some time till the software base and programmers willmigrate to Python 3.x.Today,Python is maintained by a large group ofvolunteers worldwide.Python is open source software.Python is an ideal start f
12、or those,who want to learn programming.Python programming language supports several programming styles.It doesnot force a programmer to a specific paradigm.Python supports objectoriented and procedural programming.There is also a limited support forfunctional programming.The official web site for th
13、e Python programming language is python.ergPython ranks among the most popular programming languages.According tothe .Python ranks on the 6.place.The TIOBE index puts Pythonon the 8.place.On 、a popular repository of software projects,Python is the third most popular language,having 9%share of all pr
14、ojectshosted.Python toolkitsFor creating modern graphical user interfaces,Python programmers canchoose among these decent options:PySide,PyQt4,Python/Gnome(formerPyGTK)and wxPython.This chapter was an introduction to PySide toolkit.First programs in PySideIn this part of the PySide tutorial we will
15、learn some basic functionality.Simple exampleThe code example is very simplistic.It only shows a small window.Yet we cando a lot with this window.We can resize it,maximise it or minimise it.Thisrequires a lot of coding.Someone already coded this functionality.Because itrepeats in most applications,t
16、here is no need to code it over again.So it hasbeen hidden from a programmer.PySide is a high level toolkit.If we wouldcode in a lower level toolkit,the following code example could easily havedozens of lines.#!/usr/bin/python#coding:utf-8#simple.pyimport sysfrom PySide import QtGuiapp=QtGui.QApplic
17、ation(sys.argv)wid=QtGui.QWidget()wid.resize(250,150)wid.setWindowTitle(Simple)wid.show()sys.exit(app.exec_()The above code shows a small window on the screen.import sysfrom PySide import QtGuiHere we provide the necessary imports.The basic GUI widgets are locatedin QtGui module.app=QtGui.QApplicati
18、on(sys.argv)Every PySide application must create an application object.The applicationobject is located in the QtGui module.The sys.argv parameter is a list ofarguments 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.
19、QWidget()The QWidget widget is the base class of all user interface objects in PySide.Weprovide the default constructor for QWidget.The default constructor has noparent.A widget with no parent is called a window.wid.resize(250z 150)The resize()method resizes the widget.It is 250px wide and 150px hig
20、h.wid.setWindowTitle(*Simple 1)Here we set the title for our window.The title is shown in the titlebar.wid.show()The show()method displays the widget on the screen.sys.exit(app.exec_()Finally,we enter the mainloop of the application.The event handling startsfrom this point.The mainloop receives even
21、ts from the window system anddispatches them to the application widgets.The mainloop ends if we callthe exit()method or the main widget is destroyed.The sys.exit()methodensures a clean exit.The environment will be informed,how the applicationended.You wonder why the exec_()method has an underscore?E
22、verything has ameaning.This is obviously because the exec is a Python keyword.Andthus,exec_()was used instead.Figure:SimpleAn application iconThe application icon is a small image,which is usually displayed in the top leftcorner of the titlebar.It is also visible in the taskbar.In the following exam
23、plewe will show,how we do it in PySide.We will also introduce some newmethods.#!/usr/bin/python#coding:utf-8n n nZetCode PySide tutorialThis example shows an iconin the titlebar of the window.author:Jan Bodnarwebsite:last edited:August 2011H”nimport sysfrom PySide import QtGuiclass Example(QtGui.QWi
24、dget):def _init_(self):super(Example,self)._init_()self.initUI()def initUI(self):self.setGeometry(300,300,250,150)self.setWindowTitle(*Icon)self.setwindowicon(QtGui.Qlcon(1 web.png*)self.show()def main():app=QtGui.QApplication(sys.argv)ex=Example()sys.exit(app.exec_()if _name_ =*_main_:main()The pre
25、vious example was coded in a procedural style.Python programminglanguage 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
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- PySide 学习 教程 计算机软件 应用 _IT 计算机 专业 资料
限制150内