pyqt lineedit 居中
时间: 2023-10-30 22:02:58 浏览: 23
要将 PyQt 的 QLineEdit 组件居中,可以使用以下代码:
```python
from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QVBoxLayout
import sys
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
vbox = QVBoxLayout()
# 创建 QLineEdit 对象
qle = QLineEdit(self)
# 设置文本居中
qle.setAlignment(Qt.AlignCenter)
vbox.addWidget(qle)
self.setLayout(vbox)
self.setGeometry(300, 300, 350, 300)
self.setWindowTitle('居中的 QLineEdit')
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
```
在上面的代码中,我们创建了一个 QLineEdit 对象,并使用 `setAlignment()` 方法将其文本设置为居中。最后,我们使用 QVBoxLayout 将 QLineEdit 添加到窗口中。