There are 3 geometry managers that you have available to you -- grid
, pack
and place
. The third is the most general, but also very difficult to use. I prefer grid
. Note that you can place widgets inside of other widgets -- Or you can specify columnspan
. So, if you want to get the following layout:
-------------------------
| Button1 | Button2 |
-------------------------
| Big Widget |
-------------------------
there are 2 canonical ways to do it using .grid
. The first method is columnspan
:
import Tkinter as Tk
root = Tk.Tk()
b1 = Tk.Button(root,text="Button1")
b1.grid(row=0,column=0)
b2 = Tk.Button(root,text="Button2")
b2.grid(row=0,column=1)
big_widget = Tk.Canvas(root)
big_widget.grid(row=1,column=0,columnspan=2)
*note that there is a completely analogous rowspan
option as well.
The second method is to use a Frame
to hold your buttons:
import Tkinter as Tk
root = Tk.Tk()
f = Tk.Frame(root)
f.grid(row=0,column=0)
#place buttons on the *frame*
b1 = Tk.Button(f,text="Button1")
b1.grid(row=0,column=0)
b2 = Tk.Button(f,text="Button2")
b2.grid(row=0,column=1)
big_widget = Tk.Canvas(root)
big_widget.grid(row=1,column=0) #don't need columnspan any more.
This method is SUPER useful for creating complex layouts -- I don't know how you could create a complex layout without using Frame
objects like this ...
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…