Python QT 获取lineEdit 的数字和字母 加上前缀0x 存放在yaml文件
时间: 2024-03-30 20:37:01 浏览: 32
您可以使用Python中的PyQt5库来获取lineEdit的值,并使用PyYAML库将其存储为YAML格式的文件。下面是一个示例代码:
```python
import yaml
from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QPushButton, QVBoxLayout
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.line_edit = QLineEdit()
self.button = QPushButton("Save")
self.button.clicked.connect(self.save_to_yaml)
layout = QVBoxLayout()
layout.addWidget(self.line_edit)
layout.addWidget(self.button)
self.setLayout(layout)
def save_to_yaml(self):
value = self.line_edit.text()
value_with_prefix = "0x" + value
data = {"value": value_with_prefix}
with open("data.yaml", "w") as f:
yaml.dump(data, f)
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
```
在这个示例代码中,我们创建了一个包含一个QLineEdit和一个QPushButton的窗口。当用户点击按钮时,调用save_to_yaml函数。该函数获取lineEdit的文本值,将前缀"0x"添加到该值前面,并将其存储为YAML格式的文件。在这个示例中,我们将数据存储在"data.yaml"文件中,但您可以根据自己的需求更改文件名和路径。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)