簡體   English   中英

Tkinter 使多列/單行網格適合框架尺寸

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

我正在嘗試使包含按鈕的框架適合網格。 但我不知道如何根據網格單元大小調整按鈕。

下面是我的代碼:

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)

當我運行我得到的代碼時:

在此處輸入圖像描述

但是當我使用 cursor 來調整 window 的大小時,我得到了:

在此處輸入圖像描述

或者:

在此處輸入圖像描述

例外的 Output 將類似於:

在此處輸入圖像描述

或者:

在此處輸入圖像描述

我認為您需要使用Grid.rowconfigure()Grid.rowconfigure()

在示例代碼中,我評論了你可以這樣做的方式以及配置行的作用。 基本上它需要六個按鈕並將它們放置在兩列(3 行)中,並使用 window 調整大小。 示例代碼:

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