简体   繁体   中英

Python//tkinter label update data

i'm doing an exercise to learn python. Basically i'm supposed to create 2 circles using tkinter and calculate the distance between them while i'm moving them using buttons. The 'change' button is to swap circles. my problem is that the distance shows when i first launch the code, but it doesn't update when i'm moving the circles( and it should) to show the distance on screen i use a label class. code below:

from tkinter import *
from math import sqrt
# procédure générale de déplacement :
def avance(lat1=0, vert1=0,lat2=0,vert2=0):
     global x1, y1,x2,y2,dist
     x1, y1 = x1 +lat1, y1 +vert1
     x2, y2 = x2 +lat2, y2 +vert2
     if flag==0:
        can1.coords(oval1, x1,y1, x1+60,y1+60)
     else:
        can1.coords(oval2, x2, y2, x2 + 30, y2 + 30)

     dist=calculdist(x1,y1,x2,y2)
     return dist

def calculdist(x1,y1,x2,y2):
    dist=sqrt((x2-x1)**2+(y2-y1)**2)
    dist= str(dist)
    return dist

# gestionnaires d'événements :
def depl_gauche():
    if flag==0:
        avance(-10, 0)
    else:
        avance(0,0,-10,0)
def depl_droite():
    if flag==0:
        avance(10, 0)
    else:
        avance(0,0,10,0)
def depl_haut():
    if flag==0:
        avance(0, -10)
    else:
        avance(0,0,0,-10)
def depl_bas():
     if flag==0:
         avance(0, 10)
     else:
         avance(0,0,0,10)

def which():
    global flag
    if flag==0:
        flag=1
    elif flag==1:
        flag=0

#------ Programme principal ------
# les variables suivantes seront utilisées de manière globale :
x1, y1,x2,y2 =100,100,300,300 # coordonnées initiales
flag=0

fen1 = Tk()
fen1.title("Exercice d'animation avec tkinter")

can1 = Canvas(fen1,bg='light grey',height=500,width=500)
oval1 = can1.create_oval(x1,y1,x1+60,y1+60,width=2,fill='red')
oval2 = can1.create_oval(x2,y2,x2+30,y2+30,width=2,fill='blue')

can1.pack(side=LEFT)
Button(fen1,text='Quitter',command=fen1.quit).pack(side=BOTTOM)
Button(fen1,text='Gauche',command=depl_gauche).pack()
Button(fen1,text='Droite',command=depl_droite).pack()
Button(fen1,text='Haut',command=depl_haut).pack()
Button(fen1,text='Bas',command=depl_bas).pack()
Button(fen1,text='Change',command=which).pack()
Label(fen1, text = 'distance :'+calculdist(x1,y1,x2,y2)).pack()

fen1.mainloop()

Label will not update automatically if you don't use textvariable= with StringVar() .
But you can update text manually without textvariable and StringVar

First you have to assign Label to variable to have access to it.

lab = Label(fen1, text = 'distance :'+calculdist(x1,y1,x2,y2))
lab.pack()

And later in avance you can change text using

lab['text'] = 'distance :'+calculdist(x1,y1,x2,y2)

EDIT: full code with better formatting to make it more readable

import tkinter as tk
from math import sqrt

def avance(lat1=0, vert1=0, lat2=0, vert2=0):
     global x1, y1, x2, y2

     x1 += lat1
     y1 += vert1
     x2 += lat2
     y2 += vert2

     if flag == 0:
        can1.coords(oval1, x1, y1, x1+60, y1+60)
     else:
        can1.coords(oval2, x2, y2, x2+30, y2+30)

     lab['text'] = 'distance: '+calculdist(x1,y1,x2,y2)

def calculdist(x1, y1, x2, y2):
    dist = sqrt((x2-x1)**2+(y2-y1)**2)
    #dist = str(dist)
    dist = "{:10.5f}".format(dist)
    return dist

def depl_gauche():
    if flag == 0:
        avance(-10, 0)
    else:
        avance(0, 0, -10, 0)

def depl_droite():
    if flag == 0:
        avance(10, 0)
    else:
        avance(0, 0, 10, 0)

def depl_haut():
    if flag == 0:
        avance(0, -10)
    else:
        avance(0, 0, 0, -10)

def depl_bas():
     if flag == 0:
         avance(0, 10)
     else:
         avance(0, 0, 0, 10)

def which():
    global flag

    if flag == 0:
        flag = 1
    elif flag == 1:
        flag = 0

#------

x1 = 100
y1 = 100
x2 = 300
y2 = 300
flag = 0

fen1 = tk.Tk()
fen1.title("Exercice d'animation avec tkinter")

can1 = tk.Canvas(fen1, bg='light grey', height=500, width=500)
oval1 = can1.create_oval(x1, y1, x1+60, y1+60, width=2, fill='red')
oval2 = can1.create_oval(x2, y2, x2+30, y2+30, width=2, fill='blue')

can1.pack(side='left')

tk.Button(fen1, text='Quitter', command=fen1.destroy).pack(side='bottom')
tk.Button(fen1, text='Gauche', command=depl_gauche).pack()
tk.Button(fen1, text='Droite', command=depl_droite).pack()
tk.Button(fen1, text='Haut', command=depl_haut).pack()
tk.Button(fen1, text='Bas', command=depl_bas).pack()
tk.Button(fen1, text='Change', command=which).pack()

lab = tk.Label(fen1, text='distance: '+calculdist(x1, y1, x2, y2))
lab.pack()

fen1.mainloop()

BTW: see PEP 8 -- Style Guide for Python Code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM