[python] How can I prevent a window from being resized with tkinter?

I have a program which creates a window where a message is displayed according to a check box.

How can I make the window size constant when the message is displayed and the message is not displayed?

from Tkinter import *

class App:
    def __init__(self,master):
        self.var = IntVar()
        frame = Frame(master)
        frame.grid()
        f2 = Frame(master,width=200,height=100)
        f2.grid(row=0,column=1)
        button = Checkbutton(frame,text='show',variable=self.var,command=self.fx)
        button.grid(row=0,column=0)
        msg2="""I feel bound to give them full satisfaction on this point"""
        self.v= Message(f2,text=msg2)
    def fx(self):
        if self.var.get():
            self.v.grid(column=1,row=0,sticky=N)
        else:
            self.v.grid_remove()

top = Tk()
app = App(top)            
top.mainloop()

This question is related to python python-2.7 tkinter

The answer is


You could use:

parentWindow.maxsize(#,#);
parentWindow.minsize(x,x);

At the bottom of your code to set the fixed window size.


Traceback (most recent call last):
  File "tkwindowwithlabel5.py", line 23, in <module>
    main()
  File "tkwindowwithlabel5.py", line 16, in main
    window.resizeable(width = True, height =True)
  File "/usr/lib/python3.4/tkinter/__init__.py", line 1935, in                
  __getattr__
    return getattr(self.tk, attr)
AttributeError: 'tkapp' object has no attribute 'resizeable'

is what you will get with the first answer. tk does support min and max size

window.minsize(width = X, height = x)
window.maxsize(width = X, height = x)

i figured it out but just trying the first one. using python3 with tk.


Below code will fix root = tk.Tk() to its size before it was called:

root.resizable(False, False)

This is a variant of an existing solution already provided above:

import tkinter as tk

root = tk.Tk()
root.resizable(0, 0)
root.mainloop()

The advantage is that you type less.


You can use the minsize and maxsize to set a minimum & maximum size, for example:

def __init__(self,master):
    master.minsize(width=666, height=666)
    master.maxsize(width=666, height=666)

Will give your window a fixed width & height of 666 pixels.

Or, just using minsize

def __init__(self,master):
    master.minsize(width=666, height=666)

Will make sure your window is always at least 666 pixels large, but the user can still expand the window.


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-2.7

Numpy, multiply array with scalar Not able to install Python packages [SSL: TLSV1_ALERT_PROTOCOL_VERSION] How to create a new text file using Python Could not find a version that satisfies the requirement tensorflow Python: Pandas pd.read_excel giving ImportError: Install xlrd >= 0.9.0 for Excel support Display/Print one column from a DataFrame of Series in Pandas How to calculate 1st and 3rd quartiles? How can I read pdf in python? How to completely uninstall python 2.7.13 on Ubuntu 16.04 Check key exist in python dict

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