戳人痛处

导航

[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 2022-03-07 16:42  戳人痛处  阅读(107)  评论(0编辑  收藏  举报