定位和组织 Tkinter 框架

Orient and Organize Tkinter frames

我想知道如何创建与网格对齐并填充 space 的框架。我很难掌握 window 中的框架方向。我可以制作所有的框架,但如果我打包,它们都在左边或 line/on 右边。我玩了一下,决定使用 grid()。我设置了 window 几何图形,然后使用网格作为框架。但是,我的框架全部折叠到左上角,或者我的初始框架填满了整个 space 而另外两个挤在边缘。我尝试使用 1024 和 768 的 window 几何图形,然后将我的第一帧的宽度和高度分配为 512 和 384。然后将接下来的两个设置为宽度 512 和高度 192。我还在其他文章中看到weight 很重要,并尝试了代码迭代来改变它,但没有成功。 Rowspan 和 columnspan 帮助我显示背景颜色,这样我就可以想象 space 被每个颜色占据。

无论如何,代码如下:

class twinsplay:
    
    def __init__(self):
        #Window
        self.window =  Tk()
        self.window.geometry('1024x768')
        self.window.grid_rowconfigure(1, weight=1)
        self.window.grid_columnconfigure(1, weight=1)
        
        #window title
        self.window.title('Live Feed')
        
        #frames and labels
        self.frame_left = Frame(self.window, background='magenta')
        self.frame_left.grid(row=0, column=0, padx=5, pady=5, rowspan=3, columnspan=3, sticky=NSEW)
        self.label_left = Label(self.frame_left, text = 'Weather Data', justify=LEFT)
        self.label_left.grid(row=0,column=0, sticky=NSEW)
        
        self.frame_tr = Frame(self.window, background='green')
        self.frame_tr.grid(row=0, column=3, padx=5, pady=5, rowspan=3, columnspan=2, sticky=NSEW)
        self.label_tr = Label(self.frame_tr, text = 'Solar Data', justify = RIGHT)
        self.label_tr.grid(row=0, column=512, sticky=NSEW)
        
        self.frame_br = Frame(self.window, background='yellow')
        self.frame_br.grid(padx=5, pady=5, rowspan=2, columnspan=2, sticky=NSEW)
        self.label_br = Label(self.frame_br, text = 'News', justify = RIGHT)
        self.label_br.grid(row=0, column=512, sticky=NSEW)
        
        #main
        self.window.mainloop()
    ```

不知道我是否理解你的问题。

您的 frameswindow 中使用全尺寸,但 frames 中的元素被折叠。您可能需要将 grid_rowconfigure/grid_columnconfigureframe_leftframe_trframe_br 一起使用以调整 frames.[=27 内网格中元素的大小=]

我将元素移动到行和列 0 因为空 rows/columns 没有大小并且没有意义放入列 512

from tkinter import *

class twinsplay:
    
    def __init__(self):
        self.window =  Tk()
        self.window.geometry('1024x768')
        
        self.window.grid_rowconfigure(0, weight=1)
        self.window.grid_columnconfigure(0, weight=1)

        #window title
        self.window.title('Live Feed')


        #frames and labels
        self.frame_left = Frame(self.window, background='magenta')
        self.frame_left.grid(row=0, column=0, padx=5, pady=5, sticky=NSEW)


        self.label_left = Label(self.frame_left, text = 'Weather Data', justify=LEFT, bg='red')
        self.label_left.grid(row=0,column=0, sticky=NSEW)

        self.frame_left.grid_rowconfigure(0, weight=1)
        self.frame_left.grid_columnconfigure(0, weight=1)

        # ---
        
        
        self.frame_tr = Frame(self.window, background='green')
        self.frame_tr.grid(row=0, column=1, padx=5, pady=5, sticky=NSEW)
        
        self.label_tr = Label(self.frame_tr, text = 'Solar Data', justify = RIGHT, bg='green')
        self.label_tr.grid(row=0, column=0, sticky=NSEW)

        self.frame_tr.grid_rowconfigure(0, weight=1)
        self.frame_tr.grid_columnconfigure(0, weight=1)

        # ---
        
        self.frame_br = Frame(self.window, background='yellow')
        self.frame_br.grid(row=1, column=0, padx=5, pady=5, columnspan=2, sticky=NSEW)
        
        self.label_br = Label(self.frame_br, text = 'News', justify = RIGHT, bg='yellow')
        self.label_br.grid(row=0, column=0, sticky=NSEW)

        self.frame_br.grid_rowconfigure(0, weight=1)
        self.frame_br.grid_columnconfigure(0, weight=1)

        #main
        self.window.mainloop()
        
twinsplay()        


但是,如果您尝试做一些不同的事情,那么也许您必须将 grid_rowconfigure/grid_columnconfigure 与其他 rows/columns 一起使用 - 您可以多次使用不同的 rows/columns ].


编辑:

from tkinter import *

class twinsplay:
    
    def __init__(self):
        self.window =  Tk()
        self.window.geometry('1024x768')
        
        # left and right column will use the same size
        self.window.grid_columnconfigure(0, weight=1)
        self.window.grid_columnconfigure(1, weight=1)
        
        # top and bottom rows will use the same size
        self.window.grid_rowconfigure(0, weight=1)
        self.window.grid_rowconfigure(1, weight=1)
                
        #window title
        self.window.title('Live Feed')

        #frames and labels
        
        # frame will use 2 rows 
        self.frame_left = Frame(self.window, background='magenta')
        self.frame_left.grid(row=0, column=0, padx=5, pady=5, sticky=NSEW, rowspan=2)


        self.label_left = Label(self.frame_left, text = 'Weather Data', justify=LEFT, bg='red')
        self.label_left.grid(row=0,column=0, sticky=NSEW)

        self.frame_left.grid_rowconfigure(0, weight=1)
        self.frame_left.grid_columnconfigure(0, weight=1)

        # ---
        
        self.frame_tr = Frame(self.window, background='green')
        self.frame_tr.grid(row=0, column=1, padx=5, pady=5, sticky=NSEW)
        
        self.label_tr = Label(self.frame_tr, text = 'Solar Data', justify = RIGHT, bg='green')
        self.label_tr.grid(row=0, column=0, sticky=NSEW)

        self.frame_tr.grid_rowconfigure(0, weight=1)
        self.frame_tr.grid_columnconfigure(0, weight=1)

        # ---
        
        self.frame_br = Frame(self.window, background='yellow')
        self.frame_br.grid(row=1, column=1, padx=5, pady=5, columnspan=2, sticky=NSEW)
        
        self.label_br = Label(self.frame_br, text = 'News', justify = RIGHT, bg='yellow')
        self.label_br.grid(row=0, column=0, sticky=NSEW)

        self.frame_br.grid_rowconfigure(0, weight=1)
        self.frame_br.grid_columnconfigure(0, weight=1)

        #main
        self.window.mainloop()
        
twinsplay()