TK-grid problem, please help

Ray ruiligc at earthlink.net
Mon Apr 23 07:34:42 EDT 2007


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



James Stroud wrote:
> 
> 
> 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[i]
>         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[i]:
>         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



More information about the Python-list mailing list