几何管理
Tkinter控件有特定的几何状态管理方法,管理整个控件区域组织,以下是Tkinter公开的几何管理类:包、网格、位置。
from Tkinter import*
#初始化Tk()
myWindow = Tk()
#设置标题
myWindow.title('Python GUI Learning')
#设置窗口大小
width = 380
height = 300
#获取屏幕尺寸以计算布局参数,使窗口居屏幕中央
screenwidth = myWindow.winfo_screenwidth()
screenheight = myWindow.winfo_screenheight()
alignstr = '%dx%d+%d+%d' % (width, height, (screenwidth-width)/2, (screenheight-height)/2)
myWindow.geometry(alignstr)
#设置窗口是否可变长、宽,True:可变,False:不可变
myWindow.resizable(width=False, height=True)
#框架布局
frame_root = Frame(myWindow)
frame_l = Frame(frame_root)
frame_r = Frame(frame_root)
#创建一个标签,并在窗口上显示
Label(frame_l, text="中国", bg="green", font=("Arial", 12), width=10, height=2).pack(side=TOP)
Label(frame_l, text="日本", bg="green", font=("Arial", 12), width=10, height=2).pack(side=TOP)
Label(frame_r, text="美国", bg="green", font=("Arial", 12), width=10, height=2).pack(side=TOP)
Label(frame_r, text="韩国", bg="green", font=("Arial", 12), width=10, height=2).pack(side=TOP)
#框架的位置布局
frame_l.pack(side=LEFT)
frame_r.pack(side=RIGHT)
frame_root.pack()
#进入消息循环
myWindow.mainloop()
from Tkinter import*
def printInfo():
#清理entry2
entry2.delete(0, END)
#根据输入半径计算面积
R=int(entry1.get())
S= 3.1415926*R*R
entry2.insert(10, S)
#清空entry2控件
entry1.delete(0, END)
#初始化Tk()
myWindow = Tk()
#设置标题
myWindow.title('Python GUI Learning')
#标签控件布局
Label(myWindow, text="input").grid(row=0)
Label(myWindow, text="output").grid(row=1)
#Entry控件布局
entry1=Entry(myWindow)
entry2=Entry(myWindow)
entry1.grid(row=0, column=1)
entry2.grid(row=1, column=1)
#Quit按钮退出;Run按钮打印计算结果
Button(myWindow, text='Quit', command=myWindow.quit).grid(row=2, column=0, sticky=W, padx=5,pady=5)
Button(myWindow, text='Run', command=printInfo).grid(row=2, column=1, sticky=W, padx=5, pady=5)
#row=2, column=1, sticky=W, padx=5, pady=5表示第2行第1列
#进入消息循环
myWindow.mainloop()