Example #1
0
class VerticalSliderScreen(Screen):
    def __init__(self):
        super().__init__()
        # Common args for the labels
        labels = {
            'width': 70,
            'fontcolor': WHITE,
            'border': 2,
            'fgcolor': RED,
            'bgcolor': (0, 40, 0),
        }
        # Common args for all three sliders
        table = {
            'fontcolor': WHITE,
            'legends': ('0', '5', '10'),
            'cb_end': self.callback,
        }
        btnquit = Button((390, 240),
                         font=font14,
                         callback=self.quit,
                         fgcolor=RED,
                         text='Quit',
                         shape=RECTANGLE,
                         width=80,
                         height=30)
        self.dial1 = Dial((350, 10),
                          fgcolor=YELLOW,
                          border=2,
                          pointers=(0.9, 0.7))
        self.dial2 = Dial((350, 120),
                          fgcolor=YELLOW,
                          border=2,
                          pointers=(0.9, 0.7))
        self.lstlbl = []
        for n in range(3):
            self.lstlbl.append(Label((80 * n, 240), font=font10, **labels))
        y = 5
        self.slave1 = Slider((80, y),
                             font=font10,
                             fgcolor=GREEN,
                             cbe_args=('Slave1', ),
                             cb_move=self.slave_moved,
                             cbm_args=(1, ),
                             **table)
        self.slave2 = Slider((160, y),
                             font=font10,
                             fgcolor=GREEN,
                             cbe_args=('Slave2', ),
                             cb_move=self.slave_moved,
                             cbm_args=(2, ),
                             **table)
        master = Slider((0, y),
                        font=font10,
                        fgcolor=YELLOW,
                        cbe_args=('Master', ),
                        cb_move=self.master_moved,
                        value=0.5,
                        border=2,
                        **table)
        Screen.objsched.add_thread(self.thread1())
        Screen.objsched.add_thread(self.thread2())
        # On/Off toggle: enable/disable quit button and one slider
        bs = ButtonList(self.cb_en_dis)
        lst_en_dis = [self.slave1, btnquit]
        button = bs.add_button((280, 240),
                               font=font14,
                               fontcolor=BLACK,
                               height=30,
                               width=90,
                               fgcolor=GREEN,
                               shape=RECTANGLE,
                               text='Disable',
                               args=[True, lst_en_dis])
        button = bs.add_button((280, 240),
                               font=font14,
                               fontcolor=BLACK,
                               height=30,
                               width=90,
                               fgcolor=RED,
                               shape=RECTANGLE,
                               text='Enable',
                               args=[False, lst_en_dis])

# CALLBACKS
# cb_end occurs when user stops touching the control

    def callback(self, slider, device):
        print('{} returned {}'.format(device, slider.value()))

    def master_moved(self, slider):
        val = slider.value()
        self.slave1.value(val)
        self.slave2.value(val)
        self.lstlbl[0].value(to_string(val))

# Either slave has had its slider moved (by user or by having value altered)

    def slave_moved(self, slider, idx):
        val = slider.value()
        self.lstlbl[idx].value(to_string(val))

    def quit(self, button):
        Screen.tft.clrSCR()
        Screen.objsched.stop()

    def cb_en_dis(self, button, disable, itemlist):
        for item in itemlist:
            item.greyed_out(disable)


# THREADS

    def thread1(self):
        angle = 0
        yield
        while True:
            yield 0.1
            delta = self.slave1.value()
            angle += pi * 2 * delta / 10
            self.dial1.value(angle)
            self.dial1.value(angle / 10, 1)

    def thread2(self):
        angle = 0
        yield
        while True:
            yield 0.1
            delta = self.slave2.value()
            angle += pi * 2 * delta / 10
            self.dial2.value(angle)
            self.dial2.value(angle / 10, 1)
