Tkinter上的包装器用于构建更多pythonic ui
uielem的Python项目详细描述
uielem-wrapper over tkinter用于构建更多pythonic ui
有时你只想制作一个快速的用户界面,让一些可视化的任务变得简单
Tkinter
是一个快速的ui工具包,永远都是用python打包的。uielem
希望为使用Tkinter
提供更现代的/pythonic语法。
一个简单的例子:
之前
from Tkinter import Tk, Frame, Label, Listbox, Button, mainloop
tkroot = Tk()
tkroot.title('Kanban')
frame = Frame(tkroot)
frame.pack()
board_frame = Frame(frame)
board_frame.pack(side='top')
for board_name in ["Todo", "Doing", "Done"]:
inner_frame = Frame(board_frame)
inner_frame.pack(side='left')
label = Label(inner_frame, text=board_name)
label.pack(side='top')
listbox = Listbox(inner_frame)
listbox.pack(side='top')
buttons_frame = Frame(frame)
buttons_frame.pack(side='top')
add_button = Button(buttons_frame, text='Add item', command=add)
add_button.pack(side='left')
remove_button = Button(buttons_frame, text='Remove item', command=remove)
remove_button.pack(side='left')
mainloop()
之后
from uielem import UI, uidict
from Tkinter import Tk, Frame, Label, Listbox, Button
uiroot = UI(Tk, name='root', title='Kanban', children=[
UI(Frame, packside='top', children=[
UI(Frame, packside='left', name='boards', children=[
UI(Frame, packside='top', children=[
UI(Label, text=board_name),
UI(Listbox, name=board_name.lower())])
for board_name in ["Todo", "Doing", "Done"]]),
UI(Frame, packside='left', children=[
UI(Button, text='Add item', command=add),
UI(Button, text='Remove item', command=remove), ])])])
uiroot.makeelem()
uidict["root"].mainloop()
安装
pip install -r requirements.txt
uielem
依赖于undoable。
用法和功能
基本模式就是UI(<tkinter elem>, <keyword arguments>, children=[<chidren>])
。
关键字参数
packside=
设置所有children的打包端(包含在元素中)。defaulttext=
对于Tkinter.Entry
设置初始文本。on_*=
设置事件回调(并且_
替换为-
)。因此,传递on_Button_3=func
与创建后运行elem.bind('<Button-3>', func)
是一样的。set_*=
设置创建后的属性值。例如set_title=['title']
与title='title'
具有相同的效果。- 其他非特殊关键字参数传递给tkinter元素。
其他功能
uidict
包含所有命名(name=something
)元素,以便于参考。名称必须是全局唯一的(想想svg中的id
)。- 要从容器中添加和删除子元素,请将其视为列表(使用
.append
、.insert
和.remove
)。
可以通过索引来访问包装器的子类(例如:< })。 - 包装器可以使用
.elem
属性访问tkinter对象,tkinter对象可以使用.ui
访问包装器。
替代版本
uielem
现在相当大,因此summaries
文件夹中提供了uielem
的两个较轻版本,但它们可能并不总是最新的。
simple_uielem.py
删除了一些不太常用的特性,比如代码生成。minimal_uielem.py
只对生成一次的ui起作用,之后再也没有修改过ui(如示例中所示),但在其他方面完全兼容。同时删除对可撤消的依赖。
它们还用作uielem
的体系结构/内部工作的文档。