tkpf是在受WPF(Windows展示基金会)和Angular影响的范式中构建Tkinter Guis的库。
主要功能是:



您以XML或YAML格式指定GUI。这是一个简单的示例, ExampleWindow.xml :
< Frame pack-anchor = " nw " pack-padx = " 5 " >
< LabelFrame text = " Options " pack-anchor = " w " pack-fill = " x " >
< Radiobutton pack-anchor = " w " variable = " [(choice)] " value = " option1 " >
Option 1
</ Radiobutton >
< Radiobutton pack-anchor = " w " variable = " [(choice)] " value = " option2 " >
Option 2
</ Radiobutton >
< Combobox pack-anchor = " w " textvariable = " (selected_suboption) " values = " [available_suboptions] "
name = " combobox " />
< Button command = " do_stuff " >Do stuff</ Button >
</ LabelFrame >
</ Frame >如您所见,XML标签名称对应于tkinter widget类名称,而XML属于其参数。 tkpf被认为是因为它始终使用更好的ttk主题小部件。
诸如pack-anchor="nw"或grid-row="0"之类的选项指定布局,并将传递到适当的tkinter布局管理器方法,在这种情况下.pack(anchor='nw') 。
关于如何以YAML格式指定GUI,请参见example/ExampleWindow.yaml 。
您通过创建从Window派生并显示的类来显示GUI。您必须在构造函数中提供ViewModel。
class ExampleWindow ( Window ):
template_path = 'ExampleWindow.xml' # 'ExampleWindow.yaml' works too
ExampleWindow ( ExampleModel ()). show ()如果要将布局XML保存在此文件中,也可以执行此操作:
class ExampleWindow ( Window ):
template = '<Label>Some text</Label>'或者
class ExampleWindow ( Window ):
template_yaml = '''
Label:
text: Some text
'''设置窗口标题:
def __init__ ( self , model ):
super (). __init__ ( model )
self . title = 'My application'在视图类中,您可以编写事件处理程序。例如,该按钮起作用:
def do_stuff ( self ):
self . combobox . config ( state = 'disabled' )这还显示了如何在视图类方法中通过名称访问小部件。但是,如果您愿意,您可以像这样动态访问它们:
self . named_widgets [ 'combobox' ] class ExampleModel ( ViewModel ):
choice = Bindable ( AutoProperty ( 1 ))
available_suboptions = Bindable ( AutoProperty ())
selected_suboption = Bindable ( AutoProperty ())
def __init__ ( self ):
super (). __init__ ()
self . available_suboptions = ( 'suboption1' , 'suboption2' ) AutoProperty类似于C#自动化性质。默认情况下,其数据类型为str 。您可以向其构造函数提供默认值或类型。
Bindable是一种装饰器,您可以在任何属性上使用它来返回可绑定的属性。它必须知道包装属性的数据类型,因此请用类型注释指定其返回类型:
@ Bindable
@ property
def foo () -> int :
return 1 AutoProperty为您处理这一点。
仅支持tkinter绑定,仅支持int , bool , float和str类型,尽管对于ComboBox值,您可以分配一个python元组。
如果在视图类中找不到活动处理程序,它也会在ViewModel上查找。
在XML中,您可以用类似于Angular的语法指定数据绑定的方向:
values="[available_suboptions]"
是从数据源到查看目标的单向绑定,
textvariable="(selected_suboption)"
是从视图目标到数据源的单向绑定,以及
variable="[(choice)]"
是双向绑定。
您可以使用从TKINTER小部件类派生的自定义小部件。您唯一要做的就是打电话
Directive . Registry . register ( YourCustomWidgetClass )在加载使用它的模板之前。
tkpf支持将您的GUI分解为组件。这是具有自己的ViewModel的进度键组件的示例:
class ProgressbarModel ( ViewModel ):
value = BindableProperty ( 0 )
target = BindableProperty ( 100 )
class CustomProgressbar ( Component ):
template = '<Progressbar name="progressbar" variable="[value]" maximum="[target]"/>'您可以这样使用:
< CustomProgressbar tkpf-model = " [progressbar_model] " />其中progressbar_model是您的主视图模型上的属性或属性。
在Python 3.5上,您必须在使用该组件之前注册。在Python 3.6+上是自动的。
Directive . Registry . register ( CustomProgressbar )计划您将能够添加自定义,可绑定的属性到组件,这样的组件:
class ExampleComponent ( Component ):
template = '<Label name="thelabel">Example component text</Label>'
def config ( self , ** kwargs ):
self . thelabel . config ( text = kwargs [ 'custom-text' ])然后以这样的方式使用它们:
< ExampleComponent custom-text = " Custom text " />唯一的要求是属性名称包含连字符。
tkpf仅支持Python 3.5+。
这是一项正在进行的工作。也是我第一次创建图书馆的尝试。查看项目问题,以查看不支持的问题。