戳人痛处

导航

统计

[Pyside6-Study] Chapter-1

1.库安装

pip install pyside6  

嗯,就是这么简单;

2.第一个GUI程序

复制代码
# Import the necessary modules required
# import sys
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QApplication,QLabel
# Main Function
if __name__ == '__main__':
    # Create the main application
    myApp = QApplication()#sys.argv
    # Create a Label and set its properties
    appLabel = QLabel()
    appLabel.setText("Hello, World!!!\n Look at my first app using PySide")
    appLabel.setAlignment(Qt.AlignCenter)
    appLabel.setWindowTitle("My First Application")
    appLabel.setGeometry(300, 300, 250, 175)
    # Show the Label
    appLabel.show()
    # Execute the Application and Exit
    myApp.exec_()
    # sys.exit()
复制代码

sys库提供命令行输入参数

QApplication()提供主事件循环并且必须在部件创建之前实例化
复制代码
# Import the necessary modules required
import sys
from PySide6.QtCore import Qt
from PySide6.QtWidgets import QApplication,QLabel
# Main Function
if __name__ == '__main__':
    # Create the main application
    myApp = QApplication(sys.argv)
    # Create a Label and set its properties
    appLabel = QLabel()
    appLabel.setText("Hello, World!!!\n Look at my first app using PySide")
    appLabel.setAlignment(Qt.AlignCenter)
    appLabel.setWindowTitle("My First Application")
    appLabel.setGeometry(300, 300, 250, 175)
    # Show the Label
    appLabel.show()
    # Execute the Application and Exit
    myApp.exec_()
    sys.exit()
复制代码

嗯,就是这样;

 3.简单的异常处理

--未使用异常处理 try-except

复制代码
# Import the necessary modules required
import sys
from PySide6.QtCore import *
from PySide6.QtWidgets import *
# Main Function
if __name__ == '__main__':
 # Create the main application
 myApp = QApplication(sys.argv)
 # Create a Label and set its properties
#  try:
    #appLabel = QLabel()
appLabel.setText("Hello, World!!!\n Look at my first app using PySide")
appLabel.setAlignment(Qt.AlignCenter)
appLabel.setWindowTitle("My First Application")
appLabel.setGeometry(300, 300, 250, 175)
# Show the Label
appLabel.show()
# Execute the Application and Exit
myApp.exec_()
sys.exit()
#  except NameError:
#     print("Name Error:", sys.exc_info()[1])
#     pass
复制代码

输出如下

Traceback (most recent call last):
  File "d:/pppython/python/python_all_by_me/ppyqt/newStudy/chapter1/异常处理.py", line 12, in <module>
    appLabel.setText("Hello, World!!!\n Look at my first app using PySide")
NameError: name 'appLabel' is not defined

使用try-except

复制代码
# Import the necessary modules required
import sys
from PySide6.QtCore import *
from PySide6.QtWidgets import *
# Main Function
if __name__ == '__main__':
 # Create the main application
 myApp = QApplication(sys.argv)
 # Create a Label and set its properties
try:
    #appLabel = QLabel()
    appLabel.setText("Hello, World!!!\n Look at my first app using PySide")
    appLabel.setAlignment(Qt.AlignCenter)
    appLabel.setWindowTitle("My First Application")
    appLabel.setGeometry(300, 300, 250, 175)
    # Show the Label
    appLabel.show()
    # Execute the Application and Exit
    myApp.exec_()
    sys.exit()
except NameError:
    print("Name Error:", sys.exc_info()[1])
    pass
复制代码

输出如下并且处理了错误进行输出

Name Error: name 'appLabel' is not defined

 

嗯,就酱~

posted on   戳人痛处  阅读(130)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示