Public paste
FloatLayout subclass causes Widget instantiation TypeError
By: LogicalDash | Date: Jul 13 2015 14:07 | Format: Python | Expires: never | Size: 8.33 KB | Hits: 491

  1. from kivy.uix.floatlayout import FloatLayout
  2. from kivy.properties import BooleanProperty, StringProperty, NumericProperty, DictProperty, ObjectProperty, ListProperty, ReferenceListProperty
  3.  
  4.  
  5. class Card(FloatLayout):
  6.     """A trading card, similar to the kind you use to play games like
  7.    _Magic: the Gathering_.
  8.  
  9.    Its appearance is determined by several properties, the most
  10.    important being:
  11.  
  12.    * ``headline_text``, a string to be shown at the top of the card;
  13.      may be styled with eg. ``headline_font_name`` or
  14.      ``headline_color``
  15.  
  16.    * ``art_source``, the path to an image to be displayed below the
  17.      headline; may be hidden by setting ``show_art`` to ``False``
  18.  
  19.    * ``midline_text``, similar to ``headline_text`` but appearing
  20.      below the art
  21.  
  22.    * ``text``, shown in a box the same size as the art. Styleable
  23.      like ``headline_text`` and you can customize the box with
  24.      eg. ``foreground_color`` and ``foreground_source``
  25.  
  26.    * ``footer_text``, like ``headline_text`` but at the bottom
  27.  
  28.    :class:`Card` is particularly useful when put in a
  29.    :class:`DeckLayout`, allowing the user to drag cards in between
  30.    any number of piles, into particular positions within a particular
  31.    pile, and so forth.
  32.  
  33.    """
  34.     dragging = BooleanProperty(False)
  35.     deck = NumericProperty()
  36.     idx = NumericProperty()
  37.     ud = DictProperty({})
  38.  
  39.     collide_x = NumericProperty()
  40.     collide_y = NumericProperty()
  41.     collide_pos = ReferenceListProperty(collide_x, collide_y)
  42.  
  43.     foreground = ObjectProperty()
  44.     foreground_source = StringProperty('')
  45.     foreground_color = ListProperty([1, 1, 1, 1])
  46.     foreground_image = ObjectProperty(None, allownone=True)
  47.     foreground_texture = ObjectProperty(None, allownone=True)
  48.  
  49.     background_source = StringProperty('')
  50.     background_color = ListProperty([.7, .7, .7, 1])
  51.     background_image = ObjectProperty(None, allownone=True)
  52.     background_texture = ObjectProperty(None, allownone=True)
  53.  
  54.     outline_color = ListProperty([0, 0, 0, 1])
  55.     content_outline_color = ListProperty([0, 0, 0, 0])
  56.     foreground_outline_color = ListProperty([0, 0, 0, 1])
  57.     art_outline_color = ListProperty([0, 0, 0, 0])
  58.  
  59.     art = ObjectProperty()
  60.     art_source = StringProperty('')
  61.     art_color = ListProperty([1, 1, 1, 1])
  62.     art_image = ObjectProperty(None, allownone=True)
  63.     art_texture = ObjectProperty(None, allownone=True)
  64.     show_art = BooleanProperty(True)
  65.  
  66.     headline = ObjectProperty()
  67.     headline_text = StringProperty('Headline')
  68.     headline_markup = BooleanProperty(True)
  69.     headline_font_name = StringProperty('DroidSans')
  70.     headline_font_size = NumericProperty(18)
  71.     headline_color = ListProperty([0, 0, 0, 1])
  72.  
  73.     midline = ObjectProperty()
  74.     midline_text = StringProperty('')
  75.     midline_markup = BooleanProperty(True)
  76.     midline_font_name = StringProperty('DroidSans')
  77.     midline_font_size = NumericProperty(14)
  78.     midline_color = ListProperty([0, 0, 0, 1])
  79.  
  80.     footer = ObjectProperty()
  81.     footer_text = StringProperty('')
  82.     footer_markup = BooleanProperty(True)
  83.     footer_font_name = StringProperty('DroidSans')
  84.     footer_font_size = NumericProperty(10)
  85.     footer_color = ListProperty([0, 0, 0, 1])
  86.  
  87.     text = StringProperty('')
  88.     text_color = ListProperty([0, 0, 0, 1])
  89.     markup = BooleanProperty(True)
  90.     font_name = StringProperty('DroidSans')
  91.     font_size = NumericProperty(12)
  92.  
  93.     def on_background_source(self, *args):
  94.         """When I get a new ``background_source``, load it as an
  95.        :class:`Image` and store that in ``background_image``.
  96.  
  97.        """
  98.         if self.background_source:
  99.             self.background_image = Image(source=self.background_source)
  100.  
  101.     def on_background_image(self, *args):
  102.         """When I get a new ``background_image``, store its texture in
  103.        ``background_texture``.
  104.  
  105.        """
  106.         if self.background_image is not None:
  107.             self.background_texture = self.background_image.texture
  108.  
  109.     def on_foreground_source(self, *args):
  110.         """When I get a new ``foreground_source``, load it as an
  111.        :class:`Image` and store that in ``foreground_image``.
  112.  
  113.        """
  114.         if self.foreground_source:
  115.             self.foreground_image = Image(source=self.foreground_source)
  116.  
  117.     def on_foreground_image(self, *args):
  118.         """When I get a new ``foreground_image``, store its texture in my
  119.        ``foreground_texture``.
  120.  
  121.        """
  122.         if self.foreground_image is not None:
  123.             self.foreground_texture = self.foreground_image.texture
  124.  
  125.     def on_art_source(self, *args):
  126.         """When I get a new ``art_source``, load it as an :class:`Image` and
  127.        store that in ``art_image``.
  128.  
  129.        """
  130.         if self.art_source:
  131.             self.art_image = Image(source=self.art_source)
  132.  
  133.     def on_art_image(self, *args):
  134.         """When I get a new ``art_image``, store its texture in
  135.        ``art_texture``.
  136.  
  137.        """
  138.         if self.art_image is not None:
  139.             self.art_texture = self.art_image.texture
  140.  
  141.     def on_touch_down(self, touch):
  142.         """If I'm the first card to collide this touch, grab it, store my
  143.        metadata in its userdict, and store the relative coords upon
  144.        me where the collision happened.
  145.  
  146.        """
  147.         if not self.collide_point(*touch.pos):
  148.             return
  149.         if 'card' in touch.ud:
  150.             return
  151.         touch.grab(self)
  152.         self.dragging = True
  153.         touch.ud['card'] = self
  154.         touch.ud['idx'] = self.idx
  155.         touch.ud['deck'] = self.deck
  156.         touch.ud['layout'] = self.parent
  157.         self.collide_x = touch.x - self.x
  158.         self.collide_y = touch.y - self.y
  159.  
  160.     def on_touch_move(self, touch):
  161.         """If I'm being dragged, move so as to be always positioned the same
  162.        relative to the touch.
  163.  
  164.        """
  165.         if not self.dragging:
  166.             touch.ungrab(self)
  167.             return
  168.         self.pos = (
  169.             touch.x - self.collide_x,
  170.             touch.y - self.collide_y
  171.         )
  172.  
  173.     def on_touch_up(self, touch):
  174.         """Stop dragging if needed."""
  175.         if not self.dragging:
  176.             return
  177.         touch.ungrab(self)
  178.         self.dragging = False
  179.  
  180.     def copy(self):
  181.         """Return a new :class:`Card` just like me."""
  182.         d = {}
  183.         for att in (
  184.                 'deck',
  185.                 'idx',
  186.                 'ud',
  187.                 'foreground_source',
  188.                 'foreground_color',
  189.                 'foreground_image',
  190.                 'foreground_texture',
  191.                 'background_source',
  192.                 'background_color',
  193.                 'background_image',
  194.                 'background_texture',
  195.                 'outline_color',
  196.                 'content_outline_color',
  197.                 'foreground_outline_color',
  198.                 'art_outline_color',
  199.                 'art_source',
  200.                 'art_color',
  201.                 'art_image',
  202.                 'art_texture',
  203.                 'show_art',
  204.                 'headline_text',
  205.                 'headline_markup',
  206.                 'headline_font_name',
  207.                 'headline_font_size',
  208.                 'headline_color',
  209.                 'midline_text',
  210.                 'midline_markup',
  211.                 'midline_font_name',
  212.                 'midline_font_size',
  213.                 'midline_color',
  214.                 'footer_text',
  215.                 'footer_markup',
  216.                 'footer_font_name',
  217.                 'footer_font_size',
  218.                 'footer_color',
  219.                 'text',
  220.                 'text_color',
  221.                 'markup',
  222.                 'font_name',
  223.                 'font_size'
  224.         ):
  225.             v = getattr(self, att)
  226.             if v is not None:
  227.                 d[att] = v
  228.         return Card(**d)
  229.  
  230. kwargs = {'size': (1, 1), 'headline_text': 'always', 'show_art': False, 'text': "def always(engine, character, thing):\n    assert(thing['shrub_places'])\n    return True", 'midline_text': 'Trigger', 'ud': {'type': 'trigger', 'funcname': 'always'}, 'show_footer': False}
  231.  
  232. Card(**kwargs)  # TypeError: object.__init__() takes no parameters