I've read a view posts and articles about global variables so I don't understand why it doesn't work in my app.
In the code below pos1, pos2 and pos2 create an error at keyPressEvent: "local variable 'pos...' referenced before assignment".
Why? And how can I do it right?
pos is an index that I want to increase/decrease at keypress.
In the code below pos1, pos2 and pos2 create an error at keyPressEvent: "local variable 'pos...' referenced before assignment".
Why? And how can I do it right?
pos is an index that I want to increase/decrease at keypress.
import sys
global pos1
class MainWindow(QtWidgets.QMainWindow):
global pos2
pos2 = 1
pos1 = 1
def __init__(self):
super().__init__()
global pos3
pos3 = 1
# main window
self.setWindowTitle("Uhren II")
self.setFixedSize(500, 500)
self.setWindowIcon(QIcon("Uhren2.ico"))
# ...
def keyPressEvent(self, event):
if type(event) == QtGui.QKeyEvent and (event.key() == QtCore.Qt.Key_Left or event.key() == QtCore.Qt.Key_Down) :
pos1 -= 1
pos2 -= 1
pos3 -= 1
if type(event) == QtGui.QKeyEvent and (event.key() == QtCore.Qt.Key_Right or event.key() == QtCore.Qt.Key_Up) :
pos1 += 1
pos2 += 1
pos3 += 1
print(pos1, pos2, pos3)
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()
