简体   繁体   English

PyQt5 QLineEdit循环-已添加图像

[英]PyQt5 QLineEdit loop— image added

I want to make a P2P chat application. 我想制作一个P2P聊天应用程序。

So I have this sig / slot pieces together myself. 因此,我自己将这个信号/插槽部件放在一起。 What I need to achieve is that I want to enter text in the QLineEdit (named it send_box ) and display in the QTextedit (named it main_text ). 我需要实现的是,我想在QLineEdit输入文本(命名为send_box )并在QTextedit显示QTextedit (命名为main_text )。

在此处输入图片说明

self.send_box.returnPressed.connect(self.sendData)

and here is the function definition 这是函数定义

  def sendData(self):
        self.main_text.setText ('Hello World')

This works. 这可行。 But only send the "Hello World" to the QTextEdit when I press Enter key. 但是,当我按Enter键时,仅将"Hello World"发送到QTextEdit

What I need is to send the text from the send_box ( QLineEdit ). 我需要从send_boxQLineEdit )发送文本。

To get text from send_box and 要从send_box获取文本并

  • replace text in main_text 替换main_text文本

     self.main_text.setText( self.send_box.text() ) 
  • append to existing text in main_text 追加到main_text现有文本

     self.main_text.append( self.send_box.text() ) 

And then you can clear text in send_box 然后您可以清除send_box文本

self.send_box.clear()

See doc for Qt5 (it's similar for PyQt5 ): QTextEdit and QLineEdit 请参阅Qt5文档(与PyQt5相似): QTextEditQLineEdit

Full example 完整的例子

from PyQt5 import QtGui, QtWidgets
import sys

class MyWindow(QtWidgets.QWidget):

    def __init__(self):
        super().__init__()

        self.vbox = QtWidgets.QVBoxLayout()
        self.setLayout(self.vbox)

        self.vbox.addWidget(QtWidgets.QLabel(text='Input:'))
        self.linetext = QtWidgets.QLineEdit()
        self.vbox.addWidget(self.linetext)

        self.linetext.returnPressed.connect(self.on_press_enter)

        self.vbox.addWidget(QtWidgets.QLabel(text='Output:'))
        self.textedit = QtWidgets.QTextEdit()
        self.vbox.addWidget(self.textedit)

        self.show()

    def on_press_enter(self):
        # copy from LineText to TextEdit
        #self.textedit.setText(self.linetext.text())
        self.textedit.append(self.linetext.text())
        # clear LineText
        self.linetext.clear()

app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
app.exec()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM