Qt for Python Tutorial ClickableButton: Difference between revisions
No edit summary |
No edit summary |
||
Line 9: | Line 9: | ||
Let's starting by importing the necessary Qt classes and python sys class: | Let's starting by importing the necessary Qt classes and python sys class: | ||
<code> | <code> | ||
import sys | |||
from PySide.QtCore import * | |||
from PySide.QtGui import * | |||
</code> | |||
Let's also create a python function which writes | Let's also create a python function which writes "Hello World" to the console: | ||
<code> | <code> | ||
# Greetings | |||
def sayHello(): | |||
print "Hello World!" | |||
</code> | |||
Now, as mentioned in [[Hello_World_in_PySide|Your first PySide application]], you must create the QApplication which will run your PySide code: | Now, as mentioned in [[Hello_World_in_PySide|Your first PySide application]], you must create the QApplication which will run your PySide code: | ||
<code> | <code> | ||
# Create the Qt Application | |||
app = QApplication(sys.argv) | |||
</code> | |||
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: | ||
<code> | <code> | ||
# Create a button | |||
button = QPushButton("Click me") | |||
</code> | |||
Before we show the button, we must connect it to the '''sayHello()''' 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 '''sayHello()''' function: | Before we show the button, we must connect it to the '''sayHello()''' 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 '''sayHello()''' function: | ||
<code> | <code> | ||
# Connect the button to the function | |||
button.clicked.connect(sayHello) | |||
</code> | |||
Finally, we show the button and start the Qt main loop: | Finally, we show the button and start the Qt main loop: | ||
<code> | <code> | ||
# Show the button | |||
button.show() | |||
# Run the main Qt loop | |||
app.exec_() | |||
</code> | |||
== Full Code == | == Full Code == | ||
<code> | <code> | ||
#!/usr/bin/python | |||
# -'''- coding: utf-8 -'''- | |||
import sys | import sys | ||
from PySide.QtCore import * | |||
from PySide.QtGui import * | |||
def sayHello(): | def sayHello(): | ||
print "Hello World!" | |||
# Create the Qt Application | # Create the Qt Application | ||
# Create a button, connect it and show it | app = QApplication(sys.argv) | ||
# Run the main Qt loop | # Create a button, connect it and show it | ||
button = QPushButton("Click me") | |||
button.clicked.connect(sayHello) | |||
button.show() | |||
# Run the main Qt loop | |||
app.exec_() | |||
</code> |
Revision as of 08:53, 25 February 2015
English 日本語
A simple clickable button tutorial
In this tutorial, we'll show you how to start handling with PySide'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 Hello World in the python console each time you press it.
Let's starting by importing the necessary Qt classes and python sys class:
import sys
from PySide.QtCore import *
from PySide.QtGui import *
Let's also create a python function which writes "Hello World" to the console:
# Greetings
def sayHello():
print "Hello World!"
Now, as mentioned in Your first PySide application, you must create the QApplication which will run your PySide 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 sayHello() 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 sayHello() function:
# Connect the button to the function
button.clicked.connect(sayHello)
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 PySide.QtCore import *
from PySide.QtGui import *
def sayHello():
print "Hello World!"
# Create the Qt Application
app = QApplication(sys.argv)
# Create a button, connect it and show it
button = QPushButton("Click me")
button.clicked.connect(sayHello)
button.show()
# Run the main Qt loop
app.exec_()