Example #2
0
class KnobScreen(Screen):
    def __init__(self):
        super().__init__()
        Button((390, 240),
               font=font14,
               callback=self.quit,
               fgcolor=RED,
               text='Quit',
               shape=RECTANGLE,
               width=80,
               height=30)
        self.dial = Dial((120, 0),
                         fgcolor=YELLOW,
                         border=2,
                         pointers=(0.9, 0.7))
        k0 = Knob((0, 0),
                  fgcolor=GREEN,
                  bgcolor=(0, 0, 80),
                  color=(168, 63, 63),
                  border=2,
                  cb_end=self.callback,
                  cbe_args=['Knob1'],
                  cb_move=self.knob_moved,
                  cbm_args=(0, ))
        k1 = Knob((0, 120),
                  fgcolor=WHITE,
                  border=2,
                  arc=pi * 1.5,
                  cb_end=self.callback,
                  cbe_args=['Knob2'],
                  cb_move=self.knob_moved,
                  cbm_args=(1, ))
        # Dropdown
        self.lbl_dd = Label((120, 120),
                            font=font14,
                            width=100,
                            border=2,
                            bgcolor=(0, 40, 0),
                            fgcolor=RED)
        self.dropdown = Dropdown(
            (280, 0),
            font=font14,
            width=100,
            callback=self.cbdb,
            elements=('Dog', 'Cat', 'Rat', 'Goat', 'Snake', 'Pig'))

        Button((280, 70),
               font=font14,
               callback=self.set_dropdown,
               fgcolor=BLUE,
               text='Reset',
               shape=RECTANGLE,
               width=80,
               height=30)  # Test of set by value
        Button((280, 120),
               font=font14,
               callback=self.set_bytext,
               args=('Snake', ),
               fgcolor=CYAN,
               fontcolor=BLACK,
               text='Snake',
               shape=RECTANGLE,
               width=80,
               height=30)  # test set by text
        # Listbox
        self.listbox = Listbox(
            (370, 70),
            font=font14,
            width=105,
            bgcolor=GREY,
            fgcolor=YELLOW,
            select_color=BLUE,
            elements=('aardvark', 'zebra', 'armadillo', 'warthog'),
            callback=self.cblb)
        # On/Off toggle grey style
        self.lbl_style = Label((170, 210),
                               font=font10,
                               value='Current style: grey')
        bstyle = ButtonList(self.cb_style)
        bstyle.add_button((170, 240),
                          font=font14,
                          fontcolor=WHITE,
                          height=30,
                          width=90,
                          fgcolor=RED,
                          shape=RECTANGLE,
                          text='Dim',
                          args=(False, ))
        bstyle.add_button((170, 240),
                          font=font14,
                          fontcolor=WHITE,
                          height=30,
                          width=90,
                          fgcolor=GREEN,
                          shape=RECTANGLE,
                          text='Grey',
                          args=(True, ))
        # On/Off toggle enable/disable
        bs = ButtonList(self.cb_en_dis)
        self.lst_en_dis = (bstyle, k0, k1, self.dropdown, self.listbox)
        bs.add_button((280, 240),
                      font=font14,
                      fontcolor=BLACK,
                      height=30,
                      width=90,
                      fgcolor=GREEN,
                      shape=RECTANGLE,
                      text='Disable',
                      args=(True, ))
        bs.add_button((280, 240),
                      font=font14,
                      fontcolor=BLACK,
                      height=30,
                      width=90,
                      fgcolor=RED,
                      shape=RECTANGLE,
                      text='Enable',
                      args=(False, ))


# CALLBACKS
# cb_end occurs when user stops touching the control

    def callback(self, knob, control_name):
        print('{} returned {}'.format(control_name, knob.value()))

    def knob_moved(self, knob, pointer):
        val = knob.value()  # range 0..1
        self.dial.value(2 * (val - 0.5) * pi, pointer)

    def quit(self, button):
        Screen.shutdown()

    def cb_en_dis(self, button, disable):
        for item in self.lst_en_dis:
            item.greyed_out(disable)

    def cb_style(self, button, desaturate):
        self.lbl_style.value(''.join(
            ('Current style: ', 'grey' if desaturate else 'dim')))
        Screen.set_grey_style(desaturate=desaturate)

    def cbdb(self, dropdown):
        self.lbl_dd.value(dropdown.textvalue())

    def cblb(self, listbox):
        print(listbox.textvalue())

    def set_dropdown(self, button):
        self.dropdown.value(0)

    def set_bytext(self, button, txt):
        self.dropdown.textvalue(txt)
