删除网格 Tkinter 中的某些行/列 [英] Deleting certain rows/columns in a grid Tkinter

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

问题描述

如果我在 SAME 框架中有多个行和列以及多个条目/标签小部件,是否可以只选择其中一个并删除它,同时保留其他小部件.

If I have several rows and columns with several entry/label widgets in the SAME frame, is it possible to choose just a single one of them and delete it whilst leaving the others alone.

例如

class Window():
    def __init__(self):
        self.root = Tk()
        self.win1 = Frame(self.root)
        entry1 = Entry(win1, row=1, column=1)
        entry2 = Entry(win1, row=1, column=2)
        label1 = Label(win1, row=1, column=3)

    def main1(self):
        label2 = Label(win1, row=1, column=4)
        labeln = Label(win1, row=1, column=n)

然后我想从第 3 列开始删除,其中 n 可能是一个随机数.是否可以获取 grid_forget 并插入列数?行也可以吗?

I would then like to remove from column 3 onwards, where n could be a random number. Is it possible to get grid_forget and insert the number of columns? Is it also possible for the rows as well?

更新:或者是否可以简单地返回在init下创建的窗口并删除在main1下创建的那些(但已在同一框架中创建)?

UPDATE: OR is it possible to simply return back to the window created under init and delete those created under main1 (but have been created in the same frame)?

谢谢.

推荐答案

阅读 grid_remove 和 grid_forget 方法;这些将允许您从视图中删除现有的小部件.您还可以销毁小部件,使它们消失.

Read up on the grid_remove and grid_forget methods; these will alow you to remove existing widgets from view. You can also destroy widgets, causing them to vanish.

我已经有一段时间没有这样做了(并且目前没有可以在其上创建示例的计算机),但我认为一个棘手的问题可能是您必须进入并明确设置网格行和/或列的高度、宽度或权重归零以回收空间.不过还是可以的.

It's been a while since I've done this (and don't currently have a computer on which to create an example), but I think the one sticky point may be that you will have to go in and explicitly set the grid row and/or column heights, widths or weights back to zero to reclaim the space. It's quite doable though.

您的另一个选择是通过方法或函数创建所有小部件.然后,您可以非常轻松地销毁和重新创建所有小部件.这对用户来说可能不太愉快,因为整个 UI 都会闪烁".

Your other choice is to create all of your widgets via a method or function. You can then pretty easily destroy and recreate all the widgets. This is probably less pleasant for the user since the whole UI will "blink".

这篇关于删除网格 Tkinter 中的某些行/列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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