[python] How to set a tkinter window to a constant size

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 500x500 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.

This question is related to python python-3.x tkinter

The answer is


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()


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.


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)

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!


Examples related to python

programming a servo thru a barometer Is there a way to view two blocks of code from the same file simultaneously in Sublime Text? python variable NameError Why my regexp for hyphenated words doesn't work? Comparing a variable with a string python not working when redirecting from bash script is it possible to add colors to python output? Get Public URL for File - Google Cloud Storage - App Engine (Python) Real time face detection OpenCV, Python xlrd.biffh.XLRDError: Excel xlsx file; not supported Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation

Examples related to python-3.x

Could not load dynamic library 'cudart64_101.dll' on tensorflow CPU-only installation Replace specific text with a redacted version using Python Upgrade to python 3.8 using conda "Permission Denied" trying to run Python on Windows 10 Python: 'ModuleNotFoundError' when trying to import module from imported package What is the meaning of "Failed building wheel for X" in pip install? How to downgrade python from 3.7 to 3.6 I can't install pyaudio on Windows? How to solve "error: Microsoft Visual C++ 14.0 is required."? Iterating over arrays in Python 3 How to upgrade Python version to 3.7?

Examples related to tkinter

How can I create a dropdown menu from a List in Tkinter? Windows- Pyinstaller Error "failed to execute script " When App Clicked _tkinter.TclError: no display name and no $DISPLAY environment variable How to set a tkinter window to a constant size PermissionError: [Errno 13] Permission denied matplotlib error - no module named tkinter How do I change the text size in a label widget, python tkinter Tkinter understanding mainloop How to clear/delete the contents of a Tkinter Text widget? tkinter: Open a new window with a button prompt