Open capsiamese opened 6 months ago
TkReference
Tkinter8.5Tutor TkDocs pyguis pythonTutorial TkTutor RealPythonTk
Frame.winfo_children() 获取所有子组件 widget.configure(kw='') 设置参数 widget.configure() 获取所有参数 widget.configure('') 查询某个参数
widget.winfo_xxx() 获取组件的信息
frame = ttk.Frame(parent, style='Danger.TFrame') 通常所有的风格化组件都有style关键字参数来设置风格 f['padding'] = 5 # All 5 f['padding'] = (5,10) # LR=5 TB=10 f['padding'] = (5,10,20,1) # Left,Top,Right,Bottom
f['borderwidth'] = 2 f['relief'] = 'flat','raised','sunken','solid','ridge','groove' 边界的质感
label = ttk.Label(parent, text='') 用来展示文本或者图片 label['textvariable'] = StringVar() 可以动态改变文本 img = PhotoImage(file='') label['image'] = img 展示图片 label['font'] = 'TkDefaultFount'
label可以同时展示文字和图片, 使用compoundconfiguration选项
compound
button = ttk.Button(parent, text='', command=func) button.state(['disabled']) set disable button.state(['!disabled']) clear disable button.instate(['disabled']) true if disabled button.instate(['!disabled']) true if not disabled button.instate(['disabled'], cmd) execute cmd if disabled stateFlags = ['active',disabled,focus,pressed,selected,background,readonly,alternate,invalid]
check = ttk.Checkbutton(parent, text='', command=cmd,variable=var,onvalue='',offvalue='')
a = ttk.Radiobutton(parent, text='',variable=var, value='') b = ttk.Radiobutton(parent, text='',variable=var, value='')
entry = ttk.Entry(parent,textvariable,show="*",validatecommand) entry.insert(index,'') entry.delete(index,'') entry.trace_add('',cmd) 订阅内容改变事件
cbb = ttk.Combobox(parent, textvariable) cbb['values'] = ('','','')
Listbox(parent, height, listvariable)
ttk.Scrollbar(parent,orient=VERTICAL,command=listbox.yview) listbox.configure(yscrollcommand=cmd)
Text(parent, width, height)
ttk.Scale(parent,orient=HORIZONTAL,length,from_,to)
ttk.Spinbox(parent,from_,to,textvariable)
ttk.Progressbar(parent,orient=HORIZONTAL,length,mode='determinate')
ttk.Separator ttk.Labelframe ttk.PanedWindow ttk.Notebook ttk.Treeview
widget.grid(column,row, columnspan, rowspan, sticky, padx, pady) 网格布局中, 每个组件都被赋予了一个行号和列号, span表示占几列/行
widget.columnconfigure(index,weight) widget.rowconfigure(index,weight)
grid_configure(padx, pady) # 设置组件的内边距
widget.bind('', command)绑定事件 root.event_generate('<>') 虚拟事件
变量
添加菜单栏前需要调用 root.option_add('',FALSE) win = Toplevel(root) menubar = Menu(win) menubar.add_command(label, command) menubar.add_cascade(menu=,label=) menubar.add_separator() win['menu'] = menubar
from tkinter import filedialog
from tkinter import messagebox
from tkinter import colorchooser colorchooser.askcolor()
from tkinter import colorchooser
canvas = Canvas(parent,width,height,background)
introduction
TkReference
Tkinter8.5Tutor TkDocs pyguis pythonTutorial TkTutor RealPythonTk
Concept
Widgets
Frame.winfo_children() 获取所有子组件 widget.configure(kw='') 设置参数 widget.configure() 获取所有参数 widget.configure('') 查询某个参数
widget.winfo_xxx() 获取组件的信息
Frame
frame = ttk.Frame(parent, style='Danger.TFrame') 通常所有的风格化组件都有style关键字参数来设置风格 f['padding'] = 5 # All 5 f['padding'] = (5,10) # LR=5 TB=10 f['padding'] = (5,10,20,1) # Left,Top,Right,Bottom
f['borderwidth'] = 2 f['relief'] = 'flat','raised','sunken','solid','ridge','groove' 边界的质感
Label
label = ttk.Label(parent, text='') 用来展示文本或者图片 label['textvariable'] = StringVar() 可以动态改变文本 img = PhotoImage(file='') label['image'] = img 展示图片 label['font'] = 'TkDefaultFount'
label可以同时展示文字和图片, 使用
compound
configuration选项Button
button = ttk.Button(parent, text='', command=func) button.state(['disabled']) set disable button.state(['!disabled']) clear disable button.instate(['disabled']) true if disabled button.instate(['!disabled']) true if not disabled button.instate(['disabled'], cmd) execute cmd if disabled stateFlags = ['active',disabled,focus,pressed,selected,background,readonly,alternate,invalid]
Checkbutton
check = ttk.Checkbutton(parent, text='', command=cmd,variable=var,onvalue='',offvalue='')
Radiobutton
a = ttk.Radiobutton(parent, text='',variable=var, value='') b = ttk.Radiobutton(parent, text='',variable=var, value='')
Entry
entry = ttk.Entry(parent,textvariable,show="*",validatecommand) entry.insert(index,'') entry.delete(index,'') entry.trace_add('',cmd) 订阅内容改变事件
Combobox
cbb = ttk.Combobox(parent, textvariable) cbb['values'] = ('','','')
Listbox
Listbox(parent, height, listvariable)
Scrollbar
ttk.Scrollbar(parent,orient=VERTICAL,command=listbox.yview) listbox.configure(yscrollcommand=cmd)
Text
Text(parent, width, height)
Scale
ttk.Scale(parent,orient=HORIZONTAL,length,from_,to)
Spinbox
ttk.Spinbox(parent,from_,to,textvariable)
Progressbar
ttk.Progressbar(parent,orient=HORIZONTAL,length,mode='determinate')
Misc
ttk.Separator ttk.Labelframe ttk.PanedWindow ttk.Notebook ttk.Treeview
GeometryManagement
Grid
widget.grid(column,row, columnspan, rowspan, sticky, padx, pady) 网格布局中, 每个组件都被赋予了一个行号和列号, span表示占几列/行
widget.columnconfigure(index,weight) widget.rowconfigure(index,weight)
grid_configure(padx, pady) # 设置组件的内边距
EventHandling
widget.bind('', command)绑定事件 root.event_generate('<>') 虚拟事件
变量
Menubar
添加菜单栏前需要调用 root.option_add('',FALSE) win = Toplevel(root) menubar = Menu(win) menubar.add_command(label, command) menubar.add_cascade(menu=,label=) menubar.add_separator() win['menu'] = menubar
Dialog
from tkinter import filedialog
from tkinter import messagebox
ColorChooser
from tkinter import colorchooser
colorchooser.askcolor()Canvas
canvas = Canvas(parent,width,height,background)