我不知道如何在一个文本框中输入并在另一个文本框中显示输出

Posted

技术标签:

【中文标题】我不知道如何在一个文本框中输入并在另一个文本框中显示输出【英文标题】:i dont know how to take input in one textbox and show show output in other textbox 【发布时间】:2019-10-07 12:37:31 【问题描述】:

我不知道如何在一个文本框中接受用户的输入并在另一个文本框中显示输出。

我试过了,但是没用。

from PyQt5 import QtCore, QtGui, QtWidgets
import os

def click(self):
    self.pushButton.click()
    print("hello")

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(664, 389)
        MainWindow.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
        MainWindow.setLayoutDirection(QtCore.Qt.LeftToRight)
        MainWindow.setAutoFillBackground(False)
        MainWindow.setStyleSheet("background-image: url(:/background/White-Marble-Background-product-Photography-backdrops-Newborn-Photo-props-baby-shower-PhotoCall-pictures-newborn-propsXt-6735.jpg_640x640.webp);")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(20, 20, 621, 51))
        font = QtGui.QFont()
        font.setFamily("Yu Gothic UI Semibold")
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.lineEdit.setFont(font)
        self.lineEdit.setAutoFillBackground(False)
        self.lineEdit.setObjectName("lineEdit")
        inp_val = self.lineEdit.text()

        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(240, 290, 151, 41))
        font = QtGui.QFont()
        font.setFamily("Rubik")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton.setFont(font)
        self.pushButton.setStyleSheet("background-color: rgb(240, 184, 160);")
        self.pushButton.setObjectName("pushButton")
        self.pushButton.clicked.connect(click(self))


        self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
        self.textEdit.setGeometry(QtCore.QRect(20, 90, 621, 191))
        font = QtGui.QFont()
        font.setFamily("Yu Gothic UI Semibold")
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.textEdit.setFont(font)
        self.textEdit.setCursorWidth(2)
        self.textEdit.setObjectName("textEdit")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Rahul"))
        self.pushButton.setText(_translate("MainWindow", "Run"))
        self.lineEdit.setPlaceholderText(_translate("MainWindow", "Type Your Text Here....."))
        self.textEdit.setPlaceholderText(_translate("MainWindow", "Your Output....."))
import test2_rc

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

如何在 lineedit 中输入并在 textedit 中显示输出?

【问题讨论】:

文本编辑是强制性的吗? 另外请只发布相关代码。你的代码很大。 【参考方案1】:

试试看:

from PyQt5 import QtCore, QtGui, QtWidgets
# ? import os

#def click(self):
#    self.pushButton.click()
#    print("hello")

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(664, 389)
        MainWindow.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
        MainWindow.setLayoutDirection(QtCore.Qt.LeftToRight)
        MainWindow.setAutoFillBackground(False)
        MainWindow.setStyleSheet("background-image: url(:/background/White-Marble-Background-product-Photography-backdrops-Newborn-Photo-props-baby-shower-PhotoCall-pictures-newborn-propsXt-6735.jpg_640x640.webp);")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")

        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(20, 20, 621, 51))
        font = QtGui.QFont()
        font.setFamily("Yu Gothic UI Semibold")
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.lineEdit.setFont(font)
        self.lineEdit.setAutoFillBackground(False)
        self.lineEdit.setObjectName("lineEdit")
        inp_val = self.lineEdit.text()

        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(240, 290, 151, 41))
        font = QtGui.QFont()
        font.setFamily("Rubik")
        font.setPointSize(12)
        font.setBold(True)
        font.setWeight(75)
        self.pushButton.setFont(font)
        self.pushButton.setStyleSheet("background-color: rgb(240, 184, 160);")
        self.pushButton.setObjectName("pushButton")
#        self.pushButton.clicked.connect(click(self))
        self.pushButton.clicked.connect(self.onClick)


        self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
        self.textEdit.setGeometry(QtCore.QRect(20, 90, 621, 191))
        font = QtGui.QFont()
        font.setFamily("Yu Gothic UI Semibold")
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.textEdit.setFont(font)
        self.textEdit.setCursorWidth(2)
        self.textEdit.setObjectName("textEdit")
        MainWindow.setCentralWidget(self.centralwidget)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def onClick(self):
#        self.pushButton.click()
        print("hello")
        self.textEdit.append(self.lineEdit.text())                   # <---


    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Rahul"))
        self.pushButton.setText(_translate("MainWindow", "Run"))
        self.lineEdit.setPlaceholderText(_translate("MainWindow", "Type Your Text Here....."))
        self.textEdit.setPlaceholderText(_translate("MainWindow", "Your Output....."))

# ? import test2_rc

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

【讨论】:

它完成了,但还有一件事我想在文本编辑框中打印输出,但它显示在 pycharm 输出区域中我不知道它的名称可能是 pycharm 代码的控制台...> def onClick (self): self.textEdit.append(os.system(self.lineEdit.text()))【参考方案2】:

使用setPlainText:

def click(self):
    inputtext = self.lineEdit.text()
    self.textEdit.setPlainText(inputtext)

您还需要将函数click 放入类Ui_MainWindow,如下所示:

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        #your code
    def click(self):
        inputtext = self.lineEdit.text()
        self.textEdit.setPlainText(inputtext)

同样调用函数clicklike:

self.pushButton.clicked.connect(self.click)

【讨论】:

以上是关于我不知道如何在一个文本框中输入并在另一个文本框中显示输出的主要内容,如果未能解决你的问题,请参考以下文章

在日期控件中提取日期数据,并在另一个文本框中显示其年份加1 |

根据在另一个文本框中键入的文本,使用 AJAX 更新面板在 ASP.NET 中自动填充文本框

将 Int 转换为分钟并在文本框中显示 swift 3

在启动 C# .NET 时将文本加载到文本框中

jQuery sum on change keyup() 并在文本框中输出

即使设置了焦点也无法在文本框中输入