I am very new to pyQT5 and running into some basic issues. I am trying to increment a variable by 1 (a) and then print the answer to the terminal whenever I press the cancel button, but this causes it to crash. I am unsure of what is going on. Thanks if you can help, sorry if this seems very basic to some, still learning.
from PyQt5 import QtCore, QtGui, QtWidgets
import datetime
class Ui_MainWindow(object):
a=0
def on_click(self):
print("ButtonPressed")
def func_accept(self):
print("Displaying Time")
current_time = str(datetime.datetime.now().time())
ui.label1.setText(current_time)
def func_canceled(self):
print("Cancel")
ui.label1.setText("Canceled")
a=a+1
print(a)
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.button1 = QtWidgets.QDialogButtonBox(self.centralwidget)
self.button1.setGeometry(QtCore.QRect(270, 370, 156, 23))
self.button1.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.button1.setObjectName("button1")
self.label1 = QtWidgets.QLabel(self.centralwidget)
self.label1.setGeometry(QtCore.QRect(310, 250, 141, 61))
self.label1.setObjectName("label1")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.button1.clicked.connect(self.on_click)
self.button1.accepted.connect(self.func_accept)
self.button1.rejected.connect(self.func_canceled)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label1.setText(_translate("MainWindow", "Apples"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
Thanks!