2015-04-30 90 views
2

如何在'root'大小調整時'label'和'text'小部件填充所有空間?在調整大小時使用小部件填充空間

如果可能,我想使用'網格'方法。

from tkinter import * 
root = Tk() 

root.resizable(width = True, height = True) 
label = Label(root, text = "Text") 
label.grid() 

text = Text(root) 
text.grid() 
root.mainloop() 

當我嘗試在課堂上使用時,它不起作用。

Application.py

from tkinter import * 
import menu 
import workPlace 

class Application(Frame): 
    def __init__(self, boss = None): 
     Frame.__init__(self) 

     self.master.title("Title") 

     self.master.grid_columnconfigure(0, weight = 1) 
     self.master.grid_rowconfigure(1, weight = 1) 
     self.master.resizable(width = True, height = True) 

     self.menu = menu.MenuBar(self) 
     self.menu.grid(sticky = W) 

     self.workPlace = workPlace.WorkPlace(self) 
     self.workPlace.grid(sticky = "NEWS") 

if __name__ == "__main__": 
    Application().mainloop() 

workPlace.py

from tkinter import * 
import tkinter.scrolledtext as scr 

class WorkPlace(Frame): 
    def __init__(self, boss = None): 
     Frame.__init__(self) 

     self.scrText = scr.ScrolledText(self) 
     self.scrText.grid() 

     self.label = Label(self,text = "Label") 
     self.label.grid() 

menu.py需要

from tkinter import * 

class MenuBar(Frame): 
    def __init__(self, boss = None): 
     Frame.__init__(self) 

     fileMenu = Menubutton(self, text = 'File') 
     fileMenu.grid(row = 0, column = 0) 
     me1 = Menu(fileMenu) 

     fileMenu.configure(menu = me1) 

     findMenu = Menubutton(self, text = 'Find') 
     findMenu.grid(row = 0, column = 1) 
     me1 = Menu(findMenu) 
     findMenu.configure(menu = me1) 

     optionMenu = Menubutton(self, text = 'Option') 
     optionMenu.grid(row = 0, column = 2) 
     me1 = Menu(optionMenu) 

     optionMenu.configure(menu = me1) 
+0

我沒有'menu'或我的機器上'workPlace'模塊。你從哪裏弄到的? – Kevin

+0

感謝您發佈您的其他模塊。我想我看到了問題。 – Kevin

回答

4

兩個步驟。

  1. 呼叫grid_rowconfiguregrid_columnconfigure來設置每個網格的行和列的重量。重量爲零的行和列在調整大小時不會拉伸。這是默認行爲。

  2. 當調用你的部件grid,指定sticky參數使部件伸展時他們的行或列延伸。

 

from tkinter import * 
root = Tk() 

root.grid_columnconfigure(0, weight=1) 
#uncomment this line if you want the Label widget to stretch vertically. 
#or leave it as is if you only want the Text widget to stretch. 
#root.grid_rowconfigure(0, weight=1) 
root.grid_rowconfigure(1, weight=1) 

root.resizable(width = True, height = True) 
label = Label(root, text = "Text") 
label.grid(sticky="NEWS") 

text = Text(root) 
text.grid(sticky="NEWS") 
root.mainloop() 

在你的工作對象的具體情況,你需要configure根對象和工作場所的對象兩者,你需要爲工作場所的指定對象粘性及其子對象。

from tkinter import * 
import tkinter.scrolledtext as scr 

class WorkPlace(Frame): 
    def __init__(self, boss = None): 
     Frame.__init__(self) 

     self.grid_columnconfigure(0, weight=1) 
     self.grid_rowconfigure(0, weight=1) 

     self.scrText = scr.ScrolledText(self) 
     self.scrText.grid(sticky="NEWS") 

     self.label = Label(self,text = "Label") 
     self.label.grid() 
+0

請將代碼編輯爲原始文章,而不是將其粘貼到評論中。 – Kevin

2

如果你是沒事用pack代替grid,你可以做

from tkinter import * 
root = Tk() 

root.resizable(width = True, height = True) 
label = Label(root, text = "Text") 
label.pack(fill=X) 
text = Text(root) 
text.pack(fill=BOTH, expand=True) 
root.mainloop() 
相關問題