0

I want to create a GUI where many buttons and QWidgets will be created later. Because this (I want to move it to single classes and this is where I have the problem that I can create a button but when I click on the button my code will not be executed.

import sys
from PyQt5 import QtWidgets

# Clas Mainwindow

class Window(QtWidgets.QWidget):

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

        self.init_ui()

    def init_ui(self):
        createb(self)
        self.show()

# class that should create the buttons with click action so that when I create 
# the class the button is created with click action on the main window

class createb():
    def __init__(self, mainwindow):
        mainwindow.b = QtWidgets.QPushButton('Push Me')
        mainwindow.l = QtWidgets.QLabel('I have not been clicked yet')

        h_box = QtWidgets.QHBoxLayout()
        h_box.addStretch()
        h_box.addWidget(mainwindow.l)
        h_box.addStretch()

        v_box = QtWidgets.QVBoxLayout()
        v_box.addWidget(mainwindow.b)
        v_box.addLayout(h_box)

        mainwindow.setLayout(v_box)
        mainwindow.setWindowTitle('PyQt5 Lesson 5')

        mainwindow.b.clicked.connect(self.btn_click)
    def btn_click(self):
            self.l.setText('I have been clicked')

app = QtWidgets.QApplication(sys.argv)
a_window = Window()
sys.exit(app.exec_())
4
  • Forgot the description: I want to create a GUI where many buttons and QWidgets will be created later. Because this (I want to move it to single classes and this is where I have the problem that I can create a button but when I click on the button my code will not be executed. Commented Sep 14, 2019 at 9:06
  • did you run it in console/terminal to see error messages ? I think you should get error because there is no self.l but mainwindow.l Commented Sep 14, 2019 at 9:15
  • no i didn't get an error message. If I replace self.l with mainwindow I get unresolved reference Commented Sep 14, 2019 at 9:25
  • because you should use self.mainwindow = mainwindow in __init__ to have access to self.mainwindow in other methods in createb Commented Sep 14, 2019 at 9:29

1 Answer 1

1

TRy it:

import sys
from PyQt5 import QtWidgets

# Clas Mainwindow

class Window(QtWidgets.QWidget):

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

        self.init_ui()

    def init_ui(self):
#        createb(self)
        self.btnClick = createb(self)     # +

        self.show()

# class that should create the buttons with click action so that when I create  
# the class the button is created with click action on the main window

class createb():
    def __init__(self, mainwindow):                     
        self.mainwindow = mainwindow                       # <--- +
        mainwindow.b = QtWidgets.QPushButton('Push Me')
        mainwindow.l = QtWidgets.QLabel('I have not been clicked yet')

        h_box = QtWidgets.QHBoxLayout()
        h_box.addStretch()
        h_box.addWidget(mainwindow.l)
        h_box.addStretch()

        v_box = QtWidgets.QVBoxLayout()
        v_box.addWidget(mainwindow.b)
        v_box.addLayout(h_box)

        mainwindow.setLayout(v_box)
        mainwindow.setWindowTitle('PyQt5 Lesson 5')

        mainwindow.b.clicked.connect(self.btn_click)

    def btn_click(self):
#            self.l.setText('I have been clicked')
            self.mainwindow.l.setText('I have been clicked')    # +

app = QtWidgets.QApplication(sys.argv)
a_window = Window()
sys.exit(app.exec_())

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.