tkinter 网格列配置和行配置不起作用

tkinker grid columconfigure and row configure not working

您好,我需要有关行配置和列配置的帮助。该程序是从框架继承的tk构建的,然后是从框架继承的笔记本(标签),然后是从笔记本继承的网格。直到笔记本,一切都在按照我想要的方式展开。

我的问题是网格系统没有展开。我将带有重量的行和列配置放在里面,但它不起作用。 1).不知道我引用的对不对

self.tabControl.columnconfigure(employee, weight = 1)

2).由于前 2 个级别有 pack(),我是否必须对 root 执行相同的列和行配置?如果我需要在哪里以及如何放置它?

程序如下:

import tkinter as tk
from tkinter import ttk

#upper tabs
upper_tabs = ["Final", "Requests"]
tabs = {}

Employees = ["A", "B", "C", "D", "E", "F", "G", ]
Days= ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday",   
"Saturday", "Sunday"]

class Application(ttk.Frame): #inherent from frame.

    def __init__(self, parent):
        tk.Frame.__init__(self, parent, bg="ivory2", bd=2,       
          relief=tk.RAISED )
        self.parent = parent
        self.pack(fill=tk.BOTH, expand=1)

        self.GUI()

    def GUI(self): #the function that runs all the GUI functions.

        self.create_tabs()
        self.buttons()

        for name in upper_tabs:
            self.create_grid(name)

####---------create grid_GUI---------------------####

    def create_tabs(self):
        self.tabControl = ttk.Notebook(self, width="1100", height= 
        "500") #Create Tab Control

        for name in upper_tabs:
            self.tab=ttk.Frame(self.tabControl)# Create a tab
            self.tabControl.add(self.tab, text=name)      # Add the tab
            tabs[name] = self.tab
            self.tabControl.pack(expand=1, fill=tk.BOTH)  # Pack to      
            make visible

    def create_grid(self, name):

        for employee in range(len(Employees)+2):
            self.tabControl.columnconfigure(employee, weight = 1)
            for day in range(len(Days)+2):
                self.tabControl.rowconfigure( day, weight = 1)
                self.label = tk.Label(tabs[name], relief="ridge",    
                width=12, height=3)
                self.label.grid(row=employee, column=day, )



    def buttons(self):

        self.button=tk.Button(self, text="Caluculate", bg="salmon", )
        self.button.pack(side= "right")

def main():

    root = tk.Tk()
    root.title("class basic window")
    root.geometry("1200x600")
    root.config(background="LightBlue4")
    app = Application(root)
    root.mainloop()

if __name__ == '__main__':
    main()

感谢您的帮助。

您正在为标签设置一个明确的大小,并且要让它们通过您必须指定的选项卡更改大小 sticky='nsew'

然后,您应该对每个选项卡进行行和列配置 tabControl

我进行了相应的更改,并在笔记本和按钮中添加了填充,使布局更加稀疏。笔记本现在将扩展为 window。我在应用程序框架的行配置中设置了一个最小高度,这样当 window 缩小时按钮不会消失。

我将所有几何图形更改为 grid(),因为当布局变得更复杂时它会更容易。

我评论了下面代码中的重大变化:

import tkinter as tk
from tkinter import ttk

#upper tabs
upper_tabs = ["Final", "Requests"]
tabs = {}

Employees = ["A", "B", "C", "D", "E", "F", "G", ]
Days= ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
       "Saturday", "Sunday"]

class Application(tk.Frame): #inherent from frame.
    def __init__(self, parent):
        tk.Frame.__init__(self, parent, bg="tan")
        self.parent = parent
        self.pack(fill=tk.BOTH, expand=1)
        # Configure self to fill parent when size changes
        self.rowconfigure([0,1],weight=1, minsize=40)
            # Minsize will keep row=1 at least 50 pixels high or the
            # button will disappear when the frame shrinks
        self.columnconfigure([0,1],weight=1)
        self.GUI()

    def GUI(self): #the function that runs all the GUI functions.
        self.create_tabs()
        self.buttons()
        for name in upper_tabs:
            self.create_grid(name)

####---------create grid_GUI---------------------####

    def create_tabs(self):
        self.tabControl = ttk.Notebook(self, width=1100, height=500)
        for name in upper_tabs:
            self.tab=tk.Frame(self.tabControl, bg='thistle')
            self.tabControl.add(self.tab, text=name)
            tabs[name] = self.tab
            # Set sticky to fill up available space and padding for layout
            self.tabControl.grid(row=0, column=0, padx=10, pady=10, sticky='nsew')  

    def create_grid(self, name):
        for employee in range(len(Employees)+2):
            # columnconfigure tab instead of tabControl
            tabs[name].columnconfigure(employee, weight = 1)
            for day in range(len(Days)+2):
                # rowconfigure tab instead of tabControl
                tabs[name].rowconfigure(day, weight=1)
                self.label = tk.Label(tabs[name], relief="ridge", width=12, height=3)
                # Set sticky to fill available space
                self.label.grid(row=employee, column=day, sticky='nsew')

    def buttons(self):
        self.button=tk.Button(self, text="Caluculate", bg="salmon", )
        # Adding padding for layout
        self.button.grid(row=1, column=0, padx=(0,10), pady=(0,10), sticky='e')

def main():
    root = tk.Tk()
    root.title("class basic window")
    root.config(background="LightBlue4")
    app = Application(root)
    root.mainloop()

if __name__ == '__main__':
    main()

这是你的目标吗?