简体   繁体   English

Tkinter 使多列/单行网格适合框架尺寸

[英]Tkinter Make Multiple Column/Single Row Grid Fit Frame Size

I am trying to fit frame containing buttons to the grid.我正在尝试使包含按钮的框架适合网格。 But I don't know how to recize the buttons depending on the grid cells size.但我不知道如何根据网格单元大小调整按钮。

Below is my code:下面是我的代码:

def __init__(self, parent, controller):
    tk.Frame.__init__(self,parent)
    
    IWidth = self.winfo_width()
    FrmBtn= tk.Frame(self,bg='black',width = IWidth)
    FrmBtn.pack(expand=True, fill=BOTH,pady=10,padx=10)
    FrmBtn.grid_rowconfigure(0, weight=1)
    button = tk.Button(FrmBtn, text="Tab1",
                        command=lambda: controller.show_frame(TestPage1))
    button.grid(row=0,column=0,sticky="W")
    
    button1 = tk.Button(FrmBtn, text="Tab2",
                        command=lambda: controller.show_frame(TestPage1))
    button1.grid(row=0,column=1,sticky="W")
    
    button3 = tk.Button(FrmBtn, text="Tab3",
                        command=lambda: controller.show_frame(TestPage1))
    button3.grid(row=0,column=2,sticky="W")
    
    button4 = tk.Button(FrmBtn, text="Tab4",
                        command=lambda: controller.show_frame(TestPage1))
    button4.grid(row=0,column=3,sticky="W")
    FrmW= tk.Frame(self,bg='grey',width = IWidth)
    FrmW.pack(expand=True, fill=BOTH,pady=10,padx=10)

When I run the code I got:当我运行我得到的代码时:

在此处输入图像描述

but when I use cursor to resize the window I got:但是当我使用 cursor 来调整 window 的大小时,我得到了:

在此处输入图像描述

or:或者:

在此处输入图像描述

The Excepted Output would be stuff like:例外的 Output 将类似于:

在此处输入图像描述

or:或者:

在此处输入图像描述

I think you need to use Grid.rowconfigure() and Grid.rowconfigure() .我认为您需要使用Grid.rowconfigure()Grid.rowconfigure()

In example code I commented the way yo can do it and what configure line do.在示例代码中,我评论了你可以这样做的方式以及配置行的作用。 Basicly it takes six buttons and place them in two columns (3 rows), and they resize with window.基本上它需要六个按钮并将它们放置在两列(3 行)中,并使用 window 调整大小。 Example code:示例代码:

import tkinter as tk
from tkinter import Grid, Button

root = tk.Tk()
root.title("resize button")
root.geometry("500x500")


# here you need to put on what do you want to use row configure, index(row) and weight
Grid.rowconfigure(root, 0, weight=1)  # we use on root, row=0 weight=1
Grid.columnconfigure(root, 0, weight=1)

#configure 2nd row
Grid.rowconfigure(root, 1, weight=1)


#configure 3rd row
Grid.rowconfigure(root, 2, weight=1)

#configure 2nd column
Grid.columnconfigure(root, 1, weight=1)

button1 = Button(root, text="Button1")
button2 = Button(root, text="Button2")
button3 = Button(root, text="Button3")

button1.grid(row=0, column=0, sticky="nsew")
button2.grid(row=1, column=0, sticky="nsew")
button3.grid(row=2, column=0, sticky="nsew")

button1_1 = Button(root, text="Button1_1")
button2_1 = Button(root, text="Button2_1")
button3_1 = Button(root, text="Button3_1")

button1_1.grid(row=0, column=1, sticky="nsew")
button2_1.grid(row=1, column=1, sticky="nsew")
button3_1.grid(row=2, column=1, sticky="nsew")

root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM