0

I've made a basic tk window and kept a button to delete all widgets but an imprint of the widgets is left over.

How can I remove that blank part? problem still persists

from tkinter import *

class Window(Frame):

  def __init__(self, master=None):
    Frame.__init__(self, master)                 
    self.master = master
    self.init_window()
  def init_window(self):   
    self.master.title("SAMPLE")
    self.pack(expand=1)
    loginb = Button(self, text="Log in",command=self.LogIn, height=2, width=20)
    loginb.grid(row=1)

  def Quit(self):
        exit()
  def LogIn(self):
        for widget in Frame.winfo_children(self):
              widget.destroy()
        self.grid_forget()
        self.grid()
        e1
        self.L = {}
        Label1 = Label(text="Enter your code:").grid(row=1,column=0)
        E1 = Entry(textvariable=e1).grid(row=1,column=1)
        def  F1():
              self.L["Code"] = e1.get()
              return
        B1 = Button(text="Ok", command=F1).grid(row=1,column=2)
root = Tk()
root.geometry("700x700")
app = Window(root)
root.mainloop()

1 Answer 1

1

You need to remove frame itself as well. It's the frame that is leftover.

def LogIn(self):
    for widget in Frame.winfo_children(self):
        widget.destroy()
    self.pack_forget()

Looking at your Menu though, you probably will need those buttons back again so instead of re-creating them each time after using widget.destroy(), you should use widget.grid_forget() and use widget.grid() when they are needed again.

EDIT: Added a dummy menu button to show how to add other widgets

class Window(Frame):
  def __init__(self, master=None):
    ...
    ...
    ...
    ...
    file.add_command(label="dummybutton", command=self.add_widget)

  def LogIn(self):
        for widget in Frame.winfo_children(self):
              widget.destroy()
        self.pack_forget()

  def add_widget(self):
      #here you are packing again
      #since pack doesn't remember config options, 
      #you need to specify those here as well like expand etc.
      self.pack(expand=1) 
      #adding dummy widget to show it adds without a problem
      Button(self, text="dummy widget").pack()
Sign up to request clarification or add additional context in comments.

6 Comments

pack frame again first with self.pack(). Then do your widget additions.
@CompStudent edited answer to add an example on how to do it.
but my code(which also contains an Entry widget) gives an error: cannot use geometry manager grid inside . which already has slaves managed by pack
@CompStudent Then change those pack ones to grid as well. In same container, you should stick to one geometry manager. If you used grid on one, you should use grid on all.
this is what ive done: for widget in Frame.winfo_children(self): widget.destroy() self.pack_forget() self.(expand=1) loginb = Button(self, text="Log in",command=self.LogIn, height=2, width=20).grid(row=1) E1 = Entry().grid(row=1,column=1) but it seems to show error only for the entry widget
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.