我正在尝试使用after方法在python中制作一个模拟时钟,因此时钟可以每60秒更新一次时间,问题出在'def clock(self)'方法上,但我似乎无法解决它,这是我得到的错误
''' elif isinstance(cnfs,(type(None),str)): RecursionError:_ instancecheck 中超过了最大递归深度 ''''
这是我的代码,请帮助:
from tkinter import *
import tkinter as tk # python 3
from tkinter import ttk
from time import strftime
import math
class Tkinter(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, SecondPage):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
frame.winfo_toplevel().geometry("860x864")
frame.configure(bg='#000000')
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
self.cans = Canvas(self, bg='white', height=150, width=150)
self.cans.place(relx=0.5, y=170, anchor=tk.CENTER)
self.clock()
def clock(self):
hour = int(strftime('%H'))
min = int(strftime('%M'))
hangle = hour * 30
mainangle = hangle + (min / 60 * 30)
mangle = 90 - mainangle
rad = mangle * (math.pi / 180)
xlength = 75 * math.cos(rad)
ylength = 75 * math.sin(rad)
x = 75 + xlength
y = 75 - ylength
self.cans.create_line(75, 75, x, y, fill='red')
self.after(60000, self.clock())
class SecondPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
app = Tkinter()
app.mainloop()
非常感谢您的帮助。
答案 0 :(得分:2)
问题是clock
永不返回:您正在递归地调用 clock
,以便获取self.after
的参数。
您只想将self.clock
传递给after
,以便可以在60秒内调用 。
self.after(60000, self.clock)