---
title: "python grid布局"
author: "yexin218"
date: "2010-04-24T17:54:47Z"
tags: ["C", "C#", "C++", "J#", "Python"]
categories: ["编程语言"]
slug: "python-grid"
draft: false
lastmod: "2010-04-24T17:54:47Z"
id: 652189
url: "https://yexin218.iteye.com/blog/652189"
---

转载自 [tigerjgh](http://hi.baidu.com/tigerjgh)

最终编辑 [codedeveloper](http://hi.baidu.com/codedeveloper)

[http://www.java2s.com/Code/Python/CatalogPython.htm](http://www.java2s.com/Code/Python/CatalogPython.htm)

[http://www.java2s.com/Code/Python/GUI-Tk/Layoutcomponentsingrid.htm](http://www.java2s.com/Code/Python/GUI-Tk/Layoutcomponentsingrid.htm)

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
[http://www.java2s.com/Code/Python/GUI-Tk/GridmadebyLabelandEntry.htm](http://www.java2s.com/Code/Python/GUI-Tk/GridmadebyLabelandEntry.htm)  
**from** Tkinter **import** *  
colors = \[ 're d ', 'gr een ', 'or ange ', 'wh ite ', 'ye llow ', 'bl ue ']

r = 0  
**for** c in colors:  
     Label ( text=c, relief=RIDGE,      width= 25 ) .grid ( row=r, column= 0 )  
     Entry ( bg=c,       relief=SUNKEN, width= 50 ) .grid ( row=r, column= 1 )  
     r = r+ 1

mainloop ()

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**

[http://www.java2s.com/Code/Python/GUI-Tk/GridmadebytheLabelwithRaisedborder.htm](http://www.java2s.com/Code/Python/GUI-Tk/GridmadebytheLabelwithRaisedborder.htm)  
\# simple 2d table

**from** Tkinter **import** *

**for** i in range ( 5 ) :  
     **for** j in range ( 4 ) :  
         l = Label ( text= '%d .%d ' % ( i, j ) , relief=RIDGE )  
         l.grid ( row=i, column=j, sticky=NSEW )

mainloop ()

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**

[http://www.java2s.com/Code/Python/GUI-Tk/2dtableofinputfields.htm](http://www.java2s.com/Code/Python/GUI-Tk/2dtableofinputfields.htm)  
**from** Tkinter **import** *

rows = \[]  
**for** i in range ( 5 ) :  
     cols = \[]  
     **for** j in range ( 4 ) :  
         e = Entry ( relief=RIDGE )  
         e.grid ( row=i, column=j, sticky=NSEW )  
         e. **insert** ( END, '%d .%d ' % ( i, j ))  
         cols.append ( e )  
     rows.append ( cols )

def onPress () :  
     **for** row in rows:  
         **for** col in row:  
             print col.get () ,  
         print

Button ( text= 'Fe tch ', command=onPress ) .grid ()  
mainloop ()

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**

[http://www.java2s.com/Code/Python/GUI-Tk/Gridlayoutmanagerdemonstration.htm](http://www.java2s.com/Code/Python/GUI-Tk/Gridlayoutmanagerdemonstration.htm)  
**from** Tkinter **import** *

**class** GridDemo ( Frame ) :  
    def \_\_init\__ ( self ) :  
       Frame.\_\_init\__ ( self )  
       self.master.title ( "Grid Demo" )

       self.master.rowconfigure ( 0 , weight = 1 )  
       self.master.columnconfigure ( 0 , weight = 1 )  
       self.grid ( sticky = W+E+N+S )  
    
       self.text1 = Text ( self, width = 15 , height = 5 )

       self.text1.grid ( rowspan = 3 , sticky = W+E+N+S )  
       self.text1. **insert** ( INSERT, "Text1" )

       self.button1 = Button ( self, text = "Button 1" , width = 25 )  
       self.button1.grid ( row = 0 , column = 1 , columnspan = 2 , sticky = W+E+N+S )

       self.button2 = Button ( self, text = "Button 2" )  
       self.button2.grid ( row = 1 , column = 1 , sticky = W+E+N+S )

       self.button3 = Button ( self, text = "Button 3" )  
       self.button3.grid ( row = 1 , column = 2 , sticky = W+E+N+S )

       self.button4 = Button ( self, text = "Button 4" )  
       self.button4.grid ( row = 2 , column = 1 , columnspan = 2 , sticky = W+E+N+S )

       self.entry = Entry ( self )  
       self.entry.grid ( row = 3 , columnspan = 2 , sticky = W+E+N+S )  
       self.entry. **insert** ( INSERT, "Entry" )

       self.text2 = Text ( self, width = 2 , height = 2 )  
       self.text2.grid ( row = 3 , column = 2 , sticky = W+E+N+S )  
       self.text2. **insert** ( INSERT, "Text2" )

       self.rowconfigure ( 1 , weight = 1 )  
       self.columnconfigure ( 1 , weight = 1 )

def main () :  
    GridDemo () .mainloop ()   

**if** \_\_name\__ == "\_\_main\_\_" :  
    main ()