[英]Python//tkinter label update data
i'm doing an exercise to learn python. 我正在做一个练习来学习python。 Basically i'm supposed to create 2 circles using tkinter and calculate the distance between them while i'm moving them using buttons.
基本上我应该使用tkinter创建2个圆并在使用按钮移动它们时计算它们之间的距离。 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()
. 如果您不对
StringVar()
使用textvariable=
,则Label
不会自动更新。
But you can update text manually without textvariable
and StringVar
但是您可以手动更新文本,而无需使用
textvariable
和StringVar
First you have to assign Label
to variable to have access to it. 首先,您必须将
Label
分配给变量才能访问它。
lab = Label(fen1, text = 'distance :'+calculdist(x1,y1,x2,y2))
lab.pack()
And later in avance
you can change text using 后来在
avance
您可以使用更改文本
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 顺便说一句:请参阅PEP 8-Python代码样式指南
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.