|
| 1 | +.. _Custom properties: |
| 2 | + |
| 3 | +Custom Properties |
| 4 | +****************** |
| 5 | + |
| 6 | +Formation studio allows you to specify custom properties for your |
| 7 | +:ref:`Custom widgets`. Owing to the adaptive nature of how the studio |
| 8 | +handles properties adding custom properties is relatively easy. The studio |
| 9 | +relies on proper implementation of the ``configure``, ``keys``, ``cget``, |
| 10 | +``__getitem__`` and ``__setitem__`` methods of a widget for custom properties |
| 11 | +to be correctly detected. Luckily, formation provides utilities |
| 12 | +to achieve this through the :class:`~formation.utils.CustomPropertyMixin` |
| 13 | +Let us add a custom properties `button_color` and `button_bg` to our `Dpad` widget. We will |
| 14 | +modify our class to use the :class:`~formation.utils.CustomPropertyMixin` |
| 15 | +as shown below |
| 16 | + |
| 17 | +.. code-block:: python |
| 18 | +
|
| 19 | + from tkinter import Frame, Button |
| 20 | + from formation.utils import CustomPropertyMixin |
| 21 | +
|
| 22 | + class DPad(CustomPropertyMixin, Frame): |
| 23 | +
|
| 24 | + prop_info = { |
| 25 | + "button_color": { |
| 26 | + "name": "button_color", |
| 27 | + "default": None, |
| 28 | + "setter": "set_btn_fg", |
| 29 | + "getter": "_btn_fg" |
| 30 | + }, |
| 31 | + "button_bg": { |
| 32 | + "name": "button_bg", |
| 33 | + "default": None, |
| 34 | + "setter": "set_btn_bg", |
| 35 | + "getter": "_btn_bg" |
| 36 | + } |
| 37 | + } |
| 38 | +
|
| 39 | + def __init__(self, master, **kw): |
| 40 | + super(DPad, self).__init__(master, **kw) |
| 41 | + self.left = Button(self, text="L", padx=8, pady=5) |
| 42 | + self.right = Button(self, text="R", padx=8, pady=5) |
| 43 | + self.up = Button(self, text="U", padx=8, pady=5) |
| 44 | + self.down = Button(self, text="D", padx=8, pady=5) |
| 45 | +
|
| 46 | + self.up.grid(row=0, column=1) |
| 47 | + self.left.grid(row=1, column=0) |
| 48 | + self.right.grid(row=1, column=2) |
| 49 | + self.down.grid(row=2, column=1) |
| 50 | +
|
| 51 | + self._btns = [self.left, self.right, self.up, self.down] |
| 52 | +
|
| 53 | + self._btn_bg = self.left["bg"] |
| 54 | + self._btn_fg = self.left["fg"] |
| 55 | +
|
| 56 | + def set_btn_bg(self, val): |
| 57 | + for i in self._btns: |
| 58 | + i["bg"] = val |
| 59 | + self._btn_bg = val |
| 60 | +
|
| 61 | + def set_btn_fg(self, val): |
| 62 | + for i in self._btns: |
| 63 | + i["fg"] = val |
| 64 | + self._btn_fg = val |
| 65 | +
|
| 66 | +.. note:: |
| 67 | + The ``CustomPropertyMixin`` is not necessary if configure, keys and the |
| 68 | + other methods are already implemented to accommodate you custom properties. |
| 69 | + It is however advisable to use the mixin as it has been thoroughly tested and |
| 70 | + is less prone to issues. |
| 71 | + |
| 72 | +Our widget is ready for use. We still need to inform the studio on how |
| 73 | +our properties should be handled and what type of values they contain. The studio |
| 74 | +supports the following property types: |
| 75 | + |
| 76 | + * **choice** : Allows selection from a set of values using a dropdown |
| 77 | + widget. Options are specified as a tuple using the ``options`` key. |
| 78 | + * **boolean** : Selection of true or false using a checkbutton |
| 79 | + * **relief** : Allows selection from the available relief types in tkinter |
| 80 | + * **cursor** : Allows selection from available cursor types in tkinter |
| 81 | + * **bitmap** : Allows selection from the built-in bitmaps |
| 82 | + * **color** : Provides a colorpicker dialog to select colors |
| 83 | + * **text** : Allows entry of arbitrary text |
| 84 | + * **textarea** : Similar to text but allows entry of longer texts. |
| 85 | + * **number** : Entry of integers |
| 86 | + * **float** : Entry of floating point numbers |
| 87 | + * **duration** : Allow entry of durations. You can specify the ``units`` |
| 88 | + options which can be one of ('ns', 'ms', 'sec', 'min', 'hrs'). |
| 89 | + * **font** : Selection from available system fonts. It also includes a font |
| 90 | + picker that can pick fonts from anywhere within the studio. |
| 91 | + * **dimension** : Entry of dimension. Currently only supports pixels |
| 92 | + * **anchor** : Allows easy setting of anchor and sticky values by providing |
| 93 | + realtime preview of anchor/sticky behaviour on a dummy widget. Setting the |
| 94 | + ``multiple`` option allows the application of multiple anchors simultaneously |
| 95 | + * **image** : Allows user to pick an image from their local machine |
| 96 | + * **variable** : Allows user to select from variables created by the |
| 97 | + ``Variable pane`` |
| 98 | + * **stringvariable**: A variation of the variable type that only allows |
| 99 | + selection of ``tk.StringVar`` |
| 100 | + |
| 101 | +.. note:: |
| 102 | + It is currently not possible to implement your own types but we hope to make |
| 103 | + allow custom types in future. |
| 104 | + |
| 105 | +To specify the types our custom properties, we will modify the meta class as |
| 106 | +shown below: |
| 107 | + |
| 108 | +.. code-block:: python |
| 109 | +
|
| 110 | + class DPadMeta(DPad, metaclass=WidgetMeta): |
| 111 | + display_name = 'D Pad' |
| 112 | + impl = DPad |
| 113 | + icon = "gaming" |
| 114 | + is_container = False |
| 115 | + initial_dimensions = 90, 100 |
| 116 | +
|
| 117 | + DEF_OVERRIDES = { |
| 118 | + "button_color": { |
| 119 | + "display_name": "button color", |
| 120 | + "type": "color", |
| 121 | + # you can specify additional options supported by type here |
| 122 | + "name": "button_color" |
| 123 | + }, |
| 124 | + "button_bg": { |
| 125 | + "display_name": "button bg", |
| 126 | + "type": "color", |
| 127 | + "name": "button_bg" |
| 128 | + } |
| 129 | + } |
| 130 | +
|
| 131 | +``DEF_OVERRIDES`` is a special attribute checked at runtime by the studio to |
| 132 | +make decisions on what properties to display and how. You can also override |
| 133 | +behaviour of default properties by specifying alternative definitions here. |
| 134 | + |
| 135 | +.. note:: |
| 136 | + The key and the ``name`` should always match to avoid issues. |
| 137 | + |
| 138 | +Assuming your widget is properly setup as explained in :ref:`Custom widgets`, if |
| 139 | +you open the studio and use your custom widget, the custom properties will |
| 140 | +appear in the ``attributes`` section on the ``stylepane`` as shown below |
| 141 | + |
| 142 | +.. figure:: _static/custom-property.png |
| 143 | + :align: center |
0 commit comments