code_junkie

How would you design a very “Pythonic” UI framework?

Nov 10th, 2011
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. Shoes.app do
  2. t = para "Not clicked!"
  3. button "The Label" do
  4. alert "You clicked the button!" # when clicked, make an alert
  5. t.replace "Clicked!" # ..and replace the label's text
  6. end
  7. end
  8.  
  9. def Shoeless(Shoes.app):
  10. self.t = para("Not clicked!")
  11.  
  12. def on_click_func(self):
  13. alert("You clicked the button!")
  14. self.t.replace("clicked!")
  15.  
  16. b = button("The label", click=self.on_click_func)
  17.  
  18. class BaseControl:
  19. def __init__(self):
  20. self.func = None
  21.  
  22. def clicked(self, func):
  23. self.func = func
  24.  
  25. def __call__(self):
  26. if self.func is not None:
  27. self.func()
  28.  
  29. class Button(BaseControl):
  30. pass
  31.  
  32. class Label(BaseControl):
  33. pass
  34.  
  35. # The actual applications code (that the end-user would write)
  36. class MyApp:
  37. ok = Button()
  38. la = Label()
  39.  
  40. @ok.clicked
  41. def clickeryHappened():
  42. print "OK Clicked!"
  43.  
  44. if __name__ == '__main__':
  45. a = MyApp()
  46. a.ok() # trigger the clicked action
  47.  
  48. ## All you need is this class:
  49.  
  50. class MainWindow(Window):
  51. my_button = Button('Click Me')
  52. my_paragraph = Text('This is the text you wish to place')
  53. my_alert = AlertBox('What what what!!!')
  54.  
  55. @my_button.clicked
  56. def my_button_clicked(self, button, event):
  57. self.my_paragraph.text.append('And now you clicked on it, the button that is.')
  58.  
  59. @my_paragraph.text.changed
  60. def my_paragraph_text_changed(self, text, event):
  61. self.button.text = 'No more clicks!'
  62.  
  63. @my_button.text.changed
  64. def my_button_text_changed(self, text, event):
  65. self.my_alert.show()
  66.  
  67.  
  68. ## The Style class is automatically gnerated by the framework
  69. ## but you can override it by defining it in the class:
  70. ##
  71. ## class MainWindow(Window):
  72. ## class Style:
  73. ## my_blah = {'style-info': 'value'}
  74. ##
  75. ## or like you see below:
  76.  
  77. class Style:
  78. my_button = {
  79. 'background-color': '#ccc',
  80. 'font-size': '14px'}
  81. my_paragraph = {
  82. 'background-color': '#fff',
  83. 'color': '#000',
  84. 'font-size': '14px',
  85. 'border': '1px solid black',
  86. 'border-radius': '3px'}
  87.  
  88. MainWindow.Style = Style
  89.  
  90. ## The layout class is automatically generated
  91. ## by the framework but you can override it by defining it
  92. ## in the class, same as the Style class above, or by
  93. ## defining it like this:
  94.  
  95. class MainLayout(Layout):
  96. def __init__(self, style):
  97. # It takes the custom or automatically generated style class upon instantiation
  98. style.window.pack(HBox().pack(style.my_paragraph, style.my_button))
  99.  
  100. MainWindow.Layout = MainLayout
  101.  
  102. if __name__ == '__main__':
  103. run(App(main=MainWindow))
  104.  
  105. with Shoes():
  106. t = Para("Not clicked!")
  107. with Button("The Label"):
  108. Alert("You clicked the button!")
  109. t.replace("Clicked!")
  110.  
  111. context = None
  112.  
  113. class Nestable(object):
  114. def __init__(self,caption=None):
  115. self.caption = caption
  116. self.things = []
  117.  
  118. global context
  119. if context:
  120. context.add(self)
  121.  
  122. def __enter__(self):
  123. global context
  124. self.parent = context
  125. context = self
  126.  
  127. def __exit__(self, type, value, traceback):
  128. global context
  129. context = self.parent
  130.  
  131. def add(self,thing):
  132. self.things.append(thing)
  133. print "Adding a %s to %s" % (thing,self)
  134.  
  135. def __str__(self):
  136. return "%s(%s)" % (self.__class__.__name__, self.caption)
  137.  
  138.  
  139. class Shoes(Nestable):
  140. pass
  141.  
  142. class Button(Nestable):
  143. pass
  144.  
  145. class Alert(Nestable):
  146. pass
  147.  
  148. class Para(Nestable):
  149. def replace(self,caption):
  150. Command(self,"replace",caption)
  151.  
  152. class Command(Nestable):
  153. def __init__(self, target, command, caption):
  154. self.command = command
  155. self.target = target
  156. Nestable.__init__(self,caption)
  157.  
  158. def __str__(self):
  159. return "Command(%s text of %s with "%s")" % (self.command, self.target, self.caption)
  160.  
  161. def execute(self):
  162. self.target.caption = self.caption
  163.  
  164. class w(Wndw):
  165. title='Hello World'
  166. class txt(Txt): # either a new class
  167. text='Insert name here'
  168. lbl=Lbl(text='Hello') # or an instance
  169. class greet(Bbt):
  170. text='Greet'
  171. def click(self): #on_click method
  172. self.frame.lbl.text='Hello %s.'%self.frame.txt.text
  173.  
  174. app=w()
  175.  
  176. class MyWindow(GladeWrapper):
  177. GladeWrapper.__init__(self, "my_glade_file.xml", "mainWindow")
  178. self.GtkWindow.show()
  179.  
  180. def button_click_event (self, *args):
  181. self.button1.set_label("CLICKED")
  182.  
  183. from Boots import App, Para, Button, alert
  184.  
  185. def Shoeless(App):
  186. t = Para(text = 'Not Clicked')
  187. b = Button(label = 'The label')
  188.  
  189. def on_b_clicked(self):
  190. alert('You clicked the button!')
  191. self.t.text = 'Clicked!'
  192.  
  193. class MyWindow(Window):
  194. class VBox:
  195. entry = Entry()
  196. bigtext = TextView()
  197.  
  198. def on_entry_accepted(text):
  199. bigtext.value = eval(text).__doc__
  200.  
  201. class MyWindow(Window):
  202. contents = (
  203. para('Hello World!'),
  204. button('Click Me', id='ok'),
  205. para('Epilog'),
  206. )
  207.  
  208. def __init__(self):
  209. self['#ok'].click(self.message)
  210. self['para'].hover(self.blend_in, self.blend_out)
  211.  
  212. def message(self):
  213. print 'You clicked!'
  214.  
  215. def blend_in(self, object):
  216. object.background = '#333333'
  217.  
  218. def blend_out(self, object):
  219. object.background = 'WindowBackground'
  220.  
  221. MAIN_WINDOW.my_layout.my_edit.text
Add Comment
Please, Sign In to add comment