<tfoot id='DkheC'></tfoot>

    <small id='DkheC'></small><noframes id='DkheC'>

      • <bdo id='DkheC'></bdo><ul id='DkheC'></ul>
    1. <legend id='DkheC'><style id='DkheC'><dir id='DkheC'><q id='DkheC'></q></dir></style></legend>

    2. <i id='DkheC'><tr id='DkheC'><dt id='DkheC'><q id='DkheC'><span id='DkheC'><b id='DkheC'><form id='DkheC'><ins id='DkheC'></ins><ul id='DkheC'></ul><sub id='DkheC'></sub></form><legend id='DkheC'></legend><bdo id='DkheC'><pre id='DkheC'><center id='DkheC'></center></pre></bdo></b><th id='DkheC'></th></span></q></dt></tr></i><div id='DkheC'><tfoot id='DkheC'></tfoot><dl id='DkheC'><fieldset id='DkheC'></fieldset></dl></div>

      如何在 tkinter 中创建一个自调整大小的按钮网格?

      时间:2023-07-24
      <i id='aydFu'><tr id='aydFu'><dt id='aydFu'><q id='aydFu'><span id='aydFu'><b id='aydFu'><form id='aydFu'><ins id='aydFu'></ins><ul id='aydFu'></ul><sub id='aydFu'></sub></form><legend id='aydFu'></legend><bdo id='aydFu'><pre id='aydFu'><center id='aydFu'></center></pre></bdo></b><th id='aydFu'></th></span></q></dt></tr></i><div id='aydFu'><tfoot id='aydFu'></tfoot><dl id='aydFu'><fieldset id='aydFu'></fieldset></dl></div>
      • <bdo id='aydFu'></bdo><ul id='aydFu'></ul>
          • <tfoot id='aydFu'></tfoot>

            <small id='aydFu'></small><noframes id='aydFu'>

            <legend id='aydFu'><style id='aydFu'><dir id='aydFu'><q id='aydFu'></q></dir></style></legend>

                <tbody id='aydFu'></tbody>
                本文介绍了如何在 tkinter 中创建一个自调整大小的按钮网格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试使用 Tkinter 创建一个按钮网格(以实现可点击的单元格效果).

                I am trying to create a grid of buttons(in order to achieve the clickable cell effect) with Tkinter.

                我的主要问题是我无法使 grid 和按钮自动调整大小并适合父窗口.

                My main problem is that I cannot make the grid and the buttons autoresize and fit the parent window.

                例如,当我在网格上有大量按钮时,我没有缩小按钮以使网格适合窗口内部,而是得到一个超出屏幕的拉伸框架.

                For example, when I have a high number of buttons on the grid, instead of shrinking the buttons so that the grid fits inside the window, I get a stretched frame that goes off screen.

                我正在寻找的效果是网格填充所有可用空间,然后调整其单元格的大小以适应该空间.我已阅读文档,但我仍然无法弄清楚如何使其工作.

                The effect that I am looking for is the grid filling all available space, then resizing its cells to fit within that space. I have read at the documentation, but I still cannot figure out how to make it work.

                这是我的出发点的基本代码:

                This is the basic code which is my starting point:

                def __init__(self):
                    root = Tk()
                    frame = Frame(root)
                    frame.grid()
                
                    #some widgets get added in the first 6 rows of the frame's grid          
                
                    #initialize grid
                    grid = Frame(frame)  
                    grid.grid(sticky=N+S+E+W, column=0, row=7, columnspan=2)
                
                    #example values
                    for x in range(60):
                        for y in range(30):
                            btn = Button(grid)
                            btn.grid(column=x, row=y)
                
                    root.mainloop()
                

                推荐答案

                您需要将行和列配置为具有非零权重,以便它们占用额外的空间:

                You need to configure the rows and columns to have a non-zero weight so that they will take up the extra space:

                grid.columnconfigure(tuple(range(60)), weight=1)
                grid.rowconfigure(tuple(range(30)), weight=1)
                

                您还需要配置按钮,以便它们展开以填充单元格:

                You also need to configure your buttons so that they will expand to fill the cell:

                btn.grid(column=x, row=y, sticky="news")
                

                这必须一直进行,所以这里有一个完整的例子:

                This has to be done all the way up, so here is a full example:

                from tkinter import *
                
                root = Tk()
                frame = Frame(root)
                root.rowconfigure(0, weight=1)
                root.columnconfigure(0, weight=1)
                frame.grid(row=0, column=0, sticky="news")
                grid = Frame(frame)
                grid.grid(sticky="news", column=0, row=7, columnspan=2)
                frame.rowconfigure(7, weight=1)
                frame.columnconfigure(0, weight=1)
                
                #example values
                for x in range(10):
                    for y in range(5):
                        btn = Button(frame)
                        btn.grid(column=x, row=y, sticky="news")
                
                frame.columnconfigure(tuple(range(10)), weight=1)
                frame.rowconfigure(tuple(range(5)), weight=1)
                
                root.mainloop()
                

                这篇关于如何在 tkinter 中创建一个自调整大小的按钮网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Python 中的 2D 网格数据可视化 下一篇:使用 matplotlib 制作 2D 像素图

                相关文章

                  <tfoot id='RkH1w'></tfoot>

                      <bdo id='RkH1w'></bdo><ul id='RkH1w'></ul>

                    <small id='RkH1w'></small><noframes id='RkH1w'>

                  1. <i id='RkH1w'><tr id='RkH1w'><dt id='RkH1w'><q id='RkH1w'><span id='RkH1w'><b id='RkH1w'><form id='RkH1w'><ins id='RkH1w'></ins><ul id='RkH1w'></ul><sub id='RkH1w'></sub></form><legend id='RkH1w'></legend><bdo id='RkH1w'><pre id='RkH1w'><center id='RkH1w'></center></pre></bdo></b><th id='RkH1w'></th></span></q></dt></tr></i><div id='RkH1w'><tfoot id='RkH1w'></tfoot><dl id='RkH1w'><fieldset id='RkH1w'></fieldset></dl></div>

                    <legend id='RkH1w'><style id='RkH1w'><dir id='RkH1w'><q id='RkH1w'></q></dir></style></legend>