Home > OS >  Tkinter typeError on method
Tkinter typeError on method

Time:10-17

I am trying to update a variable and have a button text update in

#called in main() to update water count every second
def waterupdater():
    #2700000
    global watercount
    waterlabel.config(text = watercount)
    waterlabel.after(1000, waterupdater, watercount   1)
    print(watercount)

but when I run the code I get

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\REDACTED\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:\Users\REDACTED\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 814, in callit
    func(*args)
TypeError: waterupdater() takes 0 positional arguments but 1 was given

Full code: (Note that waterupdater is called in the main() method, which im guessing is what is throwing the error. I tried to add 'self' as a variable on the waterupdater method and 'root' when called in main() method with no luck.)

# importing whole module
from tkinter import *
from tkinter import ttk
root = Tk()

#name of the program
label = ttk.Label(root, text = '.0.')
label.grid(row = 0, column = 0)
label.config(text = "Howdy Tkinter")
label.config(foreground = 'white', background = 'black', font = ('impact', 14))

#decrements water count by -1
def waterclick():
    global watercount
    watercount -= 1
    waterlabel.config(text = watercount)
    print(watercount)

#called in main() to update water count every second
def waterupdater():
    #2700000
    global watercount
    waterlabel.config(text = watercount)
    waterlabel.after(1000, waterupdater, watercount   1)
    print(watercount)


#declaring global var
watercount = 0

#water button
waterlabel = ttk.Button(root, text = watercount, command = waterclick)
waterlabel.grid(row = 0, column = 1, columnspan = 2)

def main():
    
    waterupdater()
    mainloop()

if __name__ == "__main__": main()

CodePudding user response:

Your problem is in this line:

waterlabel.after(1000, waterupdater, watercount   1)

Your waterupdater doesn't get any argument. If you want to add 1 water every second you could do:

# called in main() to update water count every second
def waterupdater():
    # 2700000
    global watercount
    waterlabel.config(text=watercount)
    watercount  = 1
    waterlabel.after(1000, waterupdater)
    print(watercount)
  • Related