我想将主应用程序窗口恰好放置在屏幕的中心。
我尝试了互联网上的所有(我认为)方法,但窗口仍然出现在屏幕中间的左侧和上方。
如何修复它?
from PyQt6.QtWidgets import QLineEdit, QLabel, QVBoxLayout, \
QApplication, QWidget
app = QApplication([])
def center_window(window):
qr = window.frameGeometry()
cp = window.screen().availableGeometry().center()
qr.moveCenter(cp)
window.move(qr.topLeft())
window = QWidget()
window.setWindowTitle('TestWindow')
window.setFixedSize(200, 70)
line = QLineEdit()
line.setPlaceholderText('Введите что-нибудь')
label = QLabel('Это поле для ввода')
layout = QVBoxLayout()
layout.addWidget(label)
layout.addWidget(line)
window.setLayout(layout)
center_window(window)
window.show()
app.exec()
但与此同时,我得到了这个结果:

是的,你视力很好。
尝试让窗口绘制,然后移动窗口。