gpt4 book ai didi

python - PyQt5,我不需要连续打印,但它只改变 QLabel

转载 作者:行者123 更新时间:2023-12-01 08:13:35 26 4
gpt4 key购买 nike

我需要它不连续打印,而是只更改 QLabel,我不需要添加更多内容,只要您在行编辑中编写,它就应该替换现有文本。我需要它就像股票一样

这是代码:

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_())

最佳答案

您必须创建一个QLabel,将其设置在布局中,并且仅使用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_())

关于python - PyQt5,我不需要连续打印,但它只改变 QLabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55085421/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com