繁体   English   中英

使用QLineEdit,QPushButton捕获文本并使用QLabel pypt5显示该文本

[英]Capturing Text with QLineEdit, QPushButton and displaying that text with QLabel pypt5

我正在尝试通过单击QPushButton捕获文本并将其显示在带有pyqt5的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):

        hbox = QHBoxLayout()

        game_name = QLabel("Game Name:", self)

        game_line_edit = QLineEdit(self)

        search_button = QPushButton("Search", self)

        search_button.clicked.connect(self.on_click)

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

        self.setLayout(hbox)

        self.show()

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



if __name__ == '__main__':

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

我不断收到此错误:

game = QLabel(game_line_edit.text(), self)
NameError: name 'game_line_edit' is not defined

我不确定为什么未定义game_line_edit,但有种感觉,因为它与我的on_click类不是同一“类”,但不确定

任何帮助,将不胜感激

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("Game Name:", self)

        self.game_line_edit = QLineEdit(self)

        self.search_button = QPushButton("Search", 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_())

暂无
暂无

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

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