CompStudent
CompStudent

Reputation: 1

Tkinter programming

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()

Upvotes: 0

Views: 90

Answers (1)

Lafexlos
Lafexlos

Reputation: 7735

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()

Upvotes: 1

Related Questions