PySide Beginner Examples: Difference between revisions

From FreeCAD Documentation
(minor + translate)
(Marked this version for translation)
Line 1: Line 1:
<translate>
<translate>
==Introduction==
==Introduction== <!--T:1-->
The purpose of this page is to cover beginner level examples of the PySide GUI manager (there are accompanying pages [[PySide_Medium_Examples|Medium PySide Examples]] and [[PySide_Advanced_Examples|Advanced PySide Examples]]).
The purpose of this page is to cover beginner level examples of the PySide GUI manager (there are accompanying pages [[PySide_Medium_Examples|Medium PySide Examples]] and [[PySide_Advanced_Examples|Advanced PySide Examples]]).


<!--T:2-->
Newcomers to GUI programming may stumble over the word "widget". It's meaning outside of computing is usually given as
Newcomers to GUI programming may stumble over the word "widget". It's meaning outside of computing is usually given as


<!--T:3-->
<blockquote>"a small gadget or mechanical device, especially one whose name is unknown or unspecified"</blockquote>
<blockquote>"a small gadget or mechanical device, especially one whose name is unknown or unspecified"</blockquote>


<!--T:4-->
For GUI work such as PySide the term "widget" is most often used to refer to the visible elements of the GUI - windows, dialogs, and input/output features. All visible elements of PySide are called widgets, and, for those who are interested, they all descend from a common parent class, QWidget. In addition to the visible elements PySide also offers widgets for networking, XML, multimedia, and database integration.
For GUI work such as PySide the term "widget" is most often used to refer to the visible elements of the GUI - windows, dialogs, and input/output features. All visible elements of PySide are called widgets, and, for those who are interested, they all descend from a common parent class, QWidget. In addition to the visible elements PySide also offers widgets for networking, XML, multimedia, and database integration.


<!--T:5-->
A widget that is not embedded in a parent widget is called a window and usually windows have a frame and a title bar. The most common types of windows are the "main window" (from the Class QMainWindow) and the various subclasses of the dialog (from the Class QDialog). One big difference is that QDialog is modal (i.e. the user can not do anything outside of the Dialog window while it is open) and the QMainWindow is non-modal which allows the user to interact with other windows in parallel.
A widget that is not embedded in a parent widget is called a window and usually windows have a frame and a title bar. The most common types of windows are the "main window" (from the Class QMainWindow) and the various subclasses of the dialog (from the Class QDialog). One big difference is that QDialog is modal (i.e. the user can not do anything outside of the Dialog window while it is open) and the QMainWindow is non-modal which allows the user to interact with other windows in parallel.


<!--T:6-->
This guide is a shortcut list for getting a PySide program working quickly under FreeCAD, it isn't intended to teach Python or PySide. Some sites that do that are:
This guide is a shortcut list for getting a PySide program working quickly under FreeCAD, it isn't intended to teach Python or PySide. Some sites that do that are:
* [http://zetcode.com/gui/pysidetutorial/ PySide tutorial] at zetcode.com
* [http://zetcode.com/gui/pysidetutorial/ PySide tutorial] at zetcode.com
Line 16: Line 21:
* [http://srinikom.github.io/ PySide 1.0.7 Reference] at Srinikom.github.io (note this is a reference, not a tutorial)
* [http://srinikom.github.io/ PySide 1.0.7 Reference] at Srinikom.github.io (note this is a reference, not a tutorial)


==Import Statement==
==Import Statement== <!--T:7-->
PySide is not loaded with Python by default, it must be requested prior to using it. The following command:
PySide is not loaded with Python by default, it must be requested prior to using it. The following command:
</translate>
</translate>
Line 23: Line 28:
}}
}}
<translate>
<translate>
<!--T:8-->
causes the 2 parts of PySide to be loaded - QtGui holds classes for managing the Graphic User Interface while QtCore contains classes that do not directly relate to management of the GUI (e.g. timers and geometry). Although it is possible to only import the one that is needed, generally they are both needed and both imported.
causes the 2 parts of PySide to be loaded - QtGui holds classes for managing the Graphic User Interface while QtCore contains classes that do not directly relate to management of the GUI (e.g. timers and geometry). Although it is possible to only import the one that is needed, generally they are both needed and both imported.


