0

Пытаюсь сделать обыкновенный счётчик кликов с GUI на основе библиотеки Tkinter. Код примерно такой:

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()

Программа ругается на:

count += 1

И пишет:

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

Срочно помогите, пожалуйста.

1 ответ 1

0

Извините, но я уже решил:

from tkinter import *
from tkinter.ttk import *

count = 0


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()

 
1
  • Вы заменили UnboundLocalError на NameError.
    – jfs
    14 окт 2017 в 6:20

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.