如何将 QLineEdit 值传输到控制台?
How to transfer QLineEdit value into console?
假设我们在一个 .py 文件中有一个包含 Buttons 和 LineEdits 的界面。我在另一个继承它的代码中有这个代码:
import Inter_Input_Blasting as interf
from PyQt6 import QtCore,QtWidgets,QtGui
from functools import partial
class MainWindow(QtWidgets.QMainWindow):
def on_clicked(self):
print("Button Pushed")
def __init__(self,parent=None):
super(MainWindow, self).__init__(parent)
self.ui = interf.Ui_MainWindow()
self.ui.setupUi(self)
self.ui.calc_button.clicked.connect(MainWindow.on_clicked)
self.ui.input_overall_1.textChanged.connect(MainWindow.gather_data)
def gather_data(self):
return self.ui.input_overall_1.text()
if __name__== "__main__":
import sys
app = interf.QtWidgets.QApplication(sys.argv)
Form = MainWindow()
Form.show()
sys.exit(app.exec())
所以,当我把它放在 lineedit 字段中时,我需要将我的值打印到控制台中。 .textChanged()
方法有效,但 .gather_data()
无效。
提供一个用于存储文本的变量:
def __init__(self,parent=None):
self.txt = None
然后在方法 gather_data
中,将文本存储在该变量中:
def gather_data(self):
sef.txt = self.ui.input_overall_1.text()
然后在 sys.exit
之前打印该值:
r = app.exec()
print(Form.txt)
sys.exit(r)
假设我们在一个 .py 文件中有一个包含 Buttons 和 LineEdits 的界面。我在另一个继承它的代码中有这个代码:
import Inter_Input_Blasting as interf
from PyQt6 import QtCore,QtWidgets,QtGui
from functools import partial
class MainWindow(QtWidgets.QMainWindow):
def on_clicked(self):
print("Button Pushed")
def __init__(self,parent=None):
super(MainWindow, self).__init__(parent)
self.ui = interf.Ui_MainWindow()
self.ui.setupUi(self)
self.ui.calc_button.clicked.connect(MainWindow.on_clicked)
self.ui.input_overall_1.textChanged.connect(MainWindow.gather_data)
def gather_data(self):
return self.ui.input_overall_1.text()
if __name__== "__main__":
import sys
app = interf.QtWidgets.QApplication(sys.argv)
Form = MainWindow()
Form.show()
sys.exit(app.exec())
所以,当我把它放在 lineedit 字段中时,我需要将我的值打印到控制台中。 .textChanged()
方法有效,但 .gather_data()
无效。
提供一个用于存储文本的变量:
def __init__(self,parent=None):
self.txt = None
然后在方法 gather_data
中,将文本存储在该变量中:
def gather_data(self):
sef.txt = self.ui.input_overall_1.text()
然后在 sys.exit
之前打印该值:
r = app.exec()
print(Form.txt)
sys.exit(r)