PyQt5练习(一)

这一节讲述PyQt5构建窗体的方法。并在代码块和下文中注释

import sys
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.QtGui import QIcon

if __name__ == '__main__':
    app = QApplication(sys.argv)    #创建一个应用

    windows = QWidget()       #在这个应用中创建一个窗口
    windows.resize(500,150)   #设置窗口大小
    windows.move(430,250)     #移动窗口位置
    windows.setWindowTitle("XPTest")  #设置窗口的标题
    windows.setWindowIcon(QIcon('1.jpg'))   #添加窗口图标
    windows.show()    #显示这个窗口

    sys.exit(app.exec())    #将app的退出信号传给程序进程,让程序进程退出

效果图:
PyQt5练习(一)_第1张图片
1.代码中import sys 主要是在启动的时候进行参数传递。然后QApplication可以接收系统参数。
2.代码中from PyQt5.QtWidgets import QApplication, QWidget 主要是在程序中引用QApplication, QWidget
QApplicaion英文原意:
Every PyQt5 application must create an application object. The sys.argv parameter is a list of argument from a command line. Python scripts can be run from the shell. It is a way how we can control the startup of our scripts.(每个PyQt5应用程序都必须建立1个应用程序对象,这个应用程序对象可以从命令行接收参数列表。因为Python脚本语言可以从Shell运行,所以这是一种控制脚本语言启动的方法)
QWidget英文原意:
The QWidget widget is the base class of all user interface objects in PyQt5. We provide the default constructor for QWidget. The default constructor has no parent. A widget with no parent is called a window.(QWidget是PyQt5种所有用户接口对象的基类,它有1个缺省构造器,在构造时无父对象,我们管这种无父对象的widget称为window)
3.代码if name == ‘main’: 用来判断是模块还是测试。如果是模块调用该程序,就不执行后续的语句。如果是测试就执行测试语句,
4.代码 QApplication(sys.argv) 是建立一个应用。
5.代码 QWidget() 表示创建一个窗体
6.代码 resize() 设置窗体的大小
7.代码 move() 设置窗体的位置。注释:默认在屏幕的左上方
8.代码 setWindowTitle() 给窗体添加标题
9代码 setWindowIcon(QIcon(‘图片’)) 给窗体添加图标
10.代码 show() 显示窗体
11.代码 sys.exit(应用.exec()) 将应用的退出命令给进程,进程关闭窗口(应用.exe()表示执行事件循环,整个程序就运行起来,当点击窗体的关闭按钮的时候,这个程序将返回0。因此sys.exit()函数接受到0后,程序就会退出)

博客原创地址:https://blog.csdn.net/weixin_43620922/article/details/90759484
第二篇:https://blog.csdn.net/weixin_43620922/article/details/90760256

你可能感兴趣的:(python)