473,383 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

TK-grid problem, please help

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###############
Apr 21 '07 #1
7 2120
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.
Apr 21 '07 #2
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:

#####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[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
Apr 21 '07 #3
Ray
Hi, Thanks for the help!
Anton Vredegoor wrote:
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.
Apr 23 '07 #4
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

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
Apr 23 '07 #5
Hello,

Ray schrieb:
Hi, Thanks for the help.
I was trying to find a book "Python TK" something on last Friday.
but didn't find it :-)
There is only one printed book, all the details here:

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

HTH
Hertha
Apr 23 '07 #6
Ray
Hi Anton,

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

Ray

Anton Vredegoor wrote:
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.
Apr 24 '07 #7
Hertha Steck wrote:
Hello,

Ray schrieb:
>Hi, Thanks for the help.
I was trying to find a book "Python TK" something on last Friday.
but didn't find it :-)

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
Apr 25 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Robin Becker | last post by:
I have some tkinter code that used to work in 2.2, but is failing in 2.3. I guess it's because the Tcl version is different. Looking in the text.tcl file I see that we now have ::tk::Priv and...
0
by: Jack Diederich | last post by:
I'm calling Tk() twice to create two top level windows, which works fine in every respect except fonts. Assigning a Tkfont to widgets in the first Tk() window I create works fine, the font looks...
4
by: Peter Moscatt | last post by:
I am having trouble understanding the methods for the Listbox from Tk. If I was to select at item in the list using a mouse click (have already created the bind event) - what method returns the...
2
by: Eric McDaniel | last post by:
I am trying to read in a bunch of images and manipulate them using Image::Magick, then display them using Tk::Photo. I would like to do this without creating a temp file for each image, since there...
4
by: MBW | last post by:
I have a class that is a windows in a GUI the following is the code: class optWin: def __init__(self): return None def __call__(self):
4
by: Ab Ran | last post by:
Hi, I have implemented some GUI using Tk script ( I will call it gui.tk). I want to call some C functions inside this script. How do I do it ? I don't want to re-implement the contents of gui.tk...
24
by: Penguin_X | last post by:
Hi. I'm using Tk with Perl. I've take a look on google and many forum about Tk and C relationship. Is there any way to use it with C ? I would prefer code compiled Tk programs than interpreted...
1
by: malv | last post by:
Wanting to explore tk under python, I must say that it seems to be very difficult to find the required information in one single place. I would like to give tk a try but as I need as a test...
8
by: Leon | last post by:
Hello, First of all, I beg you pardon for my poor english... You probably know it, but a new version of Tcl/TK has arrived :...
4
by: Kevin Walzer | last post by:
I'm using a build of Python 2.5.1 on OS X 10.5.1 that links to Tk 8.5. Trying to test PIL with this new build, Python barfs when trying to display an image in a Tkinter Label. Here is my sample...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.