How to set a tkinter window to a constant size

Question:

I’m programming a little game with tkinter and briefly, I’m stuck.

I have a kind od starting menu, in which are two buttons and one label.

If I just create the frame everything is fine, it has the size 500×500 pixels

I want the background not to change when I create the buttons and the labe, but it adapts the size whatever I do. Here is my code:

import tkinter as tk

def startgame():
    pass

mw = tk.Tk()              #Here I tried (1)
mw.title('The game')

back = tk.Frame(master=mw, width=500, height=500, bg='black')
back.pack()

go = tk.Button(master=back, text='Start Game', bg='black', fg='red',
                     command=lambda:startgame()).pack()
close = tk.Button(master=back, text='Quit', bg='black', fg='red',
                     command=lambda:quit()).pack()
info = tk.Label(master=back, text='Made by me!', bg='red',
                         fg='black').pack()

mw.mainloop()

I’ve searched around on stackoverflow and didn’t get anything useful!
I’ve found just one question a bit similar to mine but the answer didn’t work. I tried this:

(1) mw.resizable(width=False, height=False)

I can’t imagine what is the problem, I’m really desperate.

Asked By: Mr. Squiddy

||

Answers:

You turn off pack_propagate by setting pack_propagate(0)

Turning off pack_propagate here basically says don’t let the widgets inside the frame control it’s size. So you’ve set it’s width and height to be 500. Turning off propagate stills allows it to be this size without the widgets changing the size of the frame to fill their respective width / heights which is what would happen normally

To turn off resizing the root window, you can set root.resizable(0, 0), where resizing is allowed in the x and y directions respectively.

To set a maxsize to window, as noted in the other answer you can set the maxsize attribute or minsize although you could just set the geometry of the root window and then turn off resizing. A bit more flexible imo.

Whenever you set grid or pack on a widget it will return None. So, if you want to be able to keep a reference to the widget object you shouldn’t be setting a variabe to a widget where you’re calling grid or pack on it. You should instead set the variable to be the widget Widget(master, ....) and then call pack or grid on the widget instead.

import tkinter as tk

def startgame():

    pass

mw = tk.Tk()

#If you have a large number of widgets, like it looks like you will for your
#game you can specify the attributes for all widgets simply like this.
mw.option_add("*Button.Background", "black")
mw.option_add("*Button.Foreground", "red")

mw.title('The game')
#You can set the geometry attribute to change the root windows size
mw.geometry("500x500") #You want the size of the app to be 500x500
mw.resizable(0, 0) #Don't allow resizing in the x or y direction

back = tk.Frame(master=mw,bg='black')
back.pack_propagate(0) #Don't allow the widgets inside to determine the frame's width / height
back.pack(fill=tk.BOTH, expand=1) #Expand the frame to fill the root window

#Changed variables so you don't have these set to None from .pack()
go = tk.Button(master=back, text='Start Game', command=startgame)
go.pack()
close = tk.Button(master=back, text='Quit', command=mw.destroy)
close.pack()
info = tk.Label(master=back, text='Made by me!', bg='red', fg='black')
info.pack()

mw.mainloop()
Answered By: Pythonista

Try parent_window.maxsize(x,x); to set the maximum size. It shouldn’t get larger even if you set the background, etc.

Edit: use parent_window.minsize(x,x) also to set it to a constant size!

Answered By: Sid Sahay

If you want a window as a whole to have a specific size, you can just give it the size you want with the geometry command. That’s really all you need to do.

For example:

mw.geometry("500x500")

Though, you’ll also want to make sure that the widgets inside the window resize properly, so change how you add the frame to this:

back.pack(fill="both", expand=True)
Answered By: Bryan Oakley

There are 2 solutions for your problem:

  1. Either you set a fixed size of the Tkinter window; mw.geometry('500x500')

OR

  1. Make the Frame adjust to the size of the window automatically;back.place(x = 0, y = 0, relwidth = 1, relheight = 1)

*The second option should be used in place of back.pack()

Answered By: user6763977

Here is the most simple way.

import tkinter as tk

root = tk.Tk()

root.geometry('200x200')
root.resizable(width=0, height=0)

root.mainloop()

I don’t think there is anything to specify.
It’s pretty straight forward.

Answered By: Josh Brown90
from tkinter import *

root = Tk()

width = root.winfo_screenwidth() #get your Windows width size 
height = root.winfo_screenheight() #get your Windows height size 

root.geometry("%dx%d" % (width, height))
root.resizable(False,False)

root.mainloop()

#full size Tkinter
Answered By: Er Shubham Bhagat
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.