uwain12345
uwain12345

Reputation: 386

Python Tkinter how to hide a widget without removing it

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?

Upvotes: 0

Views: 3131

Answers (2)

figbeam
figbeam

Reputation: 7176

Use grid as geometry manager and use:

self.btn1.grid_remove()

which will remember its place.

Upvotes: 2

Subrata Sarkar
Subrata Sarkar

Reputation: 72

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

Upvotes: 1

Related Questions