Tkinter:如何使Tkinter刷新和删除最后一行



问题是:Tkinter中的时钟指针闪烁,因为我使用w.delete,但如果我不使用它,则时钟指针重复。。请帮忙。

import Tkinter as tk; import time
from math import cos,sin,pi
import sys
root=tk.Tk(); root.title("Clock")
w = tk.Canvas(root, width=320, height=320, bg="#456", relief= "sunken", border=10)
w.pack()
size=300
def funA():
    s=time.localtime()[5]
    m=time.localtime()[4]
    h=time.localtime()[3]
    w.update()
        degrees = 6*s
    angle = degrees*pi*2/360
    ox = 165
    oy = 165
    x = ox + size*sin(angle)*0.45
    y = oy - size*cos(angle)*0.45
    t = w.create_line(ox,oy,x,y, fill = "#ffc")
    degrees1 = 6*m
    angle1 = degrees1*pi*2/360
    ox1 = 165
    oy1 = 165
    x1 = ox1 + size*sin(angle1)*0.4
    y1 = oy1 - size*cos(angle1)*0.4
    t1 = w.create_line(ox1,oy1,x1,y1, fill = "Red", width=6)

    degrees2 = 30*h
    angle2 = degrees2*pi*2/360
    ox2 = 165
    oy2 = 165
    x2 = ox2 + size*sin(angle2)*0.2
    y2 = oy2 - size*cos(angle2)*0.2
    t2 = w.create_line(ox2,oy2,x2,y2, fill = "Black", width=8)
    w.update()
    root.after(200,funA)
    w.delete(t1)
root.after(1500, funA)
uzr1 = tk.Label(root, text="12", bg="#456" )
uzr1.place(x=160, y=13)
uzr2 = tk.Label(root, text="6", bg="#456" )
uzr2.place(x=160, y=303)
uzr3 = tk.Label(root, text="3", bg="#456" )
uzr3.place(x=310, y=160)
uzr4 = tk.Label(root, text="9", bg="#456" )
uzr4.place(x=11, y=160)
def Quit():
    root.after(700,root.destroy())
e = tk.Button(root,text="Quit", command=Quit)
e.pack()
root.mainloop()

如果我在funAw.delete(t)中写入,则二手时钟正在闪烁,如果不是,则其正在复制。。所有的钟表指针都是如此。。

  • t为二手
  • t1为分针
  • t2是时针

希望收到快速有用的回复

这里有一些事情需要修复。首先,您不需要调用update。一般来说,你永远不应该称之为它,尽管一旦你理解了为什么存在这个规则,这个规则就会被打破。呼叫update_idletasks是安全的,如果您觉得有必要呼叫update,这通常是您想要的。即使这样,只要设计得当也可以避免。

其次,不需要删除行并创建新行。画布小部件有一个名为coords的方法,可用于调整项目的坐标。

第三,您可以利用画布的标记功能。给每个时钟指针一个合适名称的标签(例如:"秒"、"分钟"、"小时"),然后用它告诉画布要更新哪个项目。

最后,养成创建应用程序类或将应用程序代码放入函数的习惯。它可以更容易地解决在定义函数之前无法调用函数的问题,并且不必使用全局变量。

下面是一个工作示例:

import Tkinter as tk; import time
from math import cos,sin,pi
import sys
class MyApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.size=300
        self.title("Clock")
        self.w = tk.Canvas(self, width=320, height=320, bg="#456", relief= "sunken", border=10)
        self.w.pack()
        self.w.create_line(0,0,0,0, fill="#ffc", tags="hour")
        self.w.create_line(0,0,0,0, fill="red", tags="minute")
        self.w.create_line(0,0,0,0, fill="black", tags="second")
        uzr1 = tk.Label(self, text="12", bg="#456" )
        uzr1.place(x=160, y=13)
        uzr2 = tk.Label(self, text="6", bg="#456" )
        uzr2.place(x=160, y=303)
        uzr3 = tk.Label(self, text="3", bg="#456" )
        uzr3.place(x=310, y=160)
        uzr4 = tk.Label(self, text="9", bg="#456" )
        uzr4.place(x=11, y=160)
        e = tk.Button(self,text="Quit", command=self.Quit)
        e.pack()
        self.update_clock()
    def update_clock(self):
        s=time.localtime()[5]
        m=time.localtime()[4]
        h=time.localtime()[3]
        degrees = 6*s
        angle = degrees*pi*2/360
        ox = 165
        oy = 165
        x = ox + self.size*sin(angle)*0.45
        y = oy - self.size*cos(angle)*0.45
        self.w.coords("hour", (ox,oy,x,y))
        degrees1 = 6*m
        angle1 = degrees1*pi*2/360
        ox1 = 165
        oy1 = 165
        x1 = ox1 + self.size*sin(angle1)*0.4
        y1 = oy1 - self.size*cos(angle1)*0.4
        self.w.coords("minute", (ox1,oy1,x1,y1))
        degrees2 = 30*h
        angle2 = degrees2*pi*2/360
        ox2 = 165
        oy2 = 165
        x2 = ox2 + self.size*sin(angle2)*0.2
        y2 = oy2 - self.size*cos(angle2)*0.2
        self.w.coords("second",(ox2,oy2,x2,y2))
        self.after(1000, self.update_clock)
    def Quit(self):
        self.after(700,self.destroy())
app = MyApp()
app.mainloop()

相关内容

  • 没有找到相关文章

最新更新