(1)pyside2 最简单的Qtwidgets程序

Your First QtWidgets Application

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

Here is a simple example of a Hello World application in PySide2:

import sys
from PySide2.QtWidgets import QApplication, QLabel

app = QApplication(sys.argv)
label = QLabel("Hello World!")
label.show()
sys.exit(app.exec_())

For a widget application using PySide2, you must always start by importing the appropriate class from the PySide2.QtWidgets module.

当我们想使用诸如按钮,文本框这种常见的组建时必须从PySide2.QtWidgets导入相应的模块

After the imports, you create a QApplication instance. As Qt can receive arguments from command line, you may pass any argument to the QApplication object. Usually, you don’t need to pass any arguments so you can leave it as is, or use the following approach:

任何一个图形化的qt程序必须要创建QApplication的实例。Qt可以接受命令行参数,我们可以把命令行参数传递给QApplication的对象,就像上图代码所示。也可以像下面一样传递个空数组

app = QApplication([])

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:

Qlabel是一种常见的小组件,他可以呈现诸如html之类的复杂文字或者图片

# This HTML approach will be valid too!
label = QLabel("<font color=red size=40>Hello World!</font>")

QLabel 也可以呈现HTML代码哦!

Note

After the creation of the label, we are calling the

method show() to show the label.

写完上面代码之后这些图形就在计算机内存里面了,为了让他出现在显示器上,我们要用调用show 函数

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

最后一定要调用exec__函数来让Qt的消息循环之星起来,如果不明白的话,只需要记住写完代码一定要加上这个函数,不然程序会一闪而过就完了。

总结

import sys
from PySide2.QtWidgets import QApplication, QLabel
app = QApplication(sys.argv)
label = QLabel("Hello World!")
label.show()
sys.exit(app.exec_())

一个最简单的QtWidgets程序包括一下部分

  • 从PySide2.QtWidgets模块中导入需要的widgets
  • 创建最少一个QApplication对象
  • 调用QApplication的exec__函数,不然程序会一闪而过
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值