2015-04-30 86 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() 
相关问题