0

I know similar things have been asked a lot, but I've tried to figure this out for two hours now and I'm not getting anywhere. I want to have a button in a Tkinter window that is only visible on mouseover. So far I failed at making the button invisible in the first place (I'm familiar with events and stuff, that's not what this question is about) pack_forget() won't work, because I want the widget to stay in place. I'd like some way to do it like I indicated in the code below:

import tkinter as tki

class MyApp(object):

    def __init__(self, root_win):
        self.root_win = root_win
        self.create_widgets()

    def create_widgets(self):
        self.frame1 = tki.Frame(self.root_win)
        self.frame1.pack()
        self.btn1 = tki.Button(self.frame1, text='I\'m a button')
        self.btn1.pack()
        self.btn1.visible=False #This doesnt't work

def main():
    root_win = tki.Tk()
    my_app = MyApp(root_win)
    root_win.mainloop()

if __name__ == '__main__':
    main()

Is there any way to set the visibility of widgets directly? If not, what other options are there?

uwain12345
  • 356
  • 2
  • 21

2 Answers2

2

Use grid as geometry manager and use:

self.btn1.grid_remove()

which will remember its place.

figbeam
  • 7,001
  • 2
  • 12
  • 18
1

You can try using event to call function. If "Enter" occurs for button then call a function that calls pack() and if "Leave" occurs for button then call a function that calls pack_forget().

Check this link for event description:List of All Tkinter Events

If you wish your button to stay at a defined place then you can use place(x,y) instead of pack()

  • 1
    How does an Enter event occur when the widget is not packed? – fhdrsdg Jul 03 '18 at 11:06
  • Place a blank label behind the button when enter occurs for the label then call pack() or place() – Subrata Sarkar Jul 04 '18 at 14:27
  • I ended up going with a frame of fixed height. This can then react to the Enter and Leave events and create and destroy the widgets as needed. It kind of irks me, that I have to do this, though, seems very inelegant. – uwain12345 Jul 05 '18 at 14:44