如何防止窗口被tkinter调整大小?

时间:2021-06-30 23:22:04

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

6 个解决方案

#1


93  

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.

此代码使窗口具有用户无法更改Tk()窗口的维度的条件,并且禁用最大化按钮。

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:

在程序中,您可以使用@ carpetsmokers的答案来更改窗口尺寸,或者这样做:

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

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

对您来说,将其实现到代码中应该相当容易。:)

#2


29  

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

您可以使用minsize和maxsize设置最小和最大大小,例如:

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.

将给您的窗口一个固定的宽度和高度666像素。

Or, just using minsize

或者,只是用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.

确保你的窗口总是至少666像素,但是用户仍然可以扩展窗口。

#3


4  

You could use:

您可以使用:

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

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

在代码的底部设置固定窗口大小。

#4


0  

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.

优点是你打字更少。

#5


0  

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

这是你得到的第一个答案。tk支持最小和最大大小

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.

我算出来了,不过先试试第一个。用python3 tk。

#6


0  

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

下面的代码将在调用root = tk.Tk()之前将其修改为其大小:

root.resizable(False, False)

#1


93  

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.

此代码使窗口具有用户无法更改Tk()窗口的维度的条件,并且禁用最大化按钮。

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:

在程序中,您可以使用@ carpetsmokers的答案来更改窗口尺寸,或者这样做:

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

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

对您来说,将其实现到代码中应该相当容易。:)

#2


29  

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

您可以使用minsize和maxsize设置最小和最大大小,例如:

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.

将给您的窗口一个固定的宽度和高度666像素。

Or, just using minsize

或者,只是用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.

确保你的窗口总是至少666像素,但是用户仍然可以扩展窗口。

#3


4  

You could use:

您可以使用:

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

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

在代码的底部设置固定窗口大小。

#4


0  

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.

优点是你打字更少。

#5


0  

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

这是你得到的第一个答案。tk支持最小和最大大小

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.

我算出来了,不过先试试第一个。用python3 tk。

#6


0  

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

下面的代码将在调用root = tk.Tk()之前将其修改为其大小:

root.resizable(False, False)