I am creating the application, that requires typing some values from the screen keypad. I've already got buttons with numbers calling the function, that should update the number I would like to use. But every time the button is pressed a different decimal position should be filled. So I need two global variables: one for current number and other for current decimal position. But I don't know, where to put the definition of these two variables, so they were considered as globals and were able to be used inside of any function. My code looks as follows:
import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication, QFileDialog, QMessageBox
from PyQt5.QtCore import QTimer
from mainwindow import Ui_MainWindow
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, *args, obj=None, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setupUi(self)
self.kpadBtn0.clicked.connect(lambda:self.setValue(0))
self.kpadBtn1.clicked.connect(lambda:self.setValue(1))
self.kpadBtn2.clicked.connect(lambda:self.setValue(2))
self.kpadBtn3.clicked.connect(lambda:self.setValue(3))
self.kpadBtn4.clicked.connect(lambda:self.setValue(4))
self.kpadBtn5.clicked.connect(lambda:self.setValue(5))
self.kpadBtn6.clicked.connect(lambda:self.setValue(6))
self.kpadBtn7.clicked.connect(lambda:self.setValue(7))
self.kpadBtn8.clicked.connect(lambda:self.setValue(8))
self.kpadBtn9.clicked.connect(lambda:self.setValue(9))
disp_pos=1 #here it does not work
val=0
def setValue(self,num):
val+=num*disp_pos #it should be usable here
disp_pos*=10
self.newPosDisplay.setText(str(val))
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
When I press button the following error is shown:
NameError: name 'val' is not defined