[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
嗯,就酱~
-------------====================分割线====================-------------
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!