Home > Software engineering >  TypeError: counter_label() missing 1 required positional argument: 'label'
TypeError: counter_label() missing 1 required positional argument: 'label'

Time:11-22

I can count number but i can't countdown on label tkinter

Exception in Tkinter callback Traceback (most recent call last): File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\tkinter_init_.py", line 1921, in call return self.func(*args) File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\tkinter_init_.py", line 839, in callit
func(*args) TypeError: counter_label() missing 1 required positional argument:'label'

import tkinter as tk
counter = 0 
status = 1
def counter_label(label):
    global status
    def countup():
        global counter
        counter  = 1
        
        label.after(1000, countup)
    
    def countdown():
        global counter
        counter -=1
        # for num in range(start,0,-1):
        #     print(num)
        
        label.after(1000,countdown)
    
    
    if status == 1:
        countup()
                
    if counter == 11:
        status = 0
                
    if status == 0:
        countdown()
                
    if counter == 0:
        status = 1 
    label.config(text=str(counter))

    label.after(1000,counter_label)


print(counter)

root = tk.Tk()
root.title("Counting Seconds")
label = tk.Label(root, fg="green")
label.pack()
counter_label(label)

root.mainloop()

CodePudding user response:

your recursive call

label.after(1000,counter_label)

can be modified to include label argument with an anonymous function

label.after(1000,lambda x=label: counter_label(x))
  • Related