dbr*_*dbr 12 python user-interface frameworks
我一直在玩Ruby库"鞋子".基本上,您可以通过以下方式编写GUI应用程序:
Shoes.app do
t = para "Not clicked!"
button "The Label" do
alert "You clicked the button!" # when clicked, make an alert
t.replace "Clicked!" # ..and replace the label's text
end
end
Run Code Online (Sandbox Code Playgroud)
这让我想到 - 我如何在Python中设计一个同样易于使用的GUI框架?一个没有通常的基本上是C*库包装的东西(在GTK,Tk,wx,QT等的情况下)
鞋子从web开发(如#f0c2f0
样式颜色表示法,CSS布局技术等:margin => 10
)和ruby(以明智的方式广泛使用块)中获取东西
Python缺乏"rubyish块"使得(隐喻)直接端口变得不可能:
def Shoeless(Shoes.app):
self.t = para("Not clicked!")
def on_click_func(self):
alert("You clicked the button!")
self.t.replace("clicked!")
b = button("The label", click=self.on_click_func)
Run Code Online (Sandbox Code Playgroud)
没有那么干净,并且几乎不会那么灵活,我甚至不确定它是否可以实现.
使用装饰器似乎是一种将代码块映射到特定操作的有趣方法:
class BaseControl:
def __init__(self):
self.func = None
def clicked(self, func):
self.func = func
def __call__(self):
if self.func is not None:
self.func()
class Button(BaseControl):
pass
class Label(BaseControl):
pass
# The actual applications code (that the end-user would write)
class MyApp:
ok = Button()
la = Label()
@ok.clicked
def clickeryHappened():
print "OK Clicked!"
if __name__ == '__main__':
a = MyApp()
a.ok() # trigger the clicked action
Run Code Online (Sandbox Code Playgroud)
基本上,装饰器函数存储函数,然后当动作发生时(例如,单击),将执行适当的函数.
各种东西的范围(比如la
上面例子中的标签)可能相当复杂,但它似乎可以以相当简洁的方式进行.