pyqt5实现点击按钮,生成一个文本框,文本框固定大小,固定间距;所有文本框水平布局
时间: 2024-05-09 18:15:12 浏览: 23
可以使用QHBoxLayout来实现所有文本框水平布局。具体代码如下:
```python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QHBoxLayout, QLineEdit
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# 创建按钮并绑定clicked事件
btn = QPushButton('生成文本框', self)
btn.clicked.connect(self.createLineEdit)
# 创建水平布局
hbox = QHBoxLayout()
hbox.addWidget(btn)
# 设置窗口的主布局为水平布局
self.setLayout(hbox)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('生成文本框')
self.show()
def createLineEdit(self):
# 创建文本框
lineEdit = QLineEdit(self)
lineEdit.setFixedSize(100, 30) # 固定文本框大小
lineEdit.move(10, 50) # 固定文本框间距
```
在这个例子中,我们创建了一个按钮,点击按钮会生成一个文本框。所有文本框使用水平布局,文本框大小和间距都是固定的。你可以根据需要调整文本框大小和间距。