TK-grid problem, please help
Anton Vredegoor
anton.vredegoor at gmail.com
Sat Apr 21 05:11:12 EDT 2007
Ray wrote:
> hi, I have a question about how to use .grid_forget (in python/TK)
>
> I need to work on grid repeatly. everytime when a button is pressed,
> the rows of grid is different. such like, first time, it generate 10
> rows of data.
> 2nd time, it maybe only 5 rows. so I need a way to RESET the grid data
> every time. how can I do it? by grid_forger()?, then would anyone can
> help on
> how to use grid_forget()
> the sample code as following:
I'm not sure if it solves your problem but this modification of your
code at least *looks* like it works better. The entries are completely
destroyed so that the next time you call the function they can be
recreated.
The trick I am using is to use a list in the arguments of the function
but that is a bit of a hack, the list 'remembers' its state from the
last time the function was called, I think one should use classes for
bookkeeping such things instead.
from Tkinter import *
def mygrid(text,M = []):
######## how to use grid_forget() to clean the grid??###########
while M:
x = M.pop()
x.destroy()
rows = []
count=int(text)
for i in range(count):
cols = []
for j in range(4):
e = Entry(frame3, relief=RIDGE)
M.append(e)
e.grid(row=i, column=j, sticky=NSEW)
e.insert(END, '%d.%d' % (i, j))
cols.append(e)
rows.append(cols)
root=Tk()
frame1=Frame(root, width=150, height=100)
frame1.pack()
text=Entry(frame1)
text.pack(side=LEFT)
button=Button(frame1, text='generate grid', command=(lambda:
mygrid(text.get())))
button.pack()
frame2=Frame(root, width=150, height=100)
frame2.pack()
button2=Button(frame2, text='exit', command=root.quit)
button2.pack()
frame3=Frame(root, width=150, height=300)
frame3.pack()
root.mainloop()
A.
More information about the Python-list
mailing list