PyQt5 文本框内容传递给函数参数 | 实用教程
使用以下步骤将 PyQt5 文本框中的内容传递给另一个函数的参数:
- 在 PyQt5 中创建一个文本框并获取其文本内容:
from PyQt5.QtWidgets import QLineEdit, QWidget, QVBoxLayout, QPushButton, QApplication
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.textbox = QLineEdit(self)
self.textbox.setText('Hello World')
layout = QVBoxLayout(self)
layout.addWidget(self.textbox)
button = QPushButton('Submit', self)
button.clicked.connect(self.on_submit)
layout.addWidget(button)
self.show()
def on_submit(self):
text = self.textbox.text() # 获取文本框中的文本内容
my_function(text) # 将文本内容传递给另一个函数
- 创建另一个函数并将文本框中的文本内容作为参数传递给它:
def my_function(text):
print('The text is:', text)
注意:在示例中,我们将文本框中的文本内容作为字符串传递给 my_function() 函数。如果需要将其他类型的数据传递给另一个函数,需要根据数据类型进行转换和处理。
原文地址: https://www.cveoy.top/t/topic/oKo9 著作权归作者所有。请勿转载和采集!