How to show/hide widgets in Tkinter?

前端 未结 1 1160
忘掉有多难
忘掉有多难 2020-12-19 05:48

I am attempting to create a program that performs a function given a series of user inputs. Several of the user inputs are only necessary under certain circumstances, and I

相关标签:
1条回答
  • 2020-12-19 06:21

    I think you want grid_remove().

    From http://www.tkdocs.com/tutorial/grid.html:

    The "forget" method of grid, taking as arguments a list of one or more slave widgets, can be used to remove slaves from the grid they're currently part of. This does not destroy the widget altogether, but takes it off the screen, as if it had not been gridded in the first place. You can grid it again later, though any grid options you'd originally assigned will have been lost.

    The "remove" method of grid works the same, except that the grid options will be remembered.

    Ugly example follows. Play with the grid options and the entry text to see how they are preserved.

    def toggle_entry():
        global hidden
        if hidden:
            e.grid()
        else:
            e.grid_remove()
        hidden = not hidden
    
    hidden = False
    root = tk.Tk()
    e = tk.Entry(root)
    e.grid(row=0, column=1)
    tk.Button(root, text='Toggle entry', command=toggle_entry).grid(row=0, column=0)
    root.mainloop()
    
    0 讨论(0)
提交回复
热议问题