How to get grid information from a pressed button in tkinter?

I need to create a table of buttons using Tkinter in Python 2.7 that has columns n

and n

, and does not have a button in the bottom right corner.

The problem is that when I click a button, in its place I need to create a free space and move that button to a space that was empty before, and I can't do that because I don't know how to get the grid (x and y) information about the pressed button to use to create free space.

This is my current code:

from Tkinter import *

#Input:
n=int(raw_input("Input whole positive number: "))
L = range(1,n+1)
k = n
m = n

#Program:
root = Tk()
for i in L:
    for j in L:
        frame = Frame(root)
        frame.grid(row = i, column = j)
        if j == k and i == m:
            pass
        else:
            button = Button(frame)
            button.grid(row = i, column = j)


root.mainloop()

      

It would be something like this, where I wanted to get the position of the button grid and use that to change the variables k

and m

to make an empty space where the button is clicked.

+3


source to share


3 answers


You can pass a row and a column using a lambda expression for the button:



button = Button(..., command=lambda row=i, column=j: doSomething(row, column))

      

+4


source


Could you do something like making a grid of buttons up, but include the hidden button in the white space? Then when the button is clicked, click the Hide button and show the hidden one. Then you don't have to worry about moving the buttons around unless you need the actual button object to move for some reason.

Edit to improve the answer from comments:



Below is a simple example of hiding a button, and it also shows how to track buttons, unless I bring it to an editor. This can be translated into a list or dictionary of buttons or any other need. You also need to define a way to register local buttons, or add context to know which one to show or hide.

from Tkinter import Button, Frame, Tk


class myButton(Button):

    def __init__(self, *args, **kwargs):
        Button.__init__(self, *args, command=self.hideShowButton,
                        ** kwargs)
        self.visible = True

    def hideShowButton(self):
        self.visible = False
        self.pack_forget()

window = Tk()
frame = Frame(window)
frame.pack()
b1 = myButton(window, text="b1")
b1.pack()
b2 = myButton(window, text="b2")
b2.pack()
b3 = myButton(window, text="b3")
b3.pack()
window.wait_window(window)
print "At the end of the run b1 was %s, b2 was %s, b3 was %s" % (str(b1.visible), str(b2.visible), str(b3.visible))

      

0


source


This is how I tried to do it, and the problem is that I still don't know how to get the row and column of the button clicked ...

from Tkinter import *

#Input:
n = int(raw_input("Input whole positive number: "))
L = range(1,n+1)
k = n
m = n
#Try putting values for k and m which are less then n and you will see what   i need to get

#Moving:
def Move():
    #This i cant fill
    return k,m

#Program:
root = Tk()
for i in L:
    for j in L:
        frame = Frame(root)
        frame.grid(row = i,column = j)
        if i == int(k) and j == int(m):
            pass
        else:
            button = Button(frame, command = lambda: Move())
            button.pack()

root.mainloop()

      

So by changing the values k

and m

, a space is created elsewhere ...

-1


source







All Articles