Tk/Tcl集成在Python中,包括3个模块tkinter,tkinter.tix,tkinter.ttk。
最简形式的一个窗口程序:
import tkinter as tk
class Application(tk.Frame):
def __init(self,master=None):
super.__init(master)
self.pack()
self.create_widgets()
def create_widgets(self):
pass
root=tk.Tk()
app=Application(master=root)
app.mainloop()
widget的使用:
1.设置属性
创建一个按钮
fred=tk.Button(self,fg='red',bg='blue')
或者
fred["fg"]="red"
fred["bg"]="blue"
2.布局管理
pack()方法,参数为kv pair :fill=[x|y|both|none] ipadx ipady 内部空间 padx,pady 外部空间 side 位置方向
3.窗口管理
wm类进行窗口管理,所有部件继承自这个类,所以可以直接使用。.master来获取顶层窗口或者_root()方法。
import tkinter as tk
class App(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.pack()
# create the application
myapp = App()
#
# here are method calls to the window manager class
#
myapp.master.title("My Do-Nothing Application")
myapp.master.maxsize(1000, 400)
# start the program
myapp.mainloop()
4.参数类型
anchor,bitmap,boolean,color,cursor,distance,font,relief等等,定义部件的各项属性和参数
5.绑定事件
bind(self,sequence,func,add='') 参数是,目标事件,触发函数,绑定方式
func的定义
def func(self,event):
event.widget["relief"]="raised"
其中event代表传入的事件对象,属性有time,keycode,width,x,y,type等等