Qt for Python Tutorial ClickableButton: Difference between revisions

From Qt Wiki
Jump to navigation Jump to search
No edit summary
 
No edit summary
Line 1: Line 1:
'''English''' [[PySideTutorials Clickable button Japanese|日本語]]
[[Category:LanguageBindings::PySide]]


=A simple clickable button tutorial=
'''English''' [[PySideTutorials_Clickable_button_Japanese|日本語]]


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.
= A simple clickable button tutorial =


Let’s starting by importing the necessary Qt classes and python sys class:
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 also create a python function which writes “Hello World” to the console:
Let's starting by importing the necessary Qt classes and python sys class:


Now, as mentioned in [[Hello World in PySide|Your first PySide application]], you must create the QApplication which will run your PySide code:
<code><br />import sys<br />from PySide.QtCore import *<br />from PySide.QtGui import *<br /></code>


Let’s create the clickable button, a QPushButton. We pass a python string on the constructor which will label the button:
Let's also create a python function which writes &quot;Hello World&amp;quot; to the console:


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|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><br /># Greetings<br />def sayHello():<br /> print &quot;Hello World!&quot;<br /></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><br /># Create the Qt Application<br />app = QApplication(sys.argv)<br /></code>
 
Let's create the clickable button, a QPushButton. We pass a python string on the constructor which will label the button:
 
<code><br /># Create a button<br />button = QPushButton(&quot;Click me&amp;quot;)<br /></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:
 
<code><br /># Connect the button to the function<br />button.clicked.connect(sayHello)<br /></code>


Finally, we show the button and start the Qt main loop:
Finally, we show the button and start the Qt main loop:


==Full Code==
<code><br /># Show the button<br />button.show()<br /># Run the main Qt loop<br />app.exec_()<br /></code>
 
== Full Code ==
 
<code><br />#!/usr/bin/python<br /># <s>'''- coding: utf-8 -'''</s>


Now that you know the basics, can you get it to print your name?
import sys<br />from PySide.QtCore import *<br />from PySide.QtGui import *


===Categories:===
def sayHello():<br /> print &quot;Hello World!&quot;


* [[:Category:LanguageBindings|LanguageBindings]]
# Create the Qt Application<br />app = QApplication(sys.argv)
** [[:Category:LanguageBindings::PySide|PySide]]
# Create a button, connect it and show it<br />button = QPushButton(&quot;Click me&amp;quot;)<br />button.clicked.connect(sayHello)<br />button.show()
# Run the main Qt loop<br />app.exec_()<br /></code>

Revision as of 14:29, 23 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:

<br />import sys<br />from PySide.QtCore import *<br />from PySide.QtGui import *<br />

Let's also create a python function which writes "Hello World&quot; to the console:

<br /># Greetings<br />def sayHello():<br /> print &quot;Hello World!&quot;<br />

Now, as mentioned in Your first PySide application, you must create the QApplication which will run your PySide code:

<br /># Create the Qt Application<br />app = QApplication(sys.argv)<br />

Let's create the clickable button, a QPushButton. We pass a python string on the constructor which will label the button:

<br /># Create a button<br />button = QPushButton(&quot;Click me&amp;quot;)<br />

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:

<br /># Connect the button to the function<br />button.clicked.connect(sayHello)<br />

Finally, we show the button and start the Qt main loop:

<br /># Show the button<br />button.show()<br /># Run the main Qt loop<br />app.exec_()<br />

Full Code

<br />#!/usr/bin/python<br /># <s>'''- coding: utf-8 -'''</s>

import sys<br />from PySide.QtCore import *<br />from PySide.QtGui import *

def sayHello():<br /> print &quot;Hello World!&quot;

# Create the Qt Application<br />app = QApplication(sys.argv)
# Create a button, connect it and show it<br />button = QPushButton(&quot;Click me&amp;quot;)<br />button.clicked.connect(sayHello)<br />button.show()
# Run the main Qt loop<br />app.exec_()<br />