QDesktopWidget
类来获取屏幕的大小和位置信息,然后根据这些信息计算出窗口的位置和大小;QDesktopWidget().screenGeometry()
可以获取到屏幕坐标: def screenGeometry(self, *__args): # real signature unknown; restored from __doc__ with multiple overloads
"""
screenGeometry(self, screen: int = -1) -> QRect
screenGeometry(self, QWidget) -> QRect
screenGeometry(self, QPoint) -> QRect
"""
pass
screen = QDesktopWidget().screenGeometry()
geometry()
方法获取到当前窗口的坐标:size = self.geometry()
l = (screen.width() - size.width()) / 2
t = (screen.height() - size.height()) / 2
self.move(l, t)
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/11/2
# 文件名称:test021_CenterForm.py
# 作用:窗口居中
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QDesktopWidget
class CenterForm(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("标准窗口")
self.resize(500, 400)
def center(self):
# 获得屏幕坐标
screen = QDesktopWidget().screenGeometry()
# 获取窗口坐标系
size = self.geometry()
l = (screen.width() - size.width()) / 2
t = (screen.height() - size.height()) / 2
self.move(l, t)
if __name__ == "__main__":
app = QApplication(sys.argv)
win = CenterForm()
win.show()
sys.exit(app.exec_())
Qt Designer
来实现的,主要是可视化操作;self.button = QPushButton("点击按钮")
self.button.clicked.connect(self.on_click)
lay = QHBoxLayout()
lay.addWidget(self.button)
mframe = QWidget()
mframe.setLayout(lay)
self.setCentralWidget(mframe)
def on_click(self):
# 获取对象信号
s = self.sender()
print(s.text() + " 应用程序已经退出")
app = QApplication.instance()
app.quit()
# -*- coding:utf-8 -*-
# 作者:虫无涯
# 日期:2023/11/2
# 文件名称:test022_QuitAPP.py
# 作用:退出应用程序
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QHBoxLayout, QWidget, QPushButton
class QuitAPP(QMainWindow):
def __init__(self):
super(QuitAPP, self).__init__()
# 窗口尺寸
self.resize(500, 400)
# 窗口标题
self.setWindowTitle("验证应用程序的退出")
# 设置一个按钮
self.button = QPushButton("点击按钮")
# 信号与槽关联
self.button.clicked.connect(self.on_click)
# 水平布局
lay = QHBoxLayout()
lay.addWidget(self.button)
# 增加按钮
mframe = QWidget()
mframe.setLayout(lay)
self.setCentralWidget(mframe)
# 按钮事件
def on_click(self):
# 获取对象信号
s = self.sender()
print(s.text() + " 应用程序已经退出")
app = QApplication.instance()
app.quit()
if __name__ == "__main__":
app = QApplication(sys.argv)
win = QuitAPP()
win.show()
sys.exit(app.exec_())
D:\Python37\python.exe F:/pyqt_study/test_case/test022_QuitAPP.py
点击按钮 应用程序已经退出
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。