如何在 Tkinter 中显示/隐藏小部件? [英] How to show/hide widgets in Tkinter?

查看:29
本文介绍了如何在 Tkinter 中显示/隐藏小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个程序,该程序在给定一系列用户输入的情况下执行某个功能.几个用户输入仅在某些情况下是必需的,如果可能,我想仅在选择 Checkbutton 时显示这些输入值的条目框和标签,指示存在需要这些输入的情况.我不知道该怎么做:

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.

在取消选中 Checkbutton 时隐藏"标签和 Entry 框,而不销毁它们,这样如果 Checkbutton 被取消,它们可以再次显示而不会丢失任何已经输入的数据重新选择.

"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.

  • 示例:我选择 Checkbutton,在出现的新框中输入数据,然后取消选中 Checkbutton(导致这些框不再显示).如果我随后重新选择 Checkbutton,我上次选择 Checkbutton 时输入的数据应该仍然存在.

如果 Checkbutton 在之前被取消选择后重新选择,则显示"之前隐藏"的相同标签和条目框.

"Show" the same Labels and Entry boxes that had previously been "hidden" if the Checkbutton is reselected after having been previously deselcted.

我不知道这样的事情是否可行,但如果不可行,请告诉我.此外,我知道我可以简单地将相关条目框的 state 设置为 DISABLED 而取消选中 Checkbutton,但如果可能的话,我希望这些框不出现以便它们的存在不会混淆不熟悉需要额外输入的情况的用户.

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.

如果这是相关的,我在 Windows 10 专业版上使用 Python 2.7.9、Anaconda 2.2.0(64 位)和 Tkinter 版本 81008.如果我遗漏了任何有用的信息,请随时请求更多信息.预先感谢您提供的任何帮助.

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.

推荐答案

我想你想要 grid_remove().

来自 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.

grid 的remove"方法的作用是一样的,只不过grid选项将被记住.

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

这篇关于如何在 Tkinter 中显示/隐藏小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