在 QLineEdit 中 加一个按键
时间: 2024-05-12 21:21:05 浏览: 19
要在 QLineEdit 中添加一个按键,需要使用QPushButton并将其添加到QHBoxLayout中,然后将QHBoxLayout设置为QLineEdit的布局。
以下是一个示例:
```
from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QPushButton, QHBoxLayout
import sys
class MyWindow(QWidget):
def __init__(self):
super().__init__()
# 创建QLineEdit
self.lineEdit = QLineEdit(self)
# 创建QPushButton
self.button = QPushButton('按键', self)
# 创建QHBoxLayout
layout = QHBoxLayout()
layout.addWidget(self.lineEdit)
layout.addWidget(self.button)
# 将QHBoxLayout设置为QLineEdit的布局
self.setLayout(layout)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
```
运行代码后,将看到在QLineEdit旁边添加了一个名为“按键”的QPushButton。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)