python - How to avoid global variables in command function call -


i'm still working on code moves around 2 planets , shows gravitational force between them. tried make easier use showing 2 buttons allow select planet want move. can click in canvas , selected planet move you've clicked.

the programm works, i'd know if there better way write using chngb , chngo functions global statements. still can't believe in python forced use function without parameters when assigning command parameter of button.

basically i'd know if it's possible write command = (a=1) (i know doesn't work, idea.) also, there way of doing having use variable know planet selected (which button has been pressed last).

i use python 3.

from tkinter import * import math  x, y = 135, 135 = 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 selected     global     = 1  def chngo ():#telling orange planet selected     global     = 0  def updt ():#updating gravitation label     lbl.configure (text = gravitation(oval1, oval2))    def moveblue (event):#placing blue planet 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 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 can, move selected planet (blue = 1, prange = 0)     if == 0:         moveorange(event)     else :         moveblue(event)  ##########main############  wind = tk() # window , 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() 

normally, if want button toggle variable between 1 of n values, use set of radiobuttons. when use radiobutton can associate variable whenever click button, variable automatically selected.

for example:

planet = intvar() planet.set(0)  buttonblue = radiobutton(wind, text="blue planet", variable=planet, value=1) buttonorange = radiobutton(wind, text="orange planet", variable=planet, value=0) ... def choice (event):#function binded can, move selected planet (blue = 1, prange = 0)     if planet.get() == 0:         moveorange(event)     else :         moveblue(event) 

if want use regular button, can single callback rather two, , use lambda or functools.partial pass in new value.

for example:

buttonblue = button(wind, text = 'blue planet', command = lambda: change(1)) buttonorange = button(wind, text = 'blue planet', command = lambda: change(0)) def change(newvalue):     global     = newvalue 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -