0

I'm trying to capture Text with a click on a QPushButton and Display it in a QLabel with pyqt5

I really new to this stuff so go easy on me !

here is the code I have so far:

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

I keep getting this error:

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

I am not sure why game_line_edit is not defined but have a feeling it's because it not is the same "class" as my on_click class but am not sure

any help would be appreciated

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
littlejiver
  • 255
  • 2
  • 13
  • Possible duplicate of [What is the purpose of self?](https://stackoverflow.com/questions/2709821/what-is-the-purpose-of-self) – eyllanesc Oct 07 '18 at 19:30

1 Answers1

0
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_())
littlejiver
  • 255
  • 2
  • 13