Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing window doesn't resize contents in tkinter

Tags:

python

tkinter

tk

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!

like image 470
Dunno Avatar asked Jul 11 '13 16:07

Dunno


People also ask

How do I fix the size of a window in Python?

In Tkinter, minsize() method is used to set the minimum size of the Tkinter window. Using this method a user can set window's initialized size to its minimum size, and still be able to maximize and scale the window larger. This method is used to set the maximum size of the root window.

How do I stop Tkinter from resizing?

Tkinter windows can be resized automatically by hovering and pulling over the window. We can disable the resizable property using the resizable(boolean value) method. We will pass false value to this method which will disable the window to be resized.

Can you resize an image in Tkinter?

The PIL or Pillow library in Python is used for processing images in a Tkinter application. We can use Pillow to open the images, resize them and display in the window. To resize the image, we can use image_resize((width, height) **options) method.

What is the maximum size of a window in Tkinter?

In the same way, maxsize(1200,988) depicts the maximum width, and the height of the Tkinter window must be 1200 and 988, respectively.


1 Answers

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()
like image 92
Noctis Skytower Avatar answered Oct 23 '22 03:10

Noctis Skytower