python qt 设置控件位置
时间: 2023-06-26 16:06:54 浏览: 263
在 Python Qt 中设置控件位置可以使用 `setGeometry()` 方法。这个方法需要传入四个参数,分别是控件的 x 坐标、y 坐标、宽度和高度。
以下是一个例子,将一个按钮移动到窗口的中心位置:
```python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
btn = QPushButton('Button', self)
btn.resize(btn.sizeHint())
btn.move(100, 100)
# 计算窗口的中心位置
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Example')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
```
在上面的例子中,使用 `move()` 方法将按钮移动到 (100, 100) 的位置。然后计算窗口的中心位置,并使用 `move()` 方法将窗口移动到中心位置。
注意,`move()` 方法和 `setGeometry()` 方法都是 QWidget 类的方法。
阅读全文
相关推荐


















