I see a few problems
In the method
Your code outside
and would not work because
and at the time you called it there would be no button clicks
An event handler is not meant to return anything, the button click is what calls the method
I think what you are trying is when a button is clicked use some code outside of the class/instance
you can make a function
Here is the full code with the above changes made
class UiMainWindow(object): def __init__(self): self.clicked_button = [] self.main_window = main_windowThis is where error you posted is
NameError: name 'main_window' is not defined
main_window
has not been defined, I think that you mean for main_window
to be an attribute passed to the _init__ as shown belowclass UiMainWindow(object): def __init__(self, main_window): self.clicked_button = [] self.main_window = main_windowonce changing that in the following
if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) main_window = QtWidgets.QMainWindow() ui = UiMainWindow() ui.setupUi(main_window) main_window.show() sys.exit(app.exec_())
ui = UiMainWindow()
would need to be passing main_window
as ui = UiMainWindow(main_window)
In the method
def button_clicked(self): button_ = main_window.sender().text() self.clicked_button.append(button_) return self.clicked_button
button_ = main_window.sender().text()
is missing self
before main_window
Your code outside
input_list = UiMainWindow().button_clicked() print(input_list)would need to pass in a
main_window
and would not work because
Error:QWidget: Must construct a QApplication before a QWidget
This would also not be the same instance of the window created in the if __name__ == "__main__":
and at the time you called it there would be no button clicks
An event handler is not meant to return anything, the button click is what calls the method
def button_clicked(self):
so there is nowhere to return to.I think what you are trying is when a button is clicked use some code outside of the class/instance
you can make a function
def on_button_clicked(input_list): print(input_list)and call it at the end of the button click handler
def button_clicked(self): button_ = self.main_window.sender().text() self.clicked_button.append(button_) on_button_clicked(self.clicked_button)
Here is the full code with the above changes made
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'C:\Users\hp\Desktop\PythonLearning\PyQt\Hang\Test_UI_two_buttons.ui' # # Created by: PyQt5 UI code generator 5.14.2 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class UiMainWindow(object): def __init__(self, main_window): self.clicked_button = [] self.main_window = main_window self.centralwidget = QtWidgets.QWidget(main_window) self.A_Button = QtWidgets.QPushButton(self.centralwidget) self.B_Button = QtWidgets.QPushButton(self.centralwidget) self.menubar = QtWidgets.QMenuBar(main_window) self.statusbar = QtWidgets.QStatusBar(main_window) def setupUi(self, _main__window_): _main__window_.setObjectName("MainWindow") _main__window_.setEnabled(True) _main__window_.resize(863, 600) font = QtGui.QFont() font.setFamily("MS Sans Serif") _main__window_.setFont(font) _main__window_.setWindowOpacity(1) self.centralwidget.setObjectName("centralwidget") self.A_Button.setGeometry(QtCore.QRect(143, 210, 251, 111)) font = QtGui.QFont() font.setFamily("Modern") font.setPointSize(10) font.setBold(True) font.setItalic(True) font.setWeight(75) self.A_Button.setFont(font) self.A_Button.setAutoFillBackground(False) self.A_Button.setCheckable(False) self.A_Button.setObjectName("A_Button") self.B_Button.setGeometry(QtCore.QRect(400, 210, 251, 111)) font = QtGui.QFont() font.setFamily("Modern") font.setPointSize(10) font.setBold(True) font.setItalic(True) font.setWeight(75) self.B_Button.setFont(font) self.B_Button.setObjectName("B_Button") _main__window_.setCentralWidget(self.centralwidget) self.menubar.setGeometry(QtCore.QRect(0, 0, 863, 21)) self.menubar.setObjectName("menubar") _main__window_.setMenuBar(self.menubar) self.statusbar.setObjectName("statusbar") _main__window_.setStatusBar(self.statusbar) self.retranslateUi(_main__window_) QtCore.QMetaObject.connectSlotsByName(_main__window_) def retranslateUi(self, main_window_): _translate = QtCore.QCoreApplication.translate main_window_.setWindowTitle(_translate("MainWindow", "MainWindow")) self.A_Button.setText(_translate("MainWindow", "A")) self.A_Button.setShortcut(_translate("MainWindow", "A")) self.B_Button.setText(_translate("MainWindow", "B")) self.B_Button.setShortcut(_translate("MainWindow", "B")) self.A_Button.clicked.connect(self.button_clicked) self.B_Button.clicked.connect(self.button_clicked) def button_clicked(self): button_ = self.main_window.sender().text() self.clicked_button.append(button_) on_button_clicked(self.clicked_button) def on_button_clicked(input_list): print(input_list) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) main_window = QtWidgets.QMainWindow() ui = UiMainWindow(main_window) ui.setupUi(main_window) main_window.show() sys.exit(app.exec_())Hopefully, I'm somewhere on track with what you are looking for