7

I'm making my first GUI application and I've run into a silly problem. Resizing the main window doesn't resize its contents and leaves blank space. I've read the TKDocs and they only say you should use sticky and column/row weight attributes but I don't really understand how they work. Here's my code (only the part covering widgets, if you think problem isn't here I'll post the rest of it):

from tkinter import *
from tkinter import ttk

root = Tk()

mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)

player1 = StringVar()
player2 = StringVar()
player1.set('Player 1')
player2.set('Player 1')
timer=StringVar()
running=BooleanVar()
running.set(0)
settimer = ttk.Entry(mainframe, width=7, textvariable=timer)
settimer.grid(column=2, row=1, sticky=(N, S))
ttk.Button(mainframe, text="Start", command=start).grid(column=2, row=2, sticky=(N, S))

ttk.Label(mainframe, textvariable=player1, font=TimeFont).grid(column=1, row=3, sticky=(W, S))
ttk.Label(mainframe, textvariable=player2, font=TimeFont).grid(column=3, row=3, sticky=(E, S))

for child in mainframe.winfo_children():
    child.grid_configure(padx=80, pady=10)

root.mainloop()

Thanks for your time!

Dunno
  • 3,632
  • 3
  • 28
  • 43
  • Per [gcb](http://stackoverflow.com/users/183132), you may find [Making the root window resizeable](http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/root-resize.html) to be a useful reference. – Noctis Skytower Mar 29 '17 at 13:45

1 Answers1

11

Maybe this will help you in the right direction. Be sure to configure column/row weights at each level.

import tkinter.ttk
from tkinter.constants import *

class Application(tkinter.ttk.Frame):

    @classmethod
    def main(cls):
        tkinter.NoDefaultRoot()
        root = tkinter.Tk()
        app = cls(root)
        app.grid(sticky=NSEW)
        root.grid_columnconfigure(0, weight=1)
        root.grid_rowconfigure(0, weight=1)
        root.resizable(True, False)
        root.mainloop()

    def __init__(self, root):
        super().__init__(root)
        self.create_variables()
        self.create_widgets()
        self.grid_widgets()
        self.grid_columnconfigure(0, weight=1)

    def create_variables(self):
        self.player1 = tkinter.StringVar(self, 'Player 1')
        self.player2 = tkinter.StringVar(self, 'Player 2')
        self.timer = tkinter.StringVar(self)
        self.running = tkinter.BooleanVar(self)

    def create_widgets(self):
        self.set_timer = tkinter.ttk.Entry(self, textvariable=self.timer)
        self.start = tkinter.ttk.Button(self, text='Start', command=self.start)
        self.display1 = tkinter.ttk.Label(self, textvariable=self.player1)
        self.display2 = tkinter.ttk.Label(self, textvariable=self.player2)

    def grid_widgets(self):
        options = dict(sticky=NSEW, padx=3, pady=4)
        self.set_timer.grid(column=0, row=0, **options)
        self.start.grid(column=0, row=1, **options)
        self.display1.grid(column=0, row=2, **options)
        self.display2.grid(column=0, row=3, **options)

    def start(self):
        timer = self.timer.get()
        self.player1.set(timer)
        self.player2.set(timer)

if __name__ == '__main__':
    Application.main()
Noctis Skytower
  • 21,433
  • 16
  • 79
  • 117
  • Ok, thanks, I kinda hoped I could avoid using classes, they scare me. But there's a first time for everything I guess... – Dunno Jul 11 '13 at 18:09
  • 1
    @Dunno: If you need examples of `tkinter` applications using classes, let me know. When I first started using the GUI library, my code was similar to yours. Now whenever such code comes to my attention, my first thought is that the code needs to be completely rewritten. – Noctis Skytower Jul 11 '13 at 19:10
  • Thanks for the offer, but the internet is full of such examples, I don't want to take too much of your time and practise my google-fu instead ;) – Dunno Jul 11 '13 at 19:30