How do i refresh the label in python with Tkinter
There are two issues with your code. First, you have a typo in addcoin()
, the augmented addition operator is +=
, not =+
. Your code is assigning the value +1
every time.
Second, the coin
variable you defined is a global one (defined at the toplevel, outside any 'def' scope), but when you try to access coin
inside theaddcoin()
function, python assumes you want a local variable, and it willcomplain that it's being referenced before assignment. To tell python that youwant the global variable, use the global
statement.
You can change you addcoin function like this:
def addcoin(): global coin coin += 1 label.config(text=coin)