【问题标题】:Tkinter Label not updating correctlyTkinter 标签未正确更新
【发布时间】:2013-09-02 20:20:53
【问题描述】:

今天下午我已经问了一个关于如何在 Tkinter 中更新标签的问题,得到了一个有效的答案。 但是,我发现“解决方案”代码有点长,并试图对其进行改进。 基本上我正在使用updt 函数,它在标签lbl 上执行配置method。该方法将通过调用gravitation 函数来更改标签文本。 每次点击某处时,我都使用整个窗口的bind方法调用updt函数。

现在的问题是程序无法正常运行,因为标签中显示的数字不正确,并且即使行星与以前的距离相同,也不总是显示相同的值。

我仔细阅读了每一行,但由于我对编程很陌生,所以我找不到问题所在。

我正在使用 Python 3。

这是我之前的代码:Update Tkinter Label

这是新的:

from tkinter import *
import math

x, y = 135, 135

def gravitation (obj1,obj2):
    a, b, c, d = can.coords (obj1)
    e, f, g, h = can.coords (obj2)
    dist = math.sqrt ((((a+c)/2)-((e+g)/2))**2+(((b+d)/2)-((f+h)/2))**2)
    if dist != 0:
        grav = 6.67384/dist
    else:
        grav = "Infinite"
    str(grav)
    return grav

def updt (event):
    lbl.configure (text = gravitation(oval1, oval2)) 

def move (ov, lr, tb): # function to move the ball
    coo = can.coords(ov)
    coo[0] = coo[0] + lr
    coo[1] = coo[1] + tb
    coo[2] = coo[0]+30
    coo[3] = coo[1]+30
    can.coords(ov, *coo)


def moveLeft ():
    move(oval1, -10, 0)


def moveRight ():
    move(oval1, 10, 0)


def moveTop ():
    move(oval1, 0, -10)


def moveBottom ():
    move(oval1, 0, 10)


def moveLeft2 ():
    move(oval2, -10, 0)


def moveRight2 ():
    move(oval2, 10, 0)


def moveTop2 ():
    move(oval2, 0, -10)


def moveBottom2 ():
    move(oval2, 0, 10)



##########MAIN############

wind = Tk() # Window and canvas
wind.title ("Move Da Ball")
can = Canvas (wind, width = 300, height = 300, bg = "light blue")
can.grid(row=0, column=1, sticky =W, padx = 5, pady = 5, rowspan =4)
Button(wind, text = 'Quit', command=wind.destroy).grid(row=5, column=1, sticky =W, padx = 5, pady = 5)
wind.bind ("<Button-1>", updt)


oval1 = can.create_oval(x,y,x+30,y+30,width=2,fill='orange') #Planet 1 moving etc
Button(wind, text = 'Left', command=moveLeft).grid(row=0, column=2, sticky =W, padx = 5, pady = 5)
Button(wind, text = 'Right', command=moveRight).grid(row=1, column=2, sticky =W, padx = 5, pady = 5)
Button(wind, text = 'Top', command=moveTop).grid(row=2, column=2, sticky =W, padx = 5, pady = 5)
Button(wind, text = 'Bottom', command=moveBottom).grid(row=3, column=2, sticky =W, padx = 5, pady = 5)



oval2 = can.create_oval(x+50,y+50,x+80,y+80,width=2,fill='blue') #Planet 2 moving etc
Button(wind, text = 'Left', command=moveLeft2).grid(row=0, column=3, sticky =W, padx = 5, pady = 5)
Button(wind, text = 'Right', command=moveRight2).grid(row=1, column=3, sticky =W, padx = 5, pady = 5)
Button(wind, text = 'Top', command=moveTop2).grid(row=2, column=3, sticky =W, padx = 5, pady = 5)
Button(wind, text = 'Bottom', command=moveBottom2).grid(row=3, column=3, sticky =W, padx = 5, pady = 5)




lbl = Label(wind, bg = 'white')#label
lbl.grid(row=4, column=1, sticky =W, padx = 5, pady = 5, columnspan = 3)
gravitation (oval1, oval2)


wind.mainloop()

【问题讨论】:

    标签: python tkinter label


    【解决方案1】:

    updt 在用户单击窗口的任何部分时触发,移动功能在用户单击按钮时触发,然后释放它。因此,updt 总是在 move 函数之前被调用。所以标签报告了行星在移动到新位置之前相互之间的引力。

    不要将updt绑定到全局点击事件,而是在坐标改变后将其放在move函数中。

    def updt():
        lbl.configure (text = gravitation(oval1, oval2)) 
    
    def move (ov, lr, tb): # function to move the ball
        coo = can.coords(ov)
        coo[0] = coo[0] + lr
        coo[1] = coo[1] + tb
        coo[2] = coo[0]+30
        coo[3] = coo[1]+30
        can.coords(ov, *coo)
        updt()
    

    【讨论】:

    • 是的,实际上就是这样做的,正如我们在我链接的帖子中发现的那样。在这种情况下,唯一的区别是我们使用一种方法,而在另一种方法中我们使用 'lbl["text"] = grav'。但是您的解释非常有帮助,因为我明白为什么它不能那样工作。谢谢。
    猜你喜欢
    • 2016-12-23
    • 1970-01-01
    • 2017-11-18
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-12
    相关资源
    最近更新 更多