如何在调整窗口大小时强制这些ttk应用程序调整大小?

2024-10-03 02:35:01 发布

您现在位置:Python中文网/ 问答频道 /正文

在我尝试使用columnconfigure或rowconfigure方法来调整窗口大小的许多地方都有注释,但似乎都不起作用。我也可以取出mainloop(),我的程序运行完全相同。这让我相信我可能没有正确调用/使用mainloop。这些方法主要是用于组织的,我知道它们现在不是可重用的方法。我本来打算在某个时候解决这个问题,但这不是本文的目的。你知道吗

#!/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()

Tags: fromimportselfdefcolumnallurlsgrid
1条回答
网友
1楼 · 发布于 2024-10-03 02:35:01

在根窗口中放置self.mainframe,但从不给根窗口中的任何行和列赋予权重。您也从未显式地创建根窗口,尽管这不会导致问题。而且,"all"不是有效的行或列。你知道吗

第一步是让你的主框架调整大小,然后你可以集中在框架内的内容。你知道吗

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

注意:如果您将单个框架放在其他小部件(如根窗口)中,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)

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

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

相关问题 更多 >