在调整大小时用小部件填充 space

Fill space with widgets on resize

如何在 'root' 调整大小时让 'label' 和 'text' 小部件填充所有 space?

如果可能的话,我想使用 'grid' 方法。

from tkinter import *
root = Tk()

root.resizable(width = True, height = True)
label = Label(root, text = "Text")
label.grid()

text = Text(root)
text.grid()
root.mainloop()

当我尝试在 class 中使用时它不起作用。

Application.py

from tkinter import *
import menu
import workPlace

class Application(Frame):
    def __init__(self, boss = None):
        Frame.__init__(self)

        self.master.title("Title")

        self.master.grid_columnconfigure(0, weight = 1)
        self.master.grid_rowconfigure(1, weight = 1)
        self.master.resizable(width = True, height = True)

        self.menu = menu.MenuBar(self)
        self.menu.grid(sticky = W)

        self.workPlace = workPlace.WorkPlace(self)
        self.workPlace.grid(sticky = "NEWS")

if __name__ == "__main__":
    Application().mainloop()

workPlace.py

from tkinter import *
import tkinter.scrolledtext as scr 

class WorkPlace(Frame):
    def __init__(self, boss = None):
        Frame.__init__(self)

        self.scrText = scr.ScrolledText(self)
        self.scrText.grid()

        self.label = Label(self,text = "Label")
        self.label.grid()

menu.py

from tkinter import *

class MenuBar(Frame):
    def __init__(self, boss = None):
        Frame.__init__(self)

        fileMenu = Menubutton(self, text = 'File')
        fileMenu.grid(row = 0, column = 0)
        me1 = Menu(fileMenu)

        fileMenu.configure(menu = me1)

        findMenu = Menubutton(self, text = 'Find')
        findMenu.grid(row = 0, column = 1)
        me1 = Menu(findMenu)
        findMenu.configure(menu = me1)

        optionMenu = Menubutton(self, text = 'Option')
        optionMenu.grid(row = 0, column = 2)
        me1 = Menu(optionMenu)

        optionMenu.configure(menu = me1)

需要两个步骤。

  1. 调用grid_rowconfiguregrid_columnconfigure来设置网格每个行和列的权重。权重为零的行和列在调整大小时根本不会拉伸。这是默认行为。

  2. 在您的小部件上调用 grid 时,指定 sticky 参数,以便小部件在其行或列拉伸时拉伸。

from tkinter import *
root = Tk()

root.grid_columnconfigure(0, weight=1)
#uncomment this line if you want the Label widget to stretch vertically.
#or leave it as is if you only want the Text widget to stretch.
#root.grid_rowconfigure(0, weight=1)
root.grid_rowconfigure(1, weight=1)

root.resizable(width = True, height = True)
label = Label(root, text = "Text")
label.grid(sticky="NEWS")

text = Text(root)
text.grid(sticky="NEWS")
root.mainloop()

在您的 WorkPlace 对象的特定情况下,您需要 configure 根对象和 WorkPlace 对象,并且您需要为 WorkPlace 对象及其子对象指定粘性。

from tkinter import *
import tkinter.scrolledtext as scr 

class WorkPlace(Frame):
    def __init__(self, boss = None):
        Frame.__init__(self)

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

        self.scrText = scr.ScrolledText(self)
        self.scrText.grid(sticky="NEWS")

        self.label = Label(self,text = "Label")
        self.label.grid()

如果您可以使用 pack 而不是 grid,您可以

from tkinter import *
root = Tk()

root.resizable(width = True, height = True)
label = Label(root, text = "Text")
label.pack(fill=X)
text = Text(root)
text.pack(fill=BOTH, expand=True)
root.mainloop()