PyQt5-16 屏幕坐标系的了解和基本使用

简介: PyQt5-16 屏幕坐标系的了解和基本使用

1 什么是屏幕坐标系?

2 相关概念

  • 屏幕坐标系,即窗口相对于屏幕的坐标。屏幕左上角坐标称为原点坐标(0,0);
  • 窗口的坐标,即窗口的左上角相对原来的坐标,如下图示:
    在这里插入图片描述
  • 窗口的宽和高也有两种,一种是工作取的高度,一种菜单栏的高度,比如如下说明:
    在这里插入图片描述

    3 代码实现

  • 创建一个窗口,在窗口的工作区添加一个按钮:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/11/4 
# 文件名称:test023_ScreenGeo.py
# 作用:屏幕坐标系
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import sys

from PyQt5.QtWidgets import QApplication, QMainWindow, QHBoxLayout, QWidget, QPushButton


class ScreenGeo(QMainWindow):
    def __init__(self):
        super(ScreenGeo, self).__init__()
        self.w = QWidget()
        self.b = QPushButton(self.w)
        self.b.setText("按钮")

        self.b.move(20, 30)

        # 窗口尺寸
        self.w.resize(500, 400)
        self.w.move(400, 300)
        # 窗口标题
        self.w.setWindowTitle("屏幕坐标系")
        self.w.show()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = ScreenGeo()
    # win.show()
    sys.exit(app.exec_())
  • 运行后如下效果:
    在这里插入图片描述
  • 在按钮上加一个事件,比如是点击按钮后,显示“这是一个按钮~~”,代码如下:
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/11/4 
# 文件名称:test023_ScreenGeo.py
# 作用:屏幕坐标系
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import sys

from PyQt5.QtWidgets import QApplication, QMainWindow, QHBoxLayout, QWidget, QPushButton


class ScreenGeo(QMainWindow):
    def __init__(self):
        super(ScreenGeo, self).__init__()
        self.w = QWidget()
        self.b = QPushButton(self.w)
        self.b.setText("按钮")
        self.b.clicked.connect(self.on_click)

        self.b.move(20, 30)

        # 窗口尺寸
        self.w.resize(500, 400)
        self.w.move(400, 300)
        # 窗口标题
        self.w.setWindowTitle("屏幕坐标系")
        self.w.show()

    def on_click(self):
        print("这是一个按钮~~~")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = ScreenGeo()
    # win.show()
    sys.exit(app.exec_())
  • 效果如下:
    在这里插入图片描述
D:\Python37\python.exe F:/pyqt_study/test_case/test023_ScreenGeo.py
这是一个按钮~~~

4 获取窗口坐标

4.1 直接获取

  • 这个表示的是从窗口左上角计算;
  • 以下是 窗口的横纵坐标 和 工作区宽高
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/11/4 
# 文件名称:test023_ScreenGeo.py
# 作用:屏幕坐标系
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import sys

from PyQt5.QtWidgets import QApplication, QMainWindow, QHBoxLayout, QWidget, QPushButton


class ScreenGeo(QMainWindow):
    def __init__(self):
        super(ScreenGeo, self).__init__()
        self.w = QWidget()
        self.b = QPushButton(self.w)
        self.b.setText("按钮")
        self.b.clicked.connect(self.on_click)

        self.b.move(20, 30)

        # 窗口尺寸
        self.w.resize(500, 400)
        self.w.move(400, 300)
        # 窗口标题
        self.w.setWindowTitle("屏幕坐标系")
        self.w.show()

    def on_click(self):
        print("这是一个按钮~~~")
        # 直接获取坐标
        print(f"窗口横坐标:{self.w.x()}") # 
        print(f"窗口纵坐标:{self.w.y()}")
        print(f"工作区宽度:{self.w.width()}")
        print(f"工作区高度:{self.w.height()}")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = ScreenGeo()
    # win.show()
    sys.exit(app.exec_())
  • 效果如下:
    在这里插入图片描述

4.2 通过坐标系获取

  • 这个表示从工作区左上角计算;
  • 以下表示 工作区的横纵坐标 和 工作区的宽高;
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/11/4 
# 文件名称:test023_ScreenGeo.py
# 作用:屏幕坐标系
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import sys

from PyQt5.QtWidgets import QApplication, QMainWindow, QHBoxLayout, QWidget, QPushButton


class ScreenGeo(QMainWindow):
    def __init__(self):
        super(ScreenGeo, self).__init__()
        self.w = QWidget()
        self.b = QPushButton(self.w)
        self.b.setText("按钮")
        self.b.clicked.connect(self.on_click)

        self.b.move(20, 30)

        # 窗口尺寸
        self.w.resize(500, 400)
        self.w.move(400, 300)
        # 窗口标题
        self.w.setWindowTitle("屏幕坐标系")
        self.w.show()

    def on_click(self):
        print("这是一个按钮~~~")
        # 直接获取坐标
        print("直接获取坐标")
        print(f"窗口横坐标:{self.w.x()}") # 
        print(f"窗口纵坐标:{self.w.y()}")
        print(f"工作区宽度:{self.w.width()}")
        print(f"工作区高度:{self.w.height()}")

        # 通过坐标系获取坐标
        print("通过坐标系获取坐标")
        print(f"工作区横坐标:{self.w.geometry().x()}")
        print(f"工作区纵坐标:{self.w.geometry().y()}")
        print(f"工作区宽度:{self.w.geometry().width()}")
        print(f"工作区高度:{self.w.geometry().height()}")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = ScreenGeo()
    # win.show()
    sys.exit(app.exec_())
  • 效果如下:
    在这里插入图片描述

    4.3 获取Frame坐标

  • 这个表示从整个窗口和菜单的高度;
  • 以下是获取窗口的横纵坐标 以及 窗口的宽高;
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/11/4 
# 文件名称:test023_ScreenGeo.py
# 作用:屏幕坐标系
# 联系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import sys

from PyQt5.QtWidgets import QApplication, QMainWindow, QHBoxLayout, QWidget, QPushButton


class ScreenGeo(QMainWindow):
    def __init__(self):
        super(ScreenGeo, self).__init__()
        self.w = QWidget()
        self.b = QPushButton(self.w)
        self.b.setText("按钮")
        self.b.clicked.connect(self.on_click)

        self.b.move(20, 30)

        # 窗口尺寸
        self.w.resize(500, 400)
        self.w.move(400, 300)
        # 窗口标题
        self.w.setWindowTitle("屏幕坐标系")
        self.w.show()

    def on_click(self):
        print("这是一个按钮~~~")
        # 直接获取坐标
        print("直接获取坐标")
        print(f"窗口横坐标:{self.w.x()}") # 
        print(f"窗口纵坐标:{self.w.y()}")
        print(f"工作区宽度:{self.w.width()}")
        print(f"工作区高度:{self.w.height()}")

        # 通过坐标系获取坐标
        print("通过坐标系获取坐标")
        print(f"工作区横坐标:{self.w.geometry().x()}")
        print(f"工作区纵坐标:{self.w.geometry().y()}")
        print(f"工作区宽度:{self.w.geometry().width()}")
        print(f"工作区高度:{self.w.geometry().height()}")

        # 通过坐标系获取坐标
        print("获取Frame坐标")
        print(f"窗口横坐标:{self.w.frameGeometry().x()}")
        print(f"窗口纵坐标:{self.w.frameGeometry().y()}")
        print(f"窗口宽度:{self.w.frameGeometry().width()}")
        print(f"窗口高度:{self.w.frameGeometry().height()}")

if __name__ == "__main__":
    app = QApplication(sys.argv)
    win = ScreenGeo()
    # win.show()
    sys.exit(app.exec_())
  • 效果如下:
    在这里插入图片描述
目录
相关文章
|
SQL 监控 Oracle
Oracle数据误删不用怕,跟我来学日志挖掘
Oracle数据误删不用怕,跟我来学日志挖掘
315 0
|
9月前
|
存储 编解码 算法
微帧WZ-JPEG图片编码压缩技术,实现超高压缩效率
在数字化时代,图像数据爆炸式增长,对传输和存储提出巨大挑战。JPEG作为互联网上最广泛应用的图片格式之一,占据超过60%的市场份额。微帧WZ-JPEG编码压缩技术通过优化DCT变换、量化及熵编码等步骤,实现了显著的压缩效率提升,平均节省27%的图片体积,复杂场景下可达40%,同时编码速度提升4倍,确保了高质量图像的快速加载与传输,极大提升了用户体验。此外,微帧还针对WebP、HEIF和AVIF等格式进行了专门优化,进一步彰显其在图像处理领域的技术优势。
|
Prometheus 监控 Cloud Native
使用 Jenkins 监控和优化构建性能
【8月更文第31天】在软件开发的过程中,构建性能直接影响着开发效率和团队的生产力。一个快速、可靠的构建流程可以显著加快迭代速度,减少等待时间,使团队能够更快地响应变化。Jenkins 作为一款广泛使用的持续集成/持续交付(CI/CD)工具,提供了丰富的功能来帮助开发者监控和优化构建性能。本文将探讨如何利用 Jenkins 的内置工具和外部工具来监控构建性能,并提出一些具体的优化方案。
973 0
|
测试技术 API Python
小红书API接口测试 | 小红书笔记详情 API 接口测试指南
随着互联网的发展,越来越多的应用开始使用API接口来提供服务。而API接口的测试也变得越来越重要。本文将介绍如何使用Python语言进行小红书笔记详情API接口的测试。
|
存储 SQL 安全
DVWA File Upload 通关解析
DVWA File Upload 通关解析
|
区块链
max code size exceeded
max code size exceeded
150 6
|
缓存 JavaScript 前端开发
vscode的webview性能优化总结
vscode的webview性能优化总结
158 0
|
Linux Docker 容器
Red Hat系列Docker安装与移除
Docker 安装 添加 Docker 官方 YUM 源 sudo yum-config-manager --add-repo=https://download.docker.com/linux/centos/docker-ce.repo 安装 Docker Engine sudo yum install -y docker-ce docker-ce-cli containerd.io 启动 Docker 服务并设置开机自启 sudo systemctl start docker sudo systemctl enable docker 验证 Docker 安装 docker --versio
483 0
|
存储 运维 算法
LVS详解(二)——LVS工作模式
LVS详解(二)——LVS工作模式
218 3
|
存储 Go 开发者
Golang深入浅出之-Go语言字符串操作:常见函数与面试示例
【4月更文挑战第20天】Go语言字符串是不可变的字节序列,采用UTF-8编码。本文介绍了字符串基础,如拼接(`+`或`fmt.Sprintf()`)、长度与索引、切片、查找与替换(`strings`包)以及转换与修剪。常见问题包括字符串不可变性、UTF-8编码处理、切片与容量以及查找与替换的边界条件。通过理解和实践这些函数及注意事项,能提升Go语言编程能力。
387 0

热门文章

最新文章