Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
489 views
in Technique[技术] by (71.8m points)

python - How to show/hide widgets in Tkinter?

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 would like to, if possible, only show the Entry boxes and Labels for those input values when a Checkbutton is selected indicating that the circumstances requiring those inputs are present. What I'm not sure how to do is:

  • Put the Labels and Entry boxes I'm adding in between rows that already exist.

  • "Hide" the Labels and Entry boxes when the Checkbutton is deselected, without destroying them, so that they can be displayed again without losing any already-entered data if the Checkbutton is reselected.

    • Example: I select the Checkbutton, enter data into the new boxes that appear, then deselect the Checkbutton (causing the boxes to no longer be displayed). If I were to then reselect the Checkbutton, the data I entered the last time the Checkbutton was selected should still be there.
  • "Show" the same Labels and Entry boxes that had previously been "hidden" if the Checkbutton is reselected after having been previously deselcted.

I don't know if something like this is even possible, but if it's not, please let me know. Also, I am aware that I could simply set the relevant Entry boxes' state to DISABLED while the Checkbutton is deselected, but I would prefer, if possible, that the boxes not appear so that their presence does not confuse users who are not familiar with the circumstances under which the additional inputs are necessary.

If this is relevant, I am using Python 2.7.9, Anaconda 2.2.0 (64-bit), and Tkinter version 81008 on Windows 10 Pro. Feel free to request further information if I've left anything out that would be useful to know. Thanks in advance for any help you're able to provide.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...