当我调整 window 大小时,如何强制这些 ttk 应用程序调整大小?

How do I force these ttk apps to resize when I resize the window?

我在很多地方都有评论,我尝试使用 columnconfigure 或 rowconfigure 方法使 windows 调整大小,但 none 似乎有效。我也可以取出 mainloop() 并且我的程序运行完全一样。这让我相信我可能 calling/using mainloop 不正确。这些方法大多只是为了组织,我知道它们现在不是可重用的方法。我正计划在某个时候修复它,但这不是 post 的目的。

#!/usr/bin/python

from tkinter import *
from tkinter import ttk
import webbrowser
from dictionary import *

class AI():

    def __init__(self):
         self.urls = dictionary.get()
        self.makeMainframe()
        self.makeLabels()
        self.makeDropDown()
        self.makeButton()
        self.makeEntry()
        for child in self.mainframe.winfo_children():
            child.grid_configure(padx=2, pady=2)
            #child.columnconfigure(index='all',weight=1)
            #child.rowconfigure(index='all',weight=1)

    def openMenu(self):
        webbrowser.open(self.urls[self.lunchVar.get()])
        print(self.urls[self.lunchVar.get()])

    def saveChoice(self, event):
        print(self.foodVar.get())

    def makeMainframe(self):
        self.mainframe = ttk.Frame(padding="3 3 12 12")
        self.mainframe.grid(column=0, row=0, sticky=(N,W,E,S))
        #self.mainframe.columnconfigure('all', weight=1)
        #self.mainframe.rowconfigure(index='all', weight=1)

    def makeDropDown(self):
        self.lunchVar = StringVar()

        self.lunchChoice = ttk.Combobox(self.mainframe, textvariable=self.lunchVar, state='readonly')
        self.lunchChoice['values'] = list(self.urls.keys())
        self.lunchChoice.grid(column=2, row=1, sticky=(W, E))

    def makeLabels(self):
        ttk.Label(self.mainframe, text="Today's lunch is from...").grid(column=1, row=1, sticky=(E,W))
        ttk.Label(self.mainframe, text="Click here for a menu  -->").grid(column=1, row=2, sticky=(E,W))
        ttk.Label(self.mainframe, text="What will you be having?").grid(column=1, row=3, sticky=(E,W))

    def makeButton(self):
        self.menu = ttk.Button(self.mainframe, textvariable=self.lunchVar, command=self.openMenu)
        self.menu.grid(column=2, row=2, sticky=(W, E))

    def makeEntry(self):
        self.foodVar = StringVar()
        self.foodChoice = ttk.Entry(self.mainframe, textvariable=self.foodVar)
        self.foodChoice.grid(column=2, row=3, sticky=(E,W))
        self.foodChoice.bind("<Return>", self.saveChoice)
        #self.foodChoice.columnconfigure(index='all', weight=1)

AI=AI()
mainloop()

您将 self.mainframe 放在根 window 中,但您从未给根 window 中的任何行和列赋予权重。您也永远不会显式创建根 window,尽管这不会导致问题。此外,"all" 不是有效的行或列。

第一步是让你的主框架调整大小,然后你可以专注于框架内的内容。

def makeMainframe(self):
    ...
    self.mainframe._root().columnconfigure(0, weight=1)
    self.mainframe._root().rowconfigure(index=0, weight=1)

注意:如果您将单个框架放在其他小部件(例如根 window)中,pack 可以说是更好的选择,因为您没有记住要给行和列赋予权重。您可以替换为:

self.mainframe.grid(column=0, row=0, sticky=(N,W,E,S))
self.mainframe._root().columnconfigure(0, weight=1)
self.mainframe._root().rowconfigure(index=0, weight=1)

...有了这个:

self.mainframe.pack(fill="both", expand=True)

假设您希望条目小部件展开以水平填充 space,您需要对 mainframe 执行相同的操作:

def makeMainframe(self):
    ...
    self.mainframe.grid_columnconfigure(2, weight=1)