簡體   English   中英

Tkinter標簽未正確更新

[英]Tkinter Label not updating correctly

我已經在今天下午問過一個有關如何在Tkinter中更新標簽的問題,並且得到了有效的答案。 但是,我發現“解決方案”代碼有點長,並試圖對其進行改進。 基本上,我使用的是updt函數,該函數在標簽lbl上執行configure method 該方法將通過調用gravitation函數來更改標簽文本。 每次單擊某處時,我都會在整個窗口上使用bind方法來調用updt函數。

現在的問題是該程序無法正常運行,因為標簽上顯示的數字不正確,即使行星與以前的距離相同,也不總是顯示相同的值。

我仔細閱讀了每一行,但是由於我是編程的新手,所以找不到錯誤所在。

我正在使用Python 3。

這是我以前的代碼: 更新Tkinter標簽

這是新的:

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)
    if dist != 0:
        grav = 6.67384/dist
    else:
        grav = "Infinite"
    str(grav)
    return grav

def updt (event):
    lbl.configure (text = gravitation(oval1, oval2)) 

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.grid(row=0, column=1, sticky =W, padx = 5, pady = 5, rowspan =4)
Button(wind, text = 'Quit', command=wind.destroy).grid(row=5, column=1, sticky =W, padx = 5, pady = 5)
wind.bind ("<Button-1>", updt)


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



oval2 = can.create_oval(x+50,y+50,x+80,y+80,width=2,fill='blue') #Planet 2 moving etc
Button(wind, text = 'Left', command=moveLeft2).grid(row=0, column=3, sticky =W, padx = 5, pady = 5)
Button(wind, text = 'Right', command=moveRight2).grid(row=1, column=3, sticky =W, padx = 5, pady = 5)
Button(wind, text = 'Top', command=moveTop2).grid(row=2, column=3, sticky =W, padx = 5, pady = 5)
Button(wind, text = 'Bottom', command=moveBottom2).grid(row=3, column=3, sticky =W, padx = 5, pady = 5)




lbl = Label(wind, bg = 'white')#label
lbl.grid(row=4, column=1, sticky =W, padx = 5, pady = 5, columnspan = 3)
gravitation (oval1, oval2)


wind.mainloop()

當用戶單擊窗口的任何部分時, updt觸發,當用戶單擊按鈕然后釋放按鈕時,移動功能觸發。 結果,總是在移動功能之前調用updt 因此,標簽移動到新位置之前報告了行星彼此之間的重力。

坐標更改后,不要將updt綁定到全局單擊事件,而是將其放入move函數。

def updt():
    lbl.configure (text = gravitation(oval1, oval2)) 

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM