从 tkinter 的网格中删除小部件 [英] remove widgets from grid in tkinter

查看:37
本文介绍了从 tkinter 的网格中删除小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 tkinter 框架中有一个网格,用于显示查询结果.它有一个手动更改的日期字段,然后将日期用作查询这些结果的参数.每次更改日期时,显然结果都会发生变化,给出不同数量的行.问题是,如果您第二次得到的行数较少,您仍然会在第一个查询下获得结果,并且会非常混乱.

我的问题是,如何从框架中删除行号大于 6 的所有行(不管里面有什么)?

顺便说一下,我运行的是 Python 3.3.3.提前致谢!

解决方案

在小部件上调用方法 grid_forget 会将其从窗口中移除 -此示例使用父级上的调用 grid_slaves 来查找所有widgets 映射到网格,然后调用grid_info 来了解每个小部件的位置:

<预><代码>>>>导入 tkinter# 创建:>>>a = tkinter.Tk()>>>对于范围内的我(10):... label = tkinter.Label(a, text=str(i))... label.grid(column=0, row=i)# 从屏幕上移除:>>>对于 a.grid_slaves() 中的标签:...如果 int(label.grid_info()["row"]) >6:... label.grid_forget()

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.

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

By the way, I'm running Python 3.3.3. Thanks in advance!

解决方案

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:

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

这篇关于从 tkinter 的网格中删除小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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