私はまだ、2 つの惑星の周りを移動し、それらの間の重力を示すコードに取り組んでいます。移動したい惑星を選択できる2つのボタンを表示するだけで、使いやすくしようとしました。次に、キャンバスをクリックすると、選択した惑星がクリックした場所に移動します。
プログラムは機能しますが、グローバルステートメントで関数chngB
と関数を使用するよりも良い方法があるかどうかを知りたいです。Python では、ボタンchngO
のパラメーターに関数を割り当てるときに、パラメーターなしで関数を使用することを余儀なくされているとは、まだ信じられません。command
基本的に、次のようなものを書くことが可能かどうかを知りたいですcommand = (a=1)
(これが機能しないことはわかっていますが、アイデアはわかります)。また、変数を使用する以外に、おそらく別の方法がありますどの惑星が選択されているか (最後にどのボタンが押されたか) を知ることができます。
私はPython3を使用しています。
from tkinter import *
import math
x, y = 135, 135
a = 0
def gravitation (obj1,obj2):#showing gravitational force between planets
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 chngB ():#Telling Blue planet is selected
global a
a = 1
def chngO ():#Telling Orange planet is selected
global a
a = 0
def updt ():#Updating gravitation label
lbl.configure (text = gravitation(oval1, oval2))
def moveBlue (event):#Placing blue planet where mouse click on canv
coo = [event.x-15, event.y-15, event.x+15, event.y+15]
can.coords(oval1, *coo)
updt()
def moveOrange (event):#Placing orange planet where mouse click on canv
coo = [event.x-15, event.y-15, event.x+15, event.y+15]
can.coords(oval2, *coo)
updt()
def choice (event):#Function binded to can, move the selected planet (blue = 1, prange = 0)
if a == 0:
moveOrange(event)
else :
moveBlue(event)
##########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=0, sticky =W, padx = 5, pady = 5, rowspan =3)
can.bind ("<Button-1>", choice)
Button(wind, text = 'Quit', command=wind.destroy).grid(row=2, column=1, sticky =W, padx = 5, pady = 5)
oval1 = can.create_oval(x,y,x+30,y+30,width=2,fill='blue') #Planet 1 moving etc
buttonBlue = Button(wind, text = 'Blue Planet', command = chngB)
buttonBlue.grid(row=1, column=1, sticky =W, padx = 5, pady = 5)
oval2 = can.create_oval(x+50,y+50,x+80,y+80,width=2,fill='orange') #Planet 2 moving etc
buttonOrange = Button(wind, text = 'Orange Planet', command = chngO)
buttonOrange.grid(row=0, column=1, 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()