【问题标题】:Update Tkinter Label更新 Tkinter 标签
【发布时间】:2013-09-02 15:54:38
【问题描述】:

我使用 Python 3,而且我对编程很陌生。我编写了一个代码,它应该显示一个窗口,您可以在其中围绕两个行星移动并查看它们之间的引力。 一切正常,除了应该显示力的标签。经过多次尝试和搜索,我无法找到每次移动行星时如何更新它。

我想我的问题应该在最后一行:

lbl = Label(wind, bg = 'white', text = gravitation(oval1, oval2)).pack(padx=5, pady=5)

我尝试过使用 StringVar 和 textvariable 参数,但我并没有真正理解它的概念。

这是我的代码。我想答案很简单,但我很缺乏经验。

from tkinter import *
import math

x, y = 135, 135
gravitation = 0

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)
    grav = 6.67384/dist
    return grav

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.pack (side = LEFT,padx = 5, pady = 5)
Button(wind, text = 'Quit', command=wind.destroy).pack(padx = 5, pady = 5)

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

oval2 = can.create_oval(x+50,y+50,x+80,y+80,width=2,fill='orange') #Planet 2 moving etc
Button(wind, text = 'Left', command=moveLeft2).pack(padx = 5, pady = 5)
Button(wind, text = 'Right', command=moveRight2).pack(padx = 5, pady = 5)
Button(wind, text = 'Top', command=moveTop2).pack(padx = 5, pady = 5)
Button(wind, text = 'Bottom', command=moveBottom2).pack(padx = 5, pady = 5)


lbl = Label(wind, bg = 'white', text = gravitation(oval1, oval2)).pack(padx=5, pady=5)

wind.mainloop()

【问题讨论】:

    标签: python tkinter label


    【解决方案1】:

    好的,有几件事需要做。首先,让你的标签是这样的:

    lbl = Label(wind, bg = 'white', text = gravitation(oval1, oval2))
    lbl.pack(padx=5, pady=5)
    # Put this on its own line so that there are no errors since 'lbl' isn't defined yet
    gravitation(oval1, oval2)
    

    这将使lbl 按应有的方式引用标签,而不是返回None 的pack。

    其次,你需要把这个:

    gravitation(oval1, oval2)
    

    在move 函数的末尾,这样,每次在任何方向发生移动时,它都会更新标签。

    第三,不要在gravitation中返回grav,而是把这个放在最后:

    lbl["text"] = grav
    

    现在,每次调用gravitation,标签都会更新。

    总而言之,这应该可以满足您的需求:

    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)
        grav = 6.67384/dist
        ##################
        lbl["text"] = grav
        ##################
    
    def move (ov, lr, tb):
        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)
        ########################
        gravitation(oval1, oval2)
        ########################
    
    
    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)
    
    
    
    wind = Tk()
    wind.title ("Move Da Ball")
    can = Canvas (wind, width = 300, height = 300, bg = "light blue")
    can.pack (side = LEFT,padx = 5, pady = 5)
    Button(wind, text = 'Quit', command=wind.destroy).pack(padx = 5, pady = 5)
    
    oval1 = can.create_oval(x,y,x+30,y+30,width=2,fill='orange') #Planet 1 moving etc
    Button(wind, text = 'Left', command=moveLeft).pack(padx = 5, pady = 5)
    Button(wind, text = 'Right', command=moveRight).pack(padx = 5, pady = 5)
    Button(wind, text = 'Top', command=moveTop).pack(padx = 5, pady = 5)
    Button(wind, text = 'Bottom', command=moveBottom).pack(padx = 5, pady = 5)
    
    oval2 = can.create_oval(x+50,y+50,x+80,y+80,width=2,fill='orange') #Planet 2 moving etc
    Button(wind, text = 'Left', command=moveLeft2).pack(padx = 5, pady = 5)
    Button(wind, text = 'Right', command=moveRight2).pack(padx = 5, pady = 5)
    Button(wind, text = 'Top', command=moveTop2).pack(padx = 5, pady = 5)
    Button(wind, text = 'Bottom', command=moveBottom2).pack(padx = 5, pady = 5)
    
    ###############################
    lbl = Label(wind, bg = 'white')
    lbl.pack(padx=5, pady=5)
    gravitation(oval1, oval2)
    ##############################
    
    wind.mainloop()
    

    我在我更改的所有内容周围都放置了评论框。另外,我在开始时删除了gravitation = 0,因为定义gravitation 函数将覆盖它。希望这会有所帮助!

    【讨论】:

    • 嘿。谢谢您的帮助。实际上,我希望有另一种方法可以做到这一点,但是这个方法有效。我再等一会儿,看看有人有没有想法,否则我会把它标记为答案。
    • @Pierreocinom:你不喜欢这个解决方案的什么地方?
    • 我在编程方面不是很有经验,所以我可能不知道我不喜欢解决方案,因为我自己没有任何线索。但是如果要我提一下,那就是我需要在每个移动函数中调用引力函数。如果我可以这么说,我宁愿让我的代码更通用。我只是在做别的事情,它几乎可以工作,除了一个小问题。我只是在写一个新问题,你可以看看。
    • @PierreOcinom - 我只是想到了一些事情(不知道为什么我没有早点)。在阅读了您的上述评论后,我开始考虑一种无需在每个移动函数中调用 gravitation 即可完成工作的方法。我的解决方案是将gravitation 的调用放在move 函数中。请参阅我的编辑。这消除了所有这些时间调用gravitation 的需要。
    • 哈哈哈。确实,这很容易,即使我应该看到它。非常感谢。
    猜你喜欢
    • 2017-04-12
    • 2014-08-01
    • 2017-11-18
    • 2015-12-08
    • 2015-05-24
    • 2018-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多