2014-02-22 216 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)