Jan-17-2023, 05:47 PM
(This post was last modified: Jan-17-2023, 05:47 PM by deanhystad.)
The "global" word tells Python to look for a variable in the global scope, not the local scope.
This is meaningless, because this variable is already in the global scope.
This is even odder
Since all you have are global variables, this needs to use the global keyworkd:
Use class and instance variables instead of global variables. This example creates one of each. class_variable is a class variable and it is shared by all instances of the class, instance_variable is an instance variable and it is unique for each instance of the class.
This is meaningless, because this variable is already in the global scope.
import sys global pos1This is very odd.
class MainWindow(QtWidgets.QMainWindow): global pos2Normally you would declare pos2 to be a class variable. A class variable is like a global variable, but it is only global to instances of the class. Your code does not result in a class that has pos2 as an attribute. This is better shown in the example below:
global_variable = 1 class A: global global_variable global_variable = 2 class_variable = 3 print(global_variable, A.class_variable) print(A.global_variable)
Output:2 3
Traceback (most recent call last):
File "...", line 11, in <module>
print(A.global_variable)
AttributeError: type object 'A' has no attribute 'global_variable'. Did you mean: 'class_variable'?
As you can see, the global keyword told Python to not create a variable named "global_variable". When global_variable = 2 is assigned in the class, it sets the value of the global variable. Since the assignment is to a global variable, the assignment inside the class does not create a class variable. We can access A.class_variable, but we cannot access A.global_variable because it was never created.This is even odder
def __init__(self): super().__init__() global pos3 pos3 = 1Now you have code that assigns a global variable when instances of that class are created. The global variable does not exist until this happens, No instance variable is created. I cannot think of a reason for using "global" inside a class or a method. Python allows it, but it doesn't make any sense.
Since all you have are global variables, this needs to use the global keyworkd:
def keyPressEvent(self, event): global pos1, pos2, pos3 if type(event) == QtGui.QKeyEvent and (event.key() == QtCore.Qt.Key_Left or event.key() == QtCore.Qt.Key_Down) :This code will no longer crash. It is code that doesn't make any sense at all, but it will not crash.
Use class and instance variables instead of global variables. This example creates one of each. class_variable is a class variable and it is shared by all instances of the class, instance_variable is an instance variable and it is unique for each instance of the class.
class MainWindow: class_variable = 0 def __init__(self): super().__init__() self.instance_variable = 0 def keyPressEvent(self): self.__class__.class_variable += 1 self.instance_variable += 1 a = MainWindow() b = MainWindow() a.keyPressEvent() b.keyPressEvent() print(a.class_variable, a.instance_variable, b.instance_variable)
Output:2 1 1
Each time the keyPressEvent method gets called it increments the class and instance variable. Each instance was clicked once, so each instance_variable was incremented once, but since there were two clicks, the class_variable was incremented twice.