简体   繁体   English

如何在Tkinter中显示/隐藏小部件?

[英]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. 有些用户输入仅在某些情况下是必需的,如果可能的话,我想在选择了一个Checkbutton时显示那些输入值的Entry框和标签,表明存在需要这些输入的情况。 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 destroy ing them, so that they can be displayed again without losing any already-entered data if the Checkbutton is reselected. 取消选中“检查”按钮时“隐藏”标签和输入框,而不会destroy它们,以便在重新选中“检查”按钮时可以再次显示它们而不会丢失任何已输入的数据。

    • 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). 示例:我选择Checkbutton,在出现的新框中输入数据,然后取消选中Checkbutton(导致不再显示框)。 If I were to then reselect the Checkbutton, the data I entered the last time the Checkbutton was selected should still be there. 如果我然后重新选择Checkbutton,我上次选中Checkbutton时输入的数据应该仍然存在。
  • "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. 此外,我知道我可以在取消选中Checkbutton时简单地将相关条目框的stateDISABLED ,但是如果可能的话,我希望这些框不会出现,以免它们的存在不会混淆不熟悉的用户需要额外投入的情况。

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. 如果这是相关的,我在Windows 10 Pro上使用Python 2.7.9,Anaconda 2.2.0(64位)和Tkinter版本81008。 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. 提前感谢您提供的任何帮助。

I think you want grid_remove() . 我想你想要grid_remove()

From http://www.tkdocs.com/tutorial/grid.html : 来自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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM