0

I'm trying to display the distance between my two circles in the Tkinter window. I found an algorithm, but I'm having problem with displaying it. Here's my program so far:

from Tkinter import *
from math import *

def planetM(gd, hb):
    global x1, y1
    x1, y1 = x1+gd, y1+hb
    can1.coords(oval1,x1, y1, x1+30, y1+30)

def planetU(gd, hd):
    global z1, v1
    z1, v1 = z1+gd, v1+hd
    can1.coords(oval2, z1, v1, z1+30, v1+30)

def move_M_up():
    planetM(0, -10)
def move_U_up():
    planetU(0, -10)
def move_M_down():
    planetM(0, 10)
def move_U_down():
    planetU(0, 10)
def move_M_right():
    planetM(10, 0)
def move_U_right():
    planetU(10, 0)
def move_M_left():
    planetM(-10, 0)
def move_U_left():
    planetU(-10, 0)

def distance():
    dist = math.sqrt((x1-z1)**2 + (y1-v1)**2) -  30
    chain.configure(text='dist :' + str(dist))

x1, y1, z1, v1 = 10, 10, 260, 260

win1 = Tk()
win1.title("Two planets")

can1 = Canvas(win1, bg='black', height = 300, width=300)
oval1= can1.create_oval(x1,y1,x1+30,y1+30, width=2, fill='orange')
oval2= can1.create_oval(z1,v1,z1+30,v1+30, width=2, fill='blue')
can1.grid(row=1, column =1, rowspan = 9)
Button(win1, text='Exit', command= win1.quit).grid(row=1, column =2)
Button(win1, text='M left', command=move_M_left).grid(row=2, column=2)
Button(win1, text='M right', command=move_M_right).grid(row=3, column=2)
Button(win1, text='M down', command=move_M_down).grid(row=4, column=2)
Button(win1, text='M up', command=move_M_up).grid(row=5, column=2)
Button(win1, text='U left', command=move_U_left).grid(row=6, column=2)
Button(win1, text='U right', command=move_U_right).grid(row=7, column=2)
Button(win1, text='U down', command=move_U_down).grid(row=8, column=2)
Button(win1, text='U up', command=move_U_up).grid(row=9, column=2)
chain = Label(win1)
chain.grid(row = 10, column=1)

win1.mainloop()

I tried .bind but I couldn't find anything that constantly displays a number and changes it as the circles move around.

1
  • Don't use wildcard imports. Commented Jun 3, 2016 at 18:55

1 Answer 1

1

Use after method. Make it refresh the label every 0.1 seconds.

win1.after(100, distance)    # Number 100 represents time in milliseconds to wait before function distance is called
                             # This needs to be placed BEFORE mainloop.

Then make function distance call itself with same method:

def distance():
    dist = math.sqrt((x1-z1)**2 + (y1-v1)**2) -  30
    chain.configure(text='dist :' + str(dist))
    win1.after(100, distance)

NOTE: If you are importing from math like this: from math import * then you can't say: math.sqrt(). So either change the import to: import math or remove math.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.