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

前端 未结 6 1322
野趣味
野趣味 2020-12-01 03:18

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

相关标签:
6条回答
  • 2020-12-01 03:52

    You could use:

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

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

    0 讨论(0)
  • 2020-12-01 03:55

    This code makes a window with the conditions that the user cannot change the dimensions of the Tk() window, and also disables the maximise button.

    import tkinter as tk
    
    root = tk.Tk()
    root.resizable(width=False, height=False)
    root.mainloop()
    

    Within the program you can change the window dimensions with @Carpetsmoker's answer, or by doing this:

    root.geometry('{}x{}'.format(<widthpixels>, <heightpixels>))
    

    It should be fairly easy for you to implement that into your code. :)

    0 讨论(0)
  • 2020-12-01 03:57

    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.

    0 讨论(0)
  • 2020-12-01 04:01

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

    root.resizable(False, False)
    
    0 讨论(0)
  • 2020-12-01 04:05

    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.

    0 讨论(0)
  • 2020-12-01 04:07
    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.

    0 讨论(0)
提交回复
热议问题