May-28-2020, 05:00 AM
Wrong widget toolkit. No hide/show in tkinter that I can find. Maybe it is the C/C++/C# programmer in me, but I hate the idea of destroying a button just because you want to hide it for a few seconds. This little demo uses pack_forget/pack to hide/show a button. It makes a special frame for the button to make it easier to pack. The demo has a blink feature that I set to 100x a second. Ran for several minutes and saw no change in memory use.
from tkinter import * class Peekabutton(Frame): def __init__(self, root, text=None, textvar=None, command=None, **kwargs): super().__init__(root, **kwargs) self.root = root self.blinking = False self.button = Button(self, text=text, textvar=textvar, command=command) self.show() def show(self): self.button.pack(fill=BOTH, expand=1) self.visible = True def hide(self): self.button.pack_forget() self.visible = False def toggle(self): if self.visible: self.hide() else: self.show() def blink_event(self): if self.blinking: self.toggle() self.root.after(10, self.blink_event) def blink(self): self.blinking = not self.blinking if self.blinking: self.blink_event() root = Tk() root.title('Hidden Buttons') root.geometry('300x100') b2 = Peekabutton(root, text='-a-', height=100, width=100) b1 = Peekabutton(root, text='Peek', command=b2.blink, height=100, width=100) b3 = Peekabutton(root, text='Button', command=b2.toggle, height=100, width=100) b1.pack(side=LEFT, fill=BOTH, expand=1, padx=10, pady=10) b2.pack(side=LEFT, fill=BOTH, expand=1, padx=10, pady=10) b3.pack(side=LEFT, fill=BOTH, expand=1, padx=10, pady=10)