PyQt5初级——

基础模块

QtCore模块涵盖了包的核心的非GUI功能,此模块被用于处理程序中涉及到的 time、文件、目录、数据类型、文本流、链接、mime、线程或进程等对象。

QtGui模块涵盖多种基本图形功能的类; 包括但不限于:窗口集、事件处理、2D图形、基本的图像和界面 和字体文本。

QtWidgets模块包含了一整套UI元素组件,用于建立符合系统风格的classic界面,非常方便,可以在安装时选择是否使用此功能。

 

第一个程序

#!/usr/bin/python3
#coding = utf-8

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import QCoreApplication

class Ico(QWidget):

   def __init__(self):
       super().__init__()
       self.initUI()    

   def initUI(self):

       self.setGeometry(300, 300, 300, 220)
       self.setWindowTitle('学点编程吧出品')
       self.setWindowIcon(QIcon('xdbcb8.ico'))

       qbtn = QPushButton('退出', self)
       qbtn.clicked.connect(QCoreApplication.instance().quit)
       qbtn.resize(qbtn.sizeHint())
       qbtn.move(50, 50)
       qbtn.setToolTip("点我退出")
       self.show()
if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Ico()
    sys.exit(app.exec_())

创建一个Ico类,继承已经实现好的QWidget类。

在initUI初始化中,用setGeometry设置大窗口的位置和大小;用setWindowTitle设置标题;用setWindowIcon设置图标

接下来是对按钮的设置了,有resize,move,setToolTip(设置提示信息)等方法。

最关键的是qbtn.clicked.connect(QCoreApplication.instance().quit);它将按钮上发生的事件与一个quit动作联系起来了。

QCoreApplication类包含了主事件循环;它处理和转发所有事件。instance()方法给我们返回一个实例化对象。

 

猜你喜欢

转载自blog.csdn.net/f156207495/article/details/81156453