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

self.lbutmainwindow.lself.mainwindow = mainwindowin__init__to have access toself.mainwindowin other methods increateb