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.
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.
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.
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.
tkinter Tkinter Geometry Managers grid() It uses column , columnspan , ipadx , ipady , padx , pady , row , rowspan and sticky .
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))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With