简体   繁体   中英

PyQt5, I need not to print continuously but instead it only changes the QLabel

I need it to be not continuously printing but instead it only change the QLabel, I dont need to add more, just whenever you write in Line edit it should replace the existing text. I need it like a stocks

This is the code:

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton, QLabel, QLineEdit
from PyQt5.QtCore import pyqtSlot

class Window(QWidget):

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

        self.initUI()

    def initUI(self):

        self.hbox = QHBoxLayout()

        self.game_name = QLabel("Stocks:", self)

        self.game_line_edit = QLineEdit(self)

        self.search_button = QPushButton("Print", self)

        self.search_button.clicked.connect(self.on_click)

        self.hbox.addWidget(self.game_name)
        self.hbox.addWidget(self.game_line_edit)
        self.hbox.addWidget(self.search_button)

        self.setLayout(self.hbox)

        self.show()

    @pyqtSlot()
    def on_click(self):
        game = QLabel(self.game_line_edit.text(), self)
        self.hbox.addWidget(game)


if __name__ == "__main__":

    app = QApplication(sys.argv)
    win = Window()
    sys.exit(app.exec_())

You have to create a QLabel , set it in the layout and only update the text with setText() :

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton, QLabel, QLineEdit
from PyQt5.QtCore import pyqtSlot

class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):
        self.game_name = QLabel("Stocks:")
        self.game_line_edit = QLineEdit()
        self.search_button = QPushButton("Print")
        self.search_button.clicked.connect(self.on_click)
        self.game = QLabel()

        hbox = QHBoxLayout(self)        
        hbox.addWidget(self.game_name)
        hbox.addWidget(self.game_line_edit)
        hbox.addWidget(self.search_button)
        hbox.addWidget(self.game)
        self.show()

    @pyqtSlot()
    def on_click(self):
        self.game.setText(self.game_line_edit.text())

if __name__ == "__main__":

    app = QApplication(sys.argv)
    win = Window()
    sys.exit(app.exec_())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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