1 什么是屏幕坐标系?
2 相关概念
- 屏幕坐标系,即窗口相对于屏幕的坐标。屏幕左上角坐标称为原点坐标(0,0);
- 窗口的坐标,即窗口的左上角相对原来的坐标,如下图示:
![在这里插入图片描述](https://img-blog.csdnimg.cn/img_convert/7458fce4e29a603e5ada81592b3a470d.webp?x-oss-process=image/format,png)
- 窗口的宽和高也有两种,一种是工作取的高度,一种菜单栏的高度,比如如下说明:
![在这里插入图片描述](https://img-blog.csdnimg.cn/img_convert/1a60400d469c30f7c0f1fb475f87873a.webp?x-oss-process=image/format,png)
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_())
- 运行后如下效果:
![在这里插入图片描述](https://img-blog.csdnimg.cn/img_convert/215075a105a40a0803854f40408486f8.webp?x-oss-process=image/format,png)
- 在按钮上加一个事件,比如是点击按钮后,显示“这是一个按钮~~”,代码如下:
# -*- 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("按