Qt wiki will be updated on October 12th 2023 starting at 11:30 AM (EEST) and the maintenance will last around 2-3 hours. During the maintenance the site will be unavailable.

Qt for Python Tutorial HelloWorld: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
 
(10 intermediate revisions by 5 users not shown)
Line 1: Line 1:
'''English''' [http://qt-devnet.developpez.com/tutoriels/pyside/simplissimus/#LIII-B French] ''[qt-devnet.developpez.com]'' [[Hello-World-in-PySide-Korean|한국어]] [[Hello-World-in-PySide-Japanese|日本語]]
[[Category:PySide2]]


* '''Note:''' this article is a member of the multipart [[PySide-Newbie-Tutorials|PySide Newbie Tutorials]]
= Your first PySide2 application =


=Your first PySide application=
As with any other programming framework, you start with the traditional "Hello World" program.


If you followed the [[Setting up PySide|Setting_up_PySide]] wiki page to install PySide, you should now have a full blown copy of PySide on your machine to start developing Qt+Python <span class="caps">GUI</span> applications. As with any other programming framework, you start with the traditional “Hello World” program.
Here is a simple example of an Hello World in PySide:
 
<syntaxhighlight lang="python" line='line'>
import sys
from PySide2.QtWidgets import QApplication, QLabel


Here is a simple example of an Hello World in PySide:
app = QApplication(sys.argv)
#label = QLabel("Hello World!")
label = QLabel("<font color=red size=40>Hello World!</font>")
label.show()
app.exec_()
</syntaxhighlight>


With PySide desktop applications, you must always start your file by importing PySide.QtCore and PySide.QtGui classes. These classes have the main functions for building PySide applications. For instance, PySide.QtGui contains functions for dealing with widgets while PySide.QtCore contains methods for handling signals and slots, and controlling the application.
With PySide2 desktop applications, you must always start your file by importing PySide2.QtWidgets classes. These classes have the main functions for building Qt for Python applications.


After the imports, you create a QApplication which is the main Qt application. As Qt can receive arguments from command line, you must pass any arguments to the QApplication object. Usually, you don’t need to pass any arguments so you can leave it as it is.
After the imports, you create a QApplication which is the main Qt application. As Qt can receive arguments from command line, you must pass any arguments to the QApplication object. Usually, you don't need to pass any arguments so you can leave it as it is.


After the creation of the application object, we have created a QLabel object. A QLabel is a widget that can present text (simple or rich, like html), and images. Note that after the creation of the label, we are calling the method '''show''' which will present the label to the user.
After the creation of the application object, we have created a QLabel object. A QLabel is a widget that can present text (simple or rich, like html), and images. Note that after the creation of the label, we are calling the method '''show''' which will present the label to the user.
Line 17: Line 26:
Finally, we call '''app.exec_()''' which will enter the Qt main loop and start to execute the Qt code. In reality, it is only here that the label will be shown, but this can be ignored for now.
Finally, we call '''app.exec_()''' which will enter the Qt main loop and start to execute the Qt code. In reality, it is only here that the label will be shown, but this can be ignored for now.


==Displaying html in the label==
== Displaying html in the label ==


As mentioned previously, you can insert html tags in the label to present rich text. Try changing the code which creates the label for something like:
As mentioned previously, you can insert html tags in the label to present rich text. Try changing the code which creates the label for something like:


and you will the the “Hello World” now bigger and red. You can try to change to another color, another size, or make it blink! Or you can create other elements instead of the QLabel, like QPushButton, or others.
<pre>
 
label = QLabel("<font color=red size=40>Hello World</font>")
===Categories:===
</pre>


* [[:Category:LanguageBindings|LanguageBindings]]
and you will see the "Hello World" now bigger and red. You can try to change to another color, another size, or make it blink! Or you can create other elements instead of the QLabel, like QPushButton, or others.
** [[:Category:LanguageBindings::PySide|PySide]]

Latest revision as of 08:27, 18 April 2018


Your first PySide2 application

As with any other programming framework, you start with the traditional "Hello World" program.

Here is a simple example of an Hello World in PySide:

import sys
from PySide2.QtWidgets import QApplication, QLabel

app = QApplication(sys.argv)
#label = QLabel("Hello World!")
label = QLabel("<font color=red size=40>Hello World!</font>")
label.show()
app.exec_()

With PySide2 desktop applications, you must always start your file by importing PySide2.QtWidgets classes. These classes have the main functions for building Qt for Python applications.

After the imports, you create a QApplication which is the main Qt application. As Qt can receive arguments from command line, you must pass any arguments to the QApplication object. Usually, you don't need to pass any arguments so you can leave it as it is.

After the creation of the application object, we have created a QLabel object. A QLabel is a widget that can present text (simple or rich, like html), and images. Note that after the creation of the label, we are calling the method show which will present the label to the user.

Finally, we call app.exec_() which will enter the Qt main loop and start to execute the Qt code. In reality, it is only here that the label will be shown, but this can be ignored for now.

Displaying html in the label

As mentioned previously, you can insert html tags in the label to present rich text. Try changing the code which creates the label for something like:

label = QLabel("<font color=red size=40>Hello World</font>")

and you will see the "Hello World" now bigger and red. You can try to change to another color, another size, or make it blink! Or you can create other elements instead of the QLabel, like QPushButton, or others.