<!--T:9-->
<strong>Note</strong>: the 'import' statement is not repeated in each code snippet below, it is assumed that it is already in the Python file.
<strong>Note</strong>: the 'import' statement is not repeated in each code snippet below, it is assumed that it is already in the Python file.


==Simplest Example==
==Simplest Example== <!--T:10-->
The simplest interaction with PySide is to present a message to the user which they can only accept:
The simplest interaction with PySide is to present a message to the user which they can only accept:
</translate>
</translate>
Line 38: Line 45:


<translate>
<translate>
==Yes or No Query==
==Yes or No Query== <!--T:11-->
The next most simple interaction is to ask for a yes/no answer:
The next most simple interaction is to ask for a yes/no answer:
</translate>
</translate>
Line 56: Line 63:


<translate>
<translate>
==Enter Text Query==
==Enter Text Query== <!--T:12-->
The next code snippet asks the user for a piece of text - note this can be any key on the keyboard really:
The next code snippet asks the user for a piece of text - note this can be any key on the keyboard really:
</translate>
</translate>
Line 73: Line 80:


<translate>
<translate>
<!--T:13-->
Remember that even if the user enters only digits, "1234" for example, they are strings and must be converted to number representation with either of the following:
Remember that even if the user enters only digits, "1234" for example, they are strings and must be converted to number representation with either of the following:
</translate>
</translate>

Revision as of 17:59, 9 February 2015

Introduction

The purpose of this page is to cover beginner level examples of the PySide GUI manager (there are accompanying pages Medium PySide Examples and Advanced PySide Examples).

Newcomers to GUI programming may stumble over the word "widget". It's meaning outside of computing is usually given as

"a small gadget or mechanical device, especially one whose name is unknown or unspecified"

For GUI work such as PySide the term "widget" is most often used to refer to the visible elements of the GUI - windows, dialogs, and input/output features. All visible elements of PySide are called widgets, and, for those who are interested, they all descend from a common parent class, QWidget. In addition to the visible elements PySide also offers widgets for networking, XML, multimedia, and database integration.

A widget that is not embedded in a parent widget is called a window and usually windows have a frame and a title bar. The most common types of windows are the "main window" (from the Class QMainWindow) and the various subclasses of the dialog (from the Class QDialog). One big difference is that QDialog is modal (i.e. the user can not do anything outside of the Dialog window while it is open) and the QMainWindow is non-modal which allows the user to interact with other windows in parallel.

This guide is a shortcut list for getting a PySide program working quickly under FreeCAD, it isn't intended to teach Python or PySide. Some sites that do that are:

Import Statement

PySide is not loaded with Python by default, it must be requested prior to using it. The following command:

from PySide import QtGui, QtCore

causes the 2 parts of PySide to be loaded - QtGui holds classes for managing the Graphic User Interface while QtCore contains classes that do not directly relate to management of the GUI (e.g. timers and geometry). Although it is possible to only import the one that is needed, generally they are both needed and both imported.

Note: the 'import' statement is not repeated in each code snippet below, it is assumed that it is already in the Python file.

Simplest Example

The simplest interaction with PySide is to present a message to the user which they can only accept:

reply = QtGui.QMessageBox.information(None,"","Houston, we have a problem")


Yes or No Query

The next most simple interaction is to ask for a yes/no answer:

reply = QtGui.QMessageBox.question(None, "", "This is your chance to answer, what do you think?",
         QtGui.QMessageBox.Yes | QtGui.QMessageBox.No, QtGui.QMessageBox.No)
if reply == QtGui.QMessageBox.Yes:
         # this is where the code relevant to a 'Yes' answer goes
         pass
if reply == QtGui.QMessageBox.No:
         # this is where the code relevant to a 'No' answer goes
         pass


Enter Text Query

The next code snippet asks the user for a piece of text - note this can be any key on the keyboard really:

reply = QtGui.QInputDialog.getText(None, "Ouija Central","Enter your thoughts for the day:")
if reply[1]:
	# user clicked OK
	replyText = reply[0]
else:
	# user clicked Cancel
	replyText = reply[0] # which will be "" if they clicked Cancel


Remember that even if the user enters only digits, "1234" for example, they are strings and must be converted to number representation with either of the following:

anInteger = int(userInput) # to convert to an integer from a string representation

aFloat = float(userInput) # to convert to a float from a string representation