Luke wrote:
Hello, I'm an inexperienced programmer and I'm trying to make a
Tkinter window and have so far been unsuccessful in being able to
delete widgets from the main window and then add new ones back into
the window without closing the main window.

The coding looks similar to this:
...
from Tkinter import *
def MainWin():
    main=Tk()
    ...
    close_frame1=Button(frame1,text='close',bg='light gray',
command=frame1.destroy)
    close_frame1.pack_propagate(0)
    close_frame1.pack(side=TOP, anchor=N,pady=25)
    if frame1.destroy==True:
        frame1=Frame(back_ground,width=213,height=480,bg='white')
        frame1.pack_propagate(0)
        frame1.pack(side=TOP,anchor=N)
    main.mainloop()
MainWin()

It may just be bad coding but either way I could use some help.


I'll tell you what I find helps me in exploring Tkinter: running
Idle in the "no-subprocesses" mode.  Now the resulting system _is_
less stable and may well require you to restart from time to time,
but there is a compensation: you can see the effect of each
operation as you do it by hand, as well as checking expressions:

If in Windows:
    run cmd
    C:\> python -m idlelib.idle -n

If in Linux / MacOSx:
    Start a terminal:
    $ python  -m idlelib.idle -n &

Once idle shows up,
    >>> import Tkinter
    >>> main = Tkinter.Tk()
<a window shows up>
      ...

Following through your code, you'll see you start the frame,
add the button, and, immediately after getting everything set
up, you check:
>     if frame1.destroy==True:
Which it will never be (frame1.destroy is a method, not data).
Once you get the base set up, your code should runfrom events.

So, to get a single step farther:
At the top of the MainWin function, insert:
    global frame1, close_frame1
Replace your "close_frame1 =" line down to end-o-function with:
    def on_click():
        global frame1, close_frame1
        frame1.destroy()
        frame1 =Frame(back_ground,width=213,height=480,bg='white')
        frame1.pack_propagate(0)
        frame1.pack(side=TOP,anchor=N)
        close_frame1 = Button(frame1,text='retry', bg='light blue',
                              command=on_click)
        close_frame1.pack_propagate(0)
        close_frame1.pack(side=TOP, anchor=N,pady=25)
    close_frame1 = Button(frame1,text='close',bg='light gray',
                                    command=on_click)
    close_frame1.pack_propagate(0)
    close_frame1.pack(side=TOP, anchor=N,pady=25)

The "global" lines are needed so that on_click and MainWin (which both
set the two names) are talking about the same objects.

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to