我电脑的信息是macos m2,Sonoma14.6,安装pyqt5时出错,没找到解决方案 错误大概如下
× Preparing metadata (pyproject.toml) did not run successfully.
│ exit code: 1
╰─> [21 lines of output]
pyproject.toml: line 7: using '[tool.sip.metadata]' to specify the project metadata is deprecated and will be removed in SIP v7.0.0, use '[project]' instead
于是直接安装pyqt6 ,要注意的是QDesktopWidget 使用 QScreen 代替
pip install pyqt6-tools
运行代码
import sys
from PyQt6.QtWidgets import QApplication, QMainWindow
from PyQt6.QtGui import QScreen
class MyWindow(QMainWindow):
def __init__(self):
super().__init__()
# 设置窗口标题
self.setWindowTitle("你好")
# 设置窗口大小
self.resize(800, 600)
# 将窗口移动到屏幕中央
self.center()
def center(self):
# 获取屏幕的尺寸
screen = QScreen.availableGeometry(QApplication.primaryScreen())
screen_width = screen.width()
screen_height = screen.height()
# 获取窗口的尺寸
window_size = self.geometry()
window_width = window_size.width()
window_height = window_size.height()
# 计算窗口居中的位置
x = (screen_width - window_width) // 2
y = (screen_height - window_height) // 2
# 设置窗口的位置
self.move(x, y)
def main():
# 创建应用程序对象
app = QApplication(sys.argv)
# 创建主窗口
window = MyWindow()
window.show()
# 运行应用程序主循环
sys.exit(app.exec())
if __name__ == "__main__":
main()
结果如下