问题
If I have several rows and columns with several entry/label widgets in the SAME frame, is it possible to choose just a single one of them and delete it whilst leaving the others alone.
e.g.
class Window():
def __init__(self):
self.root = Tk()
self.win1 = Frame(self.root)
entry1 = Entry(win1, row=1, column=1)
entry2 = Entry(win1, row=1, column=2)
label1 = Label(win1, row=1, column=3)
def main1(self):
label2 = Label(win1, row=1, column=4)
labeln = Label(win1, row=1, column=n)
I would then like to remove from column 3 onwards, where n could be a random number. Is it possible to get grid_forget and insert the number of columns? Is it also possible for the rows as well?
UPDATE: OR is it possible to simply return back to the window created under init and delete those created under main1 (but have been created in the same frame)?
Thank you.
回答1:
Read up on the grid_remove and grid_forget methods; these will alow you to remove existing widgets from view. You can also destroy widgets, causing them to vanish.
It's been a while since I've done this (and don't currently have a computer on which to create an example), but I think the one sticky point may be that you will have to go in and explicitly set the grid row and/or column heights, widths or weights back to zero to reclaim the space. It's quite doable though.
Your other choice is to create all of your widgets via a method or function. You can then pretty easily destroy and recreate all the widgets. This is probably less pleasant for the user since the whole UI will "blink".
回答2:
Here's a csh script to create a dynamic python code for GUI.
What it does?
enter
./dynamic_python -label name,age -frame myframe
and it will create 2 label named name,age , 2 entry box for each label and some other widgets. I know it's not exactly what you need, but consider it at a possible approach to your solution. Ping me if you need more help. Will appreciate other user's feedback :)
CODE for dynamic_python:
#! /bin/csh -f
#echo $argv[1]
#shift
#echo $argv[1]
while ("$#" != 0)
if("$argv[1]" == "-label") then
set label = "$argv[2]"
shift
shift
endif
if("$argv[1]" == "-frame") then
set frame = "$argv[2]"
shift
shift
endif
end
set label = `echo $label | sed 's/,/ /g'`
set lcount = `expr "$#label" + 1`
set lc = 1
#
echo "#! /usr/bin/python" > dy.py
echo "from Tkinter import *" >> dy.py
#
echo "def fun_ok():" >> dy.py
echo " print 'OK_Accepted'" >> dy.py
set label = `echo $label | sed 's/,/ /g'`
set lcount = `expr "$#label" + 1`
set lc = 1
echo " list_out = []" >> dy.py
while ($lc != $lcount)
echo " print $label[$lc].get()" >> dy.py
echo " list_out.append('-$label[$lc]')" >> dy.py
echo " list_out.append($label[$lc].get())" >> dy.py
set lc = `expr "$lc" + 1`
end
echo " print list_out" >> dy.py
#
echo "$frame = Tk()" >> dy.py
#
set label = `echo $label | sed 's/,/ /g'`
set lcount = `expr "$#label" + 1`
set lc = 1
while ($lc != $lcount)
echo $lc $label[$lc]
echo "$label[$lc] = StringVar()" >> dy.py
echo "Label($frame,text="_$label[$lc]_").grid(row = $lc,column = 1)" | sed 's/_/"/g' >> dy.py
echo "Entry($frame,textvariable=$label[$lc]).grid(row=$lc,column=2)" >> dy.py
set lc = `expr "$lc" + 1`
end
echo "Button($frame,text='OK',command=fun_ok).grid(row = $lc,column = 1)" >> dy.py
echo "Button($frame,text='EXIT',command=$frame.quit).grid(row = $lc,column = 2)" >> dy.py
#Button(master, text="OK", command=callback)
#
echo "$frame.mainloop()" >> dy.py
#
chmod 755 *
dy.py
来源:https://stackoverflow.com/questions/11357988/deleting-certain-rows-columns-in-a-grid-tkinter