更新Tkinter标签 [英] Update Tkinter Label

查看:69
本文介绍了更新Tkinter标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Python 3,并且对编程非常陌生.我编写了一个代码,该代码应该显示一个窗口,您可以在其中移动两个行星,并观察两个行星之间的引力. 一切正常,除了应该显示力的标签.经过多次尝试和搜索,我只是无法知道每次行星移动时如何对其进行更新.

I use Python 3 and I'm quite new to programming. I have written a code that's supposed to show a window in which you can move around two planets and see the gravitational force between them. Everything works excepted the label that's supposed to display the force. After multiple attempts and searches, I just can't find out how to update it every time a planet is being moved.

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

I guess my problem should be in the last line with :

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

我尝试使用StringVar和textvariable参数,但是我并没有真正了解它的概念.

I've tried to use a StringVar and a textvariable parameter but I didn't really get the concepts of it.

这是我的代码.我想答案很简单,但是我没有经验.

Here is my code. I guess the answer is easy but I'm quite unexperienced.

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()

推荐答案

好,需要做一些事情.首先,使您的标签像这样:

Ok, there is a few things that need to be done. First, make your label like this:

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像应该引用标签一样,而不是pack,该标签将返回None.

This will make lbl refer to the label like it should, not pack, which returns None.

第二,您需要输入以下内容:

Second, you will need to put this:

gravitation(oval1, oval2)

move函数的末尾,以便每次在任何方向上发生移动时,都会更新标签.

at the end of your move function so that, each time a movement in any direction takes place, it will update the label.

第三,而不是在gravitation中返回grav,将其放在末尾:

Third, instead of returning grav in gravitation, put this at the end:

lbl["text"] = grav

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

Now, each time gravitation is called, the label will be updated.

总而言之,这应该做您想要的:

All in all, this should do what you want:

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函数将覆盖此内容.希望这会有所帮助!

I put comment boxes around everything I changed. Also, I removed gravitation = 0 at the start since defining the gravitation function will overwrite this. Hope this helps!

这篇关于更新Tkinter标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