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 ClickableButton: Difference between revisions
AutoSpider (talk | contribs) (Rename category "LanguageBindings::PySide" -> "PySide") |
No edit summary |
||
Line 1: | Line 1: | ||
== A simple clickable button tutorial == | |||
In this tutorial, we'll show you how to start handling with Qt for Python's '''signals and slots'''. | |||
Basically, this Qt feature allows your graphical widgets to communicate with other graphical widgets or your own python code. | |||
Our application will create a clickable button which will show '''Button clicked, Hello!''' in the python console each time you click it. | |||
Let's starting by importing the necessary PySide2 classes and python sys module: | |||
<syntaxhighlight lang="python" line='line'> | |||
= | |||
import sys | import sys | ||
from | from PySide2.QtWidgets import QApplication, QPushButton | ||
</syntaxhighlight> | |||
</ | |||
Let's also create a python function which writes | Let's also create a python function which writes the message to the console: | ||
< | <syntaxhighlight lang="python" line='line'> | ||
# Greetings | # Greetings | ||
def | def say_hello(): | ||
print("Button clicked, Hello!") | |||
</ | </syntaxhighlight> | ||
Now, as mentioned in | Now, as mentioned in previous examples you must create the QApplication which will run your PySide2 code: | ||
< | <syntaxhighlight lang="python" line='line'> | ||
# Create the Qt Application | # Create the Qt Application | ||
app = QApplication(sys.argv) | app = QApplication(sys.argv) | ||
</ | </syntaxhighlight> | ||
Let's create the clickable button, a QPushButton. We pass a python string on the constructor which will label the button: | Let's create the clickable button, a QPushButton. | ||
We pass a python string on the constructor which will label the button: | |||
< | <syntaxhighlight lang="python" line='line'> | ||
# Create a button | # Create a button | ||
button = QPushButton("Click me") | button = QPushButton("Click me") | ||
</ | </syntaxhighlight> | ||
Before we show the button, we must connect it to the ''' | Before we show the button, we must connect it to the '''say_hello()''' function that we defined previously. | ||
For now, there are two ways of doing this - by using the old style or the new style. | |||
The new style is more pythonic and that's what we'll use here. | |||
You can find more information about both approaches in [[Signals_and_Slots_in_PySide]]. | |||
The QPushButton has a predefined signal called '''clicked''' which is triggered every time that the button is pressed. | |||
We'll just connect this signal to the '''say_hello()''' function: | |||
<code> | <code> | ||
# Connect the button to the function | # Connect the button to the function | ||
button.clicked.connect( | button.clicked.connect(say_hello) | ||
</code> | </code> | ||
Finally, we show the button and start the Qt main loop: | Finally, we show the button and start the Qt main loop: | ||
< | <syntaxhighlight lang="python" line='line'> | ||
# Show the button | # Show the button | ||
button.show() | button.show() | ||
# Run the main Qt loop | # Run the main Qt loop | ||
app.exec_() | app.exec_() | ||
</ | </syntaxhighlight> | ||
== Full Code == | == Full Code == | ||
< | <syntaxhighlight lang="python" line='line'> | ||
#!/usr/bin/python | #!/usr/bin/python | ||
# -'''- coding: utf-8 -'''- | # -'''- coding: utf-8 -'''- | ||
import sys | import sys | ||
from | from PySide2.QtWidgets import QApplication, QPushButton | ||
def say_hello(): | |||
def | print("Button clicked, Hello!") | ||
print "Hello | |||
# Create the Qt Application | |||
# Create the Qt Application | app = QApplication(sys.argv) | ||
app = QApplication(sys.argv) | # Create a button, connect it and show it | ||
# Create a button, connect it and show it | button = QPushButton("Click me") | ||
button = QPushButton("Click me") | button.clicked.connect(say_hello) | ||
button.clicked.connect( | button.show() | ||
button.show() | # Run the main Qt loop | ||
# Run the main Qt loop | |||
app.exec_() | app.exec_() | ||
</ | </syntaxhighlight> |
Revision as of 09:18, 18 April 2018
A simple clickable button tutorial
In this tutorial, we'll show you how to start handling with Qt for Python's signals and slots. Basically, this Qt feature allows your graphical widgets to communicate with other graphical widgets or your own python code. Our application will create a clickable button which will show Button clicked, Hello! in the python console each time you click it.
Let's starting by importing the necessary PySide2 classes and python sys module:
import sys
from PySide2.QtWidgets import QApplication, QPushButton
Let's also create a python function which writes the message to the console:
# Greetings
def say_hello():
print("Button clicked, Hello!")
Now, as mentioned in previous examples you must create the QApplication which will run your PySide2 code:
# Create the Qt Application
app = QApplication(sys.argv)
Let's create the clickable button, a QPushButton. We pass a python string on the constructor which will label the button:
# Create a button
button = QPushButton("Click me")
Before we show the button, we must connect it to the say_hello() function that we defined previously. For now, there are two ways of doing this - by using the old style or the new style. The new style is more pythonic and that's what we'll use here. You can find more information about both approaches in Signals_and_Slots_in_PySide. The QPushButton has a predefined signal called clicked which is triggered every time that the button is pressed. We'll just connect this signal to the say_hello() function:
# Connect the button to the function
button.clicked.connect(say_hello)
Finally, we show the button and start the Qt main loop:
# Show the button
button.show()
# Run the main Qt loop
app.exec_()
Full Code
#!/usr/bin/python
# -'''- coding: utf-8 -'''-
import sys
from PySide2.QtWidgets import QApplication, QPushButton
def say_hello():
print("Button clicked, Hello!")
# Create the Qt Application
app = QApplication(sys.argv)
# Create a button, connect it and show it
button = QPushButton("Click me")
button.clicked.connect(say_hello)
button.show()
# Run the main Qt loop
app.exec_()