Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get grid information from pressed Button in tkinter?

I need to create table of buttons using Tkinter in Python 2.7, which has n rows and n columns, and has no button in bottom right corner.

Problem is that when I press a button, in its place, I need to create free space and move that button to space that was empty before, and I cannot do that because I don't know how to get grid (x and y axes) information of pressed button to use it 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 button grid position, and use it to change k and m variables to make empty space in position where pressed button was.

like image 209
Никола Бањац Avatar asked Jul 01 '15 16:07

Никола Бањац


People also ask

What is Grid () in Tkinter?

The Grid geometry manager puts the widgets in a 2-dimensional table. The master widget is split into a number of rows and columns, and each “cell” in the resulting table can hold a widget. The grid manager is the most flexible of the geometry managers in Tkinter.

Can you use place with grid in Tkinter?

Tkinter has three built-in layout managers: the pack , grid , and place managers. The place geometry manager positions widgets using absolute positioning. The pack geometry manager organizes widgets in horizontal and vertical boxes. The grid geometry manager places widgets in a two dimensional grid.

What is Columnspan in grid Tkinter?

columnspan − How many columns widgetoccupies; default 1. ipadx, ipady − How many pixels to pad widget, horizontally and vertically, inside widget's borders. padx, pady − How many pixels to pad widget, horizontally and vertically, outside v's borders.

Does Tkinter have Rowspan?

tkinter Tkinter Geometry Managers grid() It uses column , columnspan , ipadx , ipady , padx , pady , row , rowspan and sticky .


1 Answers

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

button = Button(..., command=lambda row=i, column=j: doSomething(row, column))
like image 121
Bryan Oakley Avatar answered Oct 02 '22 20:10

Bryan Oakley