我正在嘗試更新一個變數并有一個按鈕文本更新
#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)
但是當我運行代碼時,我得到
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
完整代碼:(請注意,在 main() 方法中呼叫了 waterupdater,我猜這是引發錯誤的原因。我嘗試在 waterupdater 方法上添加“self”作為變數,并在 main() 中呼叫時添加“root”沒有運氣的方法。)
# 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()
uj5u.com熱心網友回復:
你的問題在這一行:
waterlabel.after(1000, waterupdater, watercount 1)
你waterupdater沒有任何論據。如果你想每秒加 1 水,你可以這樣做:
# 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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/323747.html
上一篇:使用帶有self的變數呼叫函式
