Python 在调整大小时用小部件填充空间

Python 在调整大小时用小部件填充空间,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,当调整“根”的大小时,如何使“标签”和“文本”小部件填满所有空间 如果可能的话,我想使用“网格”方法 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() 当我尝试在课堂上使用时,它不起作用 Applic

当调整“根”的大小时,如何使“标签”和“文本”小部件填满所有空间

如果可能的话,我想使用“网格”方法

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()
当我尝试在课堂上使用时,它不起作用

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)

需要两个步骤

  • 调用
    grid\u rowconfigure
    grid\u columnconfigure
    来设置每个网格行和列的权重。权重为零的行和列在调整大小期间根本不会拉伸。这是默认行为

  • 在小部件上调用
    grid
    时,指定
    sticky
    参数,以便小部件在其行或列拉伸时拉伸


  • 在WorkPlace对象的特定情况下,您需要配置根对象和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()
    

    请将代码编辑到原始帖子中,而不是粘贴到评论中。我的机器上没有
    菜单
    工作区
    模块。你从哪里得到的?谢谢你发布其他模块。我想我看到了问题所在。
    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()