利用TKinter模块创建GUI窗口
# -*- coding: utf-8 -*- from Tkinter import * root = Tk() # 80x80代表了初始化时主窗口的大小,0,0代表了初始化时窗口所在的位置 root.geometry( '80x80+10+10' ) # 填充方向 Label(root, text = 'l1' , bg = 'red' ).pack(fill = Y) Label(root, text = 'l2' , bg = 'green' ).pack(fill = BOTH) Label(root, text = 'l3' , bg = 'blue' ).pack(fill = X) # 左右布局 Label(root, text = 'l1' , bg = 'red' ).pack(fill = Y, side = LEFT) Label(root, text = 'l2' , bg = 'green' ).pack(fill = BOTH, side = RIGHT) Label(root, text = 'l3' , bg = 'blue' ).pack(fill = X, side = LEFT) # 绝对布局 l4 = Label(root, text = 'l4' ) l4.place(x = 3 , y = 3 , anchor = NW) # Grid 网格布局 l1 = Label(root, text = 'l1' , bg = 'red' ) l2 = Label(root, text = 'l2' , bg = 'blue' ) l3 = Label(root, text = 'l3' , bg = 'green' ) l4 = Label(root, text = 'l4' , bg = 'yellow' ) l5 = Label(root, text = 'l5' , bg = 'purple' ) l1.grid(row = 0 , column = 0 ) l2.grid(row = 1 , column = 0 ) l3.grid(row = 1 , column = 1 ) l4.grid(row = 2 ) l5.grid(row = 0 , column = 3 ) root.mainloop() |
-----
基本控件Label、Entry、Button、Checkbutton的使用:
1、Label
示例:
label = Label(root, fg="blue", bg="yellow", width=10, height=3, text="Test", font=('Tempus Sans ITC', 20)).pack()
第一个参数为该控件属于哪个容器,fg:前景色;bg:背景色;width:宽度;height:高度;text:文本内容,font:字体
2、Entry
Entry的text属性不能设置Entry的文本
在Entry中设定初始值,使用textvariable将变量与Entry绑定
示例:
e = StringVar()
entry = Entry(root, textvariable=e, state="readonly", show="*")
e.set("test")
entry.pack()
state:设置状态(eg:normal,readonly);show:用特定符号来代替输入内容(eg: * [密码输入框])
3、Button
示例:
btn = Button(root, relief=SOLID, bd=2, command=XXXX)
btn.pack()
relief:按钮效果([FLAT, GROOVE, RAISED, RIDGE, SOLID, SUNKEN])
bd: 边框
command:回调函数
4、Checkbutton
示例:
v = IntVar()
cbtn = tk.Checkbutton(root, text="test", variable=v, font=('Tempus Sans ITC', 10))
v.set(1)
cbtn.pack()