0

I have comments in a number of places where I've tried to use the columnconfigure or rowconfigure methods to cause the windows to resize, but none of it seems to be working. I can also take out mainloop() and my program runs exactly the same. This leads me to believe I'm perhaps not calling/using mainloop correctly. The methods are mostly just for organization, and I know they're not reusable methods right now. I was planning on maybe fixing that at some point, but that's not the purpose of this 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()
JustAGuy
  • 13
  • 1
  • 6

1 Answers1

0

You place self.mainframe in the root window, but you never give any rows and columns in the root window a weight. You also never explicitly create a root window, though that isn't contributing to the problem. Also, "all" isn't a valid row or column.

The first step is to get your main frame to resize, then you can focus on the contents inside the frame.

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

Note: in the case where you're putting a single frame inside some other widget (such as the root window), pack is arguably a better choice because you don't have to remember to give weight to the rows and columns. You can replace this:

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)

... with this:

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

Assuming that you want the entry widgets to expand to fill the space horizontally, you need to do the same with mainframe:

def makeMainframe(self):
    ...
    self.mainframe.grid_columnconfigure(2, weight=1)
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • So that definitely worked. Could you explain to me what pack() is doing, and what you mean by I never explicitly create a root window? Is that something I should be doing? Thanks a ton! – JustAGuy Jul 20 '16 at 16:22
  • @JustAGuy: http://stackoverflow.com/a/3968033/7432. All Tkinter GUIs require a root window. The root window is an instance of `Tk()`, which is created implicitly if you don't create it explicitly. PEP8 says explicit is better than implicit. http://effbot.org/tkinterbook/tkinter-hello-tkinter.htm – Bryan Oakley Jul 20 '16 at 16:28
  • So I would just need to put something like `root = Tk()` above my class? and then where would that need to be implemented/used in the rest of the code, if anywhere? Thanks again, sorry for asking so much – JustAGuy Jul 20 '16 at 16:34