I'm trying to make an ordinary click counter with a GUI based on the Tkinter library. Code like this:

count = 0 from tkinter import * from tkinter.ttk import * def printer(event): #ΠŸΡ€ΠΈ ΠΊΠ»ΠΈΠΊΠ΅ count += 1 #Π‘ΠΎΠ±Ρ‹Ρ‚ΠΈΠ΅ self = Tk() button = Button(self, text='CLICK ME PLS') button.pack() lab = Label(self, text= count, font="Tahoma") lab.pack() button.bind('<Button-1>', printer) self.mainloop() 

The program swears at:

 count += 1 

And writes:

 Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\ElPI-25\AppData\Local\Programs\Python\Python36- 32\lib\tkinter\__init__.py", line 1699, in __call__ return self.func(*args) File "D:/Ρ€Π°Π±ΠΎΡ‡ΠΈΠΉ стол/ΠŸΡ€ΠΎΠ΅ΠΊΡ‚Ρ‹ АрсСния/TK.py", line 9, in printer count += 1 UnboundLocalError: local variable 'count' referenced before assignment 

Urgently help, please.

    1 answer 1

    Sorry, but I already decided:

     from tkinter import * from tkinter.ttk import * def printer(event): #ΠŸΡ€ΠΈ ΠΊΠ»ΠΈΠΊΠ΅ global count #Π‘ΠΎΠ±Ρ‹Ρ‚ΠΈΠ΅ count += 1 lab.config(text= count) self = Tk() button = Button(self, text='CLICK ME PLS') button.pack() lab = Label(self, text= 'imho', font="Tahoma") lab.pack() button.bind('<Button-1>', printer) self.mainloop() 
    • You replaced UnboundLocalError with NameError . - jfs