Python 从 tkinter 的网格中删除小部件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23189610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
remove widgets from grid in tkinter
提问by rodrigocf
I have a grid in a tkinter frame which displays query results. It has a date field that is changed manually and then date is used as a parameter on the query for those results. every time the date is changed, obviously the results change, giving a different amount of rows. The problem is that if you get less rows the second time you do it, you will still have results from the first query underneath those and will be very confusing.
我在 tkinter 框架中有一个网格,用于显示查询结果。它有一个手动更改的日期字段,然后将日期用作查询这些结果的参数。每次更改日期时,显然结果都会发生变化,给出不同数量的行。问题是,如果您第二次得到的行数较少,您仍然会在第一个查询下获得结果,并且会非常混乱。
My question is, how can i remove all rows from the frame that have a row number greater than 6 (regardless of what's in it)?
我的问题是,如何从框架中删除所有行号大于 6 的行(不管里面有什么)?
By the way, I'm running Python 3.3.3. Thanks in advance!
顺便说一下,我正在运行 Python 3.3.3。提前致谢!
采纳答案by jsbueno
Calling the method grid_forget
on the widget will remove it from the window -
this example uses the call grid_slaves
on the parent to findout all
widgets mapped to the grid, and then the grid_info
call to learn about
each widget's position:
调用grid_forget
小部件上的方法会将其从窗口中删除 - 此示例使用grid_slaves
对父级的调用来查找映射到网格的所有小部件,然后grid_info
调用以了解每个小部件的位置:
>>> import tkinter
# create:
>>> a = tkinter.Tk()
>>> for i in range(10):
... label = tkinter.Label(a, text=str(i))
... label.grid(column=0, row=i)
# remove from screen:
>>> for label in a.grid_slaves():
... if int(label.grid_info()["row"]) > 6:
... label.grid_forget()