Python 如何防止使用 tkinter 调整窗口大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21958534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I prevent a window from being resized with tkinter?
提问by mahes
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()
采纳答案by Alex Thornton
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:
在程序中,您可以使用@Carpetsmoker 的答案或通过执行以下操作来更改窗口尺寸:
root.geometry('{}x{}'.format(<widthpixels>, <heightpixels>))
It should be fairly easy for you to implement that into your code. :)
您应该很容易将其实现到您的代码中。:)
回答by Martin Tournoij
You can use the minsizeand maxsizeto 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 least666 pixels large, but the user can still expand the window.
将确保您的窗口始终至少为666 像素大,但用户仍然可以扩展窗口。
回答by Pamal Mangat
You could use:
你可以使用:
parentWindow.maxsize(#,#);
parentWindow.minsize(x,x);
At the bottom of your code to set the fixed window size.
在代码底部设置固定窗口大小。
回答by Billal Begueradj
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.
优点是打字少。
回答by nate
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 一起使用。
回答by Nae
Below code will fix root = tk.Tk()to its size before it was called:
下面的代码将root = tk.Tk()在它被调用之前固定到它的大小:
root.resizable(False, False)

