2014-02-22 215 views
44

我有一个程序,它根据复选框创建一个显示消息的窗口。如何防止窗口被tkinter调整大小?

当消息显示并且消息不显示时,如何使窗口大小保持不变?

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

回答

87

此代码使用户无法更改Tk()窗口尺寸的条件窗口,并禁用最大化按钮。

import tkinter as tk 

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

在此系统中,你可以Carpetsmoker的答案,或者通过做这个改变与@窗口尺寸:

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

应该很容易为你实现一个到你的代码。 :)

+0

但是,当点击它的位置在 – user2332665

28

您可以使用minsizemaxsize设定最低&最大尺寸,例如:

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

会给你的窗口固定宽度& 666个像素高度。

或者,只是用minsize

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

将确保你的窗口始终是至少 666像素超大,但用户仍然可以展开窗口。

4

你可以使用:

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

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

0

这已经是提供上述现有的解决方案的一个变体:

import tkinter as tk 

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

的优点是,在键入以下。

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' 

是你会得到第一个答案。 TK不支持的最小和最大尺寸

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

我理解了它,但只是想第一个。与tk一起使用python3。

+1

你正在尝试使用的第一个答案用错字的窗口中更改的复选框。 'root.resizeable(...)'给你上面的错误,尝试'root.resizable(...)'。 – Nae

0

下面的代码将解决root = tk.Tk()其规模之前,它被称为:

root.resizable(False, False)