0

私はPython 3を使用していますが、プログラミングはまったくの初心者です。2 つの惑星の周りを動き回り、それらの間の重力を確認できるウィンドウを表示するコードを作成しました。力を表示するはずのラベルを除いて、すべてが機能します。複数の試行と検索の後、惑星が移動するたびに更新する方法がわかりません。

私の問題は最後の行にあるはずだと思います:

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

StringVar と textvariable パラメーターを使用しようとしましたが、その概念がよくわかりませんでした。

これが私のコードです。答えは簡単だと思いますが、私はまったく経験がありません。

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

1 に答える 1

2

わかりました、やらなければならないことがいくつかあります。まず、次のようにラベルを作成します。

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

次に、これを配置する必要があります。

gravitation(oval1, oval2)

関数の最後にmove、任意の方向への移動が発生するたびにラベルが更新されるようにします。

第三に、 に戻る代わりにgravgravitationこれを最後に置きます:

lbl["text"] = grav

これで、 が呼び出されるたびgravitationに、ラベルが更新されます。

全体として、これはあなたが望むことをするはずです:

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とこれが上書きされるため、最初に削除しました。お役に立てれば!

于 2013-09-02T16:21:27.073 に答える