Your first PySide application

Your first PySide application

If you followed 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 GUI applications. As any other programming framework, you start by the traditional “Hello World”.

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

#!/usr/bin/python
 
# Import PySide classes
import sys
from PySide.QtCore import *
from PySide.QtGui import *
 
 
# Create a Qt application
app = QApplication(sys.argv)
# Create a Label and show it
label = QLabel("Hello World")
label.show()
# Enter Qt application main loop
app.exec_()
sys.exit()


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.

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:

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

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.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值