class KnobScreen(Screen):
    def __init__(self):
        super().__init__()
        Button((390, 240), font = font14, callback = self.quit, fgcolor = RED,
            text = 'Quit', shape = RECTANGLE, width = 80, height = 30)
        self.dial = Dial((120, 0), fgcolor = YELLOW, border = 2, pointers = (0.9, 0.7))
        k0 = Knob((0, 0), fgcolor = GREEN, bgcolor=(0, 0, 80), color = (168,63,63), border = 2,
                cb_end = self.callback, cbe_args = ['Knob1'], cb_move = self.knob_moved, cbm_args = (0,))
        k1 = Knob((0, 120), fgcolor = WHITE, border = 2, arc = pi * 1.5,
                cb_end = self.callback, cbe_args = ['Knob2'], cb_move = self.knob_moved, cbm_args = (1,))
# Dropdown
        self.lbl_dd = Label((120, 120), font = font14, width = 100, border = 2, bgcolor = (0, 40, 0), fgcolor = RED)
        self.dropdown = Dropdown((280, 0), font = font14, width = 100, callback = self.cbdb,
                                 elements = ('Dog', 'Cat', 'Rat', 'Goat', 'Snake', 'Pig'))

        Button((280, 70), font = font14, callback = self.set_dropdown, fgcolor = BLUE,
            text = 'Reset', shape = RECTANGLE, width = 80, height = 30) # Test of set by value
        Button((280, 120), font = font14, callback = self.set_bytext, args = ('Snake',), fgcolor = CYAN,
            fontcolor = BLACK, text = 'Snake', shape = RECTANGLE, width = 80, height = 30) # test set by text
# Listbox
        self.listbox = Listbox((370, 70), font = font14, width = 105,
                               bgcolor = GREY, fgcolor = YELLOW, select_color = BLUE,
                               elements = ('aardvark', 'zebra', 'armadillo', 'warthog'),
                               callback = self.cblb)
# On/Off toggle grey style
        self.lbl_style = Label((170, 210), font = font10, value = 'Current style: grey')
        bstyle = ButtonList(self.cb_style)
        bstyle.add_button((170, 240), font = font14, fontcolor = WHITE, height = 30, width = 90,
                            fgcolor = RED, shape = RECTANGLE, text = 'Dim', args = (False,))
        bstyle.add_button((170, 240), font = font14, fontcolor = WHITE, height = 30, width = 90,
                          fgcolor = GREEN, shape = RECTANGLE, text = 'Grey', args = (True,))
# On/Off toggle enable/disable
        bs = ButtonList(self.cb_en_dis)
        self.lst_en_dis = (bstyle, k0, k1, self.dropdown, self.listbox)
        bs.add_button((280, 240), font = font14, fontcolor = BLACK, height = 30, width = 90,
                      fgcolor = GREEN, shape = RECTANGLE, text = 'Disable', args = (True,))
        bs.add_button((280, 240), font = font14, fontcolor = BLACK, height = 30, width = 90,
                      fgcolor = RED, shape = RECTANGLE, text = 'Enable', args = (False,))

# CALLBACKS
# cb_end occurs when user stops touching the control
    def callback(self, knob, control_name):
        print('{} returned {}'.format(control_name, knob.value()))

    def knob_moved(self, knob, pointer):
        val = knob.value() # range 0..1
        self.dial.value(2 * (val - 0.5) * pi, pointer)

    def quit(self, button):
        Screen.shutdown()

    def cb_en_dis(self, button, disable):
        for item in self.lst_en_dis:
            item.greyed_out(disable)

    def cb_style(self, button, desaturate):
        self.lbl_style.value(''.join(('Current style: ', 'grey' if desaturate else 'dim')))
        Screen.set_grey_style(desaturate = desaturate)

    def cbdb(self, dropdown):
        self.lbl_dd.value(dropdown.textvalue())

    def cblb(self, listbox):
        print(listbox.textvalue())

    def set_dropdown(self, button):
        self.dropdown.value(0)

    def set_bytext(self, button, txt):
        self.dropdown.textvalue(txt)