Python 方法内的For循环:TypeError位置参数
我正在尝试使用类创建一个带有tkinter的简单Gui。 但是我真的不明白如何使for循环在Python 方法内的For循环:TypeError位置参数,python,class,for-loop,tkinter,typeerror,Python,Class,For Loop,Tkinter,Typeerror,我正在尝试使用类创建一个带有tkinter的简单Gui。 但是我真的不明白如何使for循环在count方法中工作,有人能告诉我应该在哪里添加缺少的参数吗 from tkinter import * import time class App: def __init__(self, master): self.container1 = Frame(master) self.container1.pack() self.button1 =
count
方法中工作,有人能告诉我应该在哪里添加缺少的参数吗
from tkinter import *
import time
class App:
def __init__(self, master):
self.container1 = Frame(master)
self.container1.pack()
self.button1 = Button(self.container1, text="count")
self.button1.bind("<Button-1>", self.count)
self.button1.pack()
def count(self):
for i in range(100):
self.button1["text"] = str(i)
time.sleep(1)
root = Tk()
Myapp = App(root)
root.mainloop()
绑定事件时,会向回调函数提供位置参数
event
将您的计数方法更改为:
def count(self, event):
您还需要去掉时间。sleep(1)
,因为.sleep()
是一个阻塞调用,这意味着它将阻塞tkinter main循环,这将导致程序不响应。绑定事件时,会向回调函数提供一个位置参数event
将您的计数方法更改为:
def count(self, event):
您还需要去掉时间。sleep(1)
,因为.sleep()
是一个阻塞调用,这意味着它将阻塞tkinter main循环,这将导致您的程序不响应。因此,每当我在有变量的函数中定义函数时,我都必须使用事件作为位置参数?如果不止一个呢?还有,你能给我建议一种替代时间的方法吗?感谢您的帮助。由于tkinter的事件绑定(当您编写self.button1.bind(“…”,self.count)
时)自动将事件参数传递给回调函数,因此您的函数必须将其命名为event
,它可以是这样的def count(self),
。要替换time.sleep(),请尝试搜索tkinter的root.after()方法,如果您仍然有问题,我可以在有时间时编写解决方案。因此,每当我在其中定义一个变量的函数时,我都必须使用事件作为位置参数?如果不止一个呢?还有,你能给我建议一种替代时间的方法吗?感谢您的帮助。由于tkinter的事件绑定(当您编写self.button1.bind(“…”,self.count)
时)自动将事件参数传递给回调函数,因此您的函数必须将其命名为event
,它可以是这样的def count(self),
。要替换time.sleep(),请尝试搜索tkinter的root.after()方法,如果您仍然有问题,我可以在有时间时编写解决方案。