带有网格布局的 Tkinter 升降方法 [英] Tkinter lift and lower methods with grid layout

查看:24
本文介绍了带有网格布局的 Tkinter 升降方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

毫无疑问,这是一个新手问题.我在 Python 2.7 中使用 Tkinter 中的网格布局管理器.我想要一个按钮来隐藏单击时的列表框.到目前为止,这是我的代码:

No doubt this is a newbie question. I'm using the grid layout manager in Tkinter with Python 2.7. I want a button to hide a listbox on clicking. Here's my code so far:

from Tkinter import *
root = Tk()
frame = Frame(root)
pyList = ["Eric", "Terry", "Graham", "Terry", "John", "Carol?", "Michael"]
arbList = ['ham', 'spam', 'eggs', 'potatos', 'tots', 'home fries']
pythons = Listbox(frame, width=10, height=5, selectmode=EXTENDED, exportselection=0)
food = Listbox(frame, width=10, height=5, selectmode=EXTENDED, exportselection=0)
def hider():
    if pythons.selection_includes(4):
        food.lower()
    elif pythons.selection_includes(0):
        food.lift()
b2 = Button(frame, text="Hide!", command=hider)
b2.grid(row=2, column=1)
food.grid(row=0, column=1)
pythons.grid(row=1, column=1, pady=10)
frame.grid()

for python in pyList:
        pythons.insert('end', python)

for thing in arbList:
        food.insert('end', thing)


root.mainloop()

不幸的是,在这上面胡闹似乎会抛出一个错误,说我无法在框架上方或下方提升/降低我的列表框.我已经让它与 pack() 管理器一起工作,但不是 grid().

Unfortunately, monkeying around with this appears to throw an error saying I can't lift/lower my listbox above or below my frame. I've gotten this to work with the pack() manager, but not grid().

我错过了什么?

推荐答案

您不能将小部件降低到其父级下方.根据官方tk文档:

You can't lower a widget below its parent. According to the official tk docs:

如果上面的这个参数被省略,那么命令会引发窗口它在堆叠顺序中位于其所有同级之上(它将不会被任何兄弟姐妹遮蔽,并将遮蔽任何兄弟姐妹重叠).如果指定了 aboveThis 则它必须是路径名窗口的兄弟窗口或窗口的后代窗口的兄弟.在这种情况下, raise 命令将插入窗口进入正上方的堆叠顺序(或以上这是窗口的兄弟);这可能最终升高或降低窗口.

If the aboveThis argument is omitted then the command raises window so that it is above all of its siblings in the stacking order (it will not be obscured by any siblings and will obscure any siblings that overlap it). If aboveThis is specified then it must be the path name of a window that is either a sibling of window or the descendant of a sibling of window. In this case the raise command will insert window into the stacking order just above aboveThis (or the ancestor of aboveThis that is a sibling of window); this could end up either raising or lowering window.

(注意,tk raise 命令是 lift() 在最低级别实际调用的)

(NB. the tk raise command is what lift() actually calls at the lowest level)

为了得到你想要的效果,让框架和列表框同级,然后使用in_参数将列表框打包在框架内:

To get the effect you want, make the frame and the listbox siblings, then use the in_ parameter to pack the listboxes inside the frame:

food.grid(row=0, column=1, in_=frame)
pythons.grid(row=1, column=1, pady=10, in_=frame)

这篇关于带有网格布局的 Tkinter 升降方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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