Button changing its own grid options

Martin Franklin martin.franklin at westerngeco.com
Wed Feb 20 10:08:22 EST 2002


a.clarke11 wrote:

> Hi
> Does anyone know if it is possible to allow a Tkinter button, when
> pressed, to change its own grid options?
> I've tried various command callbacks, but I'm having problems
> referencing the buttons own grid info.
> The idea is to get a button to move around, in a grid of other buttons,
> when pressed. Any ideas or even better, code, would be appreciated.
> Thanks in advance
> Tony
> 
> 
from Tkinter import *
root=Tk()
bdict={}
def moveme(b):
    old_row, old_col=bdict[b]
    ## change them 
    row=old_row+1
    col=old_col+1
    b.grid_forget()
    b.grid(row=row, col=col)
    ## update dict to reflect the change
    bdict[b]=(row, col)
for row in range(2):
    for col in range(2):
        b=Button(root)
        b.grid(row=row, col=col)
        b.config(command=lambda b=b: moveme(b))
        bdict[b]=(row, col)
root.mainloop()



More information about the Python-list mailing list