• pyqt5 学习笔记三 (窗口设置)


    pyqt5 学习笔记三 (w.resize()、w.move()、窗口设置、窗口图标)

    写在前面:
    ①本教程使用pycharm编译器进行pyqt5的学习,安装教程请大家自行百度
    ②本系列博客根据B站王铭东博主教程学习 学习代码 lesson3_1
    链接:https://pan.baidu.com/s/1i3y4mI_9N84iAC6tav-j8Q
    提取码:gupx
    资料:
    【1】B站教程
    【2】B站教程配套课件
    【3】PyQt官网的所有模块 (有部分内容没有补充进去)
    【4】C++具体实现的API文档

    (一)设置窗口大小

    w.resize(800, 800)

    import sys
    from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel, QLineEdit
    if __name__ == '__main__':
       app = QApplication(sys.argv)
       w = QWidget()
       w.setWindowTitle("第一个PyQt")
       w.resize(800, 800)   # (w,h)
       w.show()
       app.exec()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    (二)设置窗口位置

    1、初始情况:默认弹出的窗口中心位置为屏幕中心位置

    在这里插入图片描述

    2、移动窗口函数 w.move(x,y)

    将窗口位置移动到屏幕左上角 w.move(0, 0)

    import sys
    from PyQt5.QtWidgets import QApplication, QWidget
    if __name__ == '__main__':
       app = QApplication(sys.argv)
       w = QWidget()
       w.setWindowTitle("第一个PyQt")
       w.resize(300, 300)  # resize(w,h)
       # 调整窗口在屏幕左上角
       w.move(0, 0)
       w.show()
       app.exec()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    其中move中的位置不能为负值,否则会报错
    在这里插入图片描述

    3、将新生成窗口左上角移动到屏幕中心

    QDesktopWidget().availableGeometry().center()
    center_pointer.x()

    import sys
    from PyQt5.QtWidgets import QApplication, QWidget, QDesktopWidget
    if __name__ == '__main__':
       app = QApplication(sys.argv)
       w = QWidget()
       w.setWindowTitle("第一个PyQt")
       w.resize(300, 300)  # resize(w,h)
       # 调整窗口在屏幕左上角
       w.move(0, 0)
       # 调整窗口在屏幕中央显示
       # QDesktopWidget() 当前屏幕组件 屏幕可用的区域 中心坐标
       center_pointer = QDesktopWidget().availableGeometry().center()   # todo
       print(center_pointer)
       x = center_pointer.x()
       y = center_pointer.y()
       w.move(x, y)
       # 显示窗口
       w.show()
       # 程序进行循环等待状态
       app.exec()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    QDesktopWidget().availableGeometry().center()
    得到屏幕组件可用区域的中心位置

       center_pointer = QDesktopWidget().availableGeometry().center() 
       print(center_pointer)
       print(type(center_pointer))
    
    • 1
    • 2
    • 3

    center_pointer.x():得到center_pointer的横纵坐标

       x = center_pointer.x()
       y = center_pointer.y()
       w.move(x, y)  # 窗口左上角在屏幕中心
    
    • 1
    • 2
    • 3

    输出:新生成窗口的左上角显示在屏幕中心位置

    PyQt5.QtCore.QPoint(959, 519)
    
    
    • 1
    • 2

    在这里插入图片描述

    4、将新生成窗口中心移动到屏幕中心

    已知:屏幕中心 x y
    未知:新生成窗口的宽高
    w.frameGeometry():得到新生成窗口的坐标与宽高
    输出:

    PyQt5.QtCore.QRect(0, 0, 300, 300)
    
    
    • 1
    • 2

    w.frameGeometry().getRect():得到坐标及宽高的元组
    输出:

    (0, 0, 300, 300)
    
    
    • 1
    • 2

    代码:

    import sys
    from PyQt5.QtWidgets import QApplication, QWidget, QDesktopWidget
    if __name__ == '__main__':
       app = QApplication(sys.argv)
       w = QWidget()
       w.setWindowTitle("第一个PyQt")
       w.resize(300, 300)  # resize(w,h)
       # 调整窗口在屏幕左上角
       w.move(0, 0)
       # 调整窗口在屏幕中央显示
       # QDesktopWidget() 当前屏幕组件 屏幕可用的区域 中心坐标
       center_pointer = QDesktopWidget().availableGeometry().center()  # todo
       print(center_pointer)
       print(type(center_pointer))
       x = center_pointer.x()
       y = center_pointer.y()
       # w.move(x, y)  # 窗口左上角在屏幕中心
       # w.move(x - 300 / 2, y - 300 / 2)  # 将窗口中心移动到屏幕中心
       print(w.frameGeometry())  # 得到窗户的位置与大小
       print(type(w.frameGeometry()))
       print(w.frameGeometry().getRect())  # 将位置与大小写作(x,y,width,height)
       print(type(w.frameGeometry().getRect()))  # 打印出来为元组
       p_x, p_y, width, height = w.frameGeometry().getRect()  # 拆包
       w.move(x - width / 2, y - height / 2)  # 将窗口中心移动到屏幕中心
       # 显示窗口
       w.show()
       # 程序进行循环等待状态
       app.exec()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    输出结果:
    在这里插入图片描述

    (三)窗口图标设置

    图标素材链接:https://igoutu.cn/icons

    import sys
    from PyQt5.QtWidgets import QApplication, QWidget
    from PyQt5.QtGui import QIcon
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        w = QWidget()
        w.setWindowTitle("图标")
        # 设置图标
        w.setWindowIcon(QIcon('icon.png'))
        w.show()
        app.exec()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在这里插入图片描述


    补充内容:
    (1)搜索函数
    当在代码中有个打印了一个你不知道的方法时,查询官方文档:
    https://www.riverbankcomputing.com/static/Docs/PyQt5/module_index.html#ref-module-index
    运行代码后,打印输出了:
    PyQt5.QtCore.QPoint(959, 519)
    此时查找上面链接的文档找到对应的说明:
    在这里插入图片描述
    ②如果此时该文档中没有解释,查询https://doc.qt.io/qt-5/classes.html ctrl+F 搜索内容
    如查找QPoint,则找到下面链接:https://doc.qt.io/qt-5/qpoint.html
    (2)todo的用法
    todo:在代码注释中写入todo表示待解决的事情
    在这里插入图片描述
    在后续编写代码的时候就能够快速定位该位置

    在这里插入图片描述


  • 相关阅读:
    【操作系统笔记】进程和线程
    推荐3款卓越的 .NET 开源搜索组件库
    接口测试及接口测试工具
    线性表的链式存储结构以及单链表的插入和删除原理实现
    containerd的ctr tasks kill不起作用
    数字化转型的同群效应数据集(2007-2021年)
    求实数的整数次幂(循环版)(高效)(位运算解题)
    代码随想录贪心算法——买卖股票的最佳时机含手续费
    Accelerate 0.24.0文档 三:超大模型推理(内存估算、Sharded checkpoints、bitsandbytes量化、分布式推理)
    医学访问学者申请四点规划建议
  • 原文地址:https://blog.csdn.net/weixin_40581859/article/details/125745469