Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tkinter problem
#4
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)
Reply


Messages In This Thread
Tkinter problem - by DPaul - May-27-2020, 07:36 AM
RE: Tkinter problem - by Knight18 - May-27-2020, 05:14 PM
RE: Tkinter problem - by DT2000 - May-27-2020, 07:12 PM
RE: Tkinter problem - by deanhystad - May-28-2020, 05:00 AM
RE: Tkinter problem - by DPaul - May-28-2020, 07:17 AM
RE: Tkinter problem - by deanhystad - May-28-2020, 12:51 PM
RE: Tkinter problem - by DPaul - May-28-2020, 03:40 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python3 tkinter radiobutton problem Nick_tkinter 14 6,187 Feb-15-2021, 11:01 PM
Last Post: Nick_tkinter
  tkinter python button position problem Nick_tkinter 3 3,620 Jan-31-2021, 05:15 AM
Last Post: deanhystad
  [Tkinter] ClockIn/Out tkinter problem Maryan 2 2,253 Oct-12-2020, 03:42 AM
Last Post: joe_momma
  tkinter| listbox.insert problem Maryan 3 3,571 Sep-29-2020, 05:34 PM
Last Post: Yoriz
  [Tkinter] Tkinter - I have problem after import varaible or function from aGUI to script johnjh 2 2,630 Apr-17-2020, 08:12 PM
Last Post: johnjh
  [Tkinter] Problem with tkinter when creating .exe file Jan_97 2 4,651 Feb-27-2020, 05:17 PM
Last Post: Jan_97
  [Tkinter] Tkinter problem catlessness 1 2,085 Jan-15-2020, 05:17 AM
Last Post: Larz60+
  Problem with Submit button Tkinter Reldaing 2 3,710 Jan-05-2020, 01:58 AM
Last Post: balenaucigasa
  tkinter GUI, problem running seperate files fishglue 17 6,498 Oct-15-2019, 02:56 PM
Last Post: Denni
  [python] [Tkinter] Problem bidding combobox with np.array NEL 3 3,454 Aug-04-2019, 11:07 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020