Tkinter网格动态布局 [英] Tkinter Grid Dynamic Layout

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

问题描述

我想创建一个网格布局,其中的网格会填充第一行,直到窗口中的空间用完为止,并将动态地将项目移动到下面的行中(如文本换行).调整窗口宽度后,网格将调整为适合的大小.不需要调整框的大小.我打算保持每个小盒子的大小,但要更改布局放置每个盒子的位置.

I am wanting to create a grid layout, with a grid that fills the first row until it runs out of space in the window, and will dynamically move items to the row below (like text line-wrapping). As the window width is adjusted, the grid adjusts to fit. The boxes resizing is not desired. I intend to maintain each small box's size, but change where the layout puts each box.

我想通过测量框架的宽度可以实现此功能,并且如果(框数)*(每个框的宽度)超过宽度,请移至下一行.我只是想知道是否有一种我不了解的更好的内置方法.

I imagine this functionality is possible by measuring the width of the frame, and if the (number of boxes)*(width of each box) exceeds the width, move to the next row. I was just wondering if there was a better way built in that I'm not understanding.

如果以上是唯一选择,那么更新该内容的最佳方法是什么?我是否需要在窗口大小调整或其他方面设置事件?看来我不必重新设计布局管理器,这就是这种感觉.我只想检查是否已经内置了类似的功能.网格似乎是一个功能强大的布局管理器,但是我找不到该选项.

If the above is the only option, what is the best way to update that? Do I have to set an event on window resize or something? It seems like I shouldn't have to rework a layout manager, which is what that feels like. I just want to check if similar functionality is already built in. Grid seems like a powerful layout manager, but I have not been able to find that option.

下面的图片描述了我希望使用网格布局在同一帧上使用同一组6个框的行为.

The below pics describes the behavior I want using the same set of 6 boxes on a single frame using grid layout.

窗口足够宽,可以容纳所有6个框,因此它们都适合于第1行.它们随窗口大小的变化而调整.

Window is wide enough to hold all 6 boxes, so they all fit on row 1. They then adjust as window size changes.

推荐答案

如果计划将每个框强制设置为统一大小,则最简单的解决方案是使用文本小部件作为容器,因为它具有内置功能包装.

If you plan on forcing each box to be a uniform size, the simplest solution is to use the text widget as the container since it has the built-in ability to wrap.

这是一个可行的示例.单击添加"按钮以添加其他框.调整窗口大小,以查看它们随着窗口的增大和缩小而自动环绕.

Here is a working example. Click on the "add" button to add additional boxes. Resize the window to see that they automatically wrap as the window grows and shrinks.

import Tkinter as tk
import random

class DynamicGrid(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.text = tk.Text(self, wrap="char", borderwidth=0, highlightthickness=0,
                            state="disabled")
        self.text.pack(fill="both", expand=True)
        self.boxes = []

    def add_box(self, color=None):
        bg = color if color else random.choice(("red", "orange", "green", "blue", "violet"))
        box = tk.Frame(self.text, bd=1, relief="sunken", background=bg,
                       width=100, height=100)
        self.boxes.append(box)
        self.text.configure(state="normal")
        self.text.window_create("end", window=box)
        self.text.configure(state="disabled")

class Example(object):
    def __init__(self):
        self.root = tk.Tk()
        self.dg = DynamicGrid(self.root, width=500, height=200)
        add_button  = tk.Button(self.root, text="Add", command=self.dg.add_box)

        add_button.pack()
        self.dg.pack(side="top", fill="both", expand=True)

        # add a few boxes to start
        for i in range(10):
            self.dg.add_box()

    def start(self):
        self.root.mainloop()

Example().start()

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

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