TK-grid problem, please help

R

Ray

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:

#####begin of program###############

from Tkinter import *
def mygrid(text):
######## how to use grid_forget() to clean the grid??###########
rows = []
count=int(text)
for i in range(count):
cols = []
for j in range(4):
e = Entry(frame3, relief=RIDGE)
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()

#####end of program###############
 
A

Anton Vredegoor

Ray said:
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.
 
J

James Stroud

Ray said:
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:

#####begin of program###############

from Tkinter import *
def mygrid(text):
######## how to use grid_forget() to clean the grid??###########
rows = []
count=int(text)
for i in range(count):
cols = []
for j in range(4):
e = Entry(frame3, relief=RIDGE)
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()

#####end of program###############


Using grid_forget() is probably optimization overkill, but may be handy
for slower computers where you can watch the widgets appear one by one
(older than about 5 years--for example original mac ibook). Also, you
should get a good book on Tkinter because your design here will pretty
difficult to maintain and is not very flexible.

But...if you want to know how it might be done with grid_forget using
the code you already have (i.e. making widgets only if necessary):


#START#
from Tkinter import *
from tkMessageBox import showerror
def mygrid(text):
######## how to use grid_forget() to clean the grid??###########
numrows = len(frame3.rows)
try:
count=int(text)
except:
showerror('Entry Error',
'''Hey, "%s" don't make an int, Fool!''' % text,
parent=frame3)
return 'break'
for i in range(count):
if i < numrows:
cols = frame3.rows
else:
cols = [Entry(frame3, relief=RIDGE) for j in range(4)]
frame3.rows.append(cols)
for j in range(4):
e = cols[j]
e.grid(row=i, column=j, sticky=NSEW)
e.delete(0,END)
e.insert(END, '%d.%d' % (i, j))
for i in range(i+1, numrows):
for e in frame3.rows:
e.grid_forget()


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)
# adding an attribute here
frame3.rows = []
frame3.pack()

root.mainloop()
#END#

Notice also the necessity for the "e.delete(0, END)" line to get the
desired text in the entries.

Also demonstrated is how to handle poor input.

*Note*
Remember to always call the user "Fool" when he does something stupid.


James
 
R

Ray

Hi, Thanks for the help!


Anton said:
Ray said:
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.
 
R

Ray

Hi, Thanks for the help.


I was trying to find a book "Python TK" something on last Friday.
but didn't find it :)

I know those codes are in poor design, because I wrote those sample code
to show the idea about what I need. the real code is working with mysql.

however, I'm really new in python. (I start learning it on last Wednesday).

Thanks again for the help!

Ray
 
R

Ray

Hi Anton,

Thanks again. This is what I need!
my problem already solved.

Ray

Anton said:
Ray said:
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.
 
J

James Stroud

Hertha said:
Hello,


There is only one printed book, all the details here:

http://wiki.python.org/moin/GuiBooks

HTH
Hertha

This is inaccurate. There is only one book listed in the wiki. Python
Programming by Mark Lutz has an excellent Tkinter section as well as an
*incredible* amount of other information. Also, it has recently been
updated. It is probably the most relevant book for making a complete
transition from novice python programmer to expert python programmer.

James
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top