Example #1
0
    def test_tickscale_methods(self):
        scale = TickScale(self.window, from_=0, to=10, orient='horizontal')
        scale.pack()
        self.window.update()
        self.assertEqual(scale.cget('digits'), -1)
        self.assertEqual(scale['tickinterval'], 0)
        self.assertEqual(scale['resolution'], 0)
        self.assertTrue(scale.cget('showvalue'))
        self.assertEqual(scale['from'], 0)
        self.assertEqual(scale.cget('to'), 10)
        keys = [
            'command', 'variable', 'orient', 'from', 'to', 'value', 'length',
            'takefocus', 'cursor', 'style', 'class', 'tickinterval',
            'showvalue', 'digits'
        ]
        self.assertTrue(all(key in scale.keys() for key in keys))

        scale.config(from_=-1)
        self.window.update()
        self.assertEqual(scale['from'], -1)

        scale.configure({'to': 20, 'tickinterval': 5, 'digits': 1})
        self.window.update()
        self.assertEqual(scale['to'], 20)
        self.assertEqual(scale['digits'], 1)
        self.assertEqual(scale['tickinterval'], 5)

        scale.configure(tickinterval=0.01, resolution=0.05)
        self.window.update()
        self.assertEqual(scale['digits'], 2)
        self.assertEqual(scale['resolution'], 0.05)
        self.assertEqual(scale['tickinterval'], 0.05)

        scale.set(1.1036247)
        self.assertEqual(scale.get(), 1.10)

        scale['labelpos'] = 's'
        self.window.update()
        self.assertEqual(scale['labelpos'], 's')

        scale['labelpos'] = 'e'
        self.window.update()
        self.assertEqual(scale['labelpos'], 'e')

        scale['labelpos'] = 'w'
        self.window.update()
        self.assertEqual(scale['labelpos'], 'w')

        scale['tickpos'] = 'n'
        self.window.update()
        self.assertEqual(scale['tickpos'], 'n')

        scale['orient'] = 'vertical'
        self.window.update()

        scale['tickpos'] = 'e'
        self.window.update()
        self.assertEqual(scale['tickpos'], 'e')

        with self.assertRaises(ValueError):
            scale.configure(orient='vertical', tickpos='n')

        with self.assertRaises(ValueError):
            scale.configure(orient='horizontal', tickpos='e')

        with self.assertRaises(ValueError):
            TickScale(self.window, labelpos='ne')

        scale.configure(style='my.Vertical.TScale')
        self.window.update()
        scale.style.configure('my.Vertical.TScale',
                              font='TkDefaultFont 20 italic',
                              sliderlength=50)

        scale.x = 0

        def cmd(value):
            scale.x = 2 * float(value)

        scale.configure(command=cmd)
        self.window.update()
        scale.configure(digits=1)
        scale.set(10)
        self.window.update()
        self.assertEqual(scale.x, 20)
        self.assertEqual(scale.label.cget('text'), '10.0')

        scale['showvalue'] = False
        self.window.update()
        self.assertEqual(scale.label.place_info(), {})

        scale['orient'] = 'horizontal'
        self.window.update()
        scale['style'] = ''
        scale['showvalue'] = True
        self.window.update()
        scale['length'] = 200
        scale['digits'] = 3
        scale.style.configure('Horizontal.TScale',
                              font='TkDefaultFont 20 italic',
                              sliderlength=10)
        self.window.update()
        scale.set(0)
        self.window.update()
        scale.set(20)
        self.window.update()
Example #2
0
class DoubleSlider(ttk.Frame):
    def __init__(self, parent, controller):
        ttk.Frame.__init__(self, parent)
        self.controller = controller

        # style
        style = ttk.Style()
        style.configure('Double.Horizontal.TScale', sliderlenght=30)

        self.top_slider = TickScale(self,
                                    style='Double.Horizontal.TScale',
                                    tickinterval=1,
                                    resolution=1,
                                    from_=1,
                                    to=12,
                                    length=400,
                                    labelpos='s',
                                    command=self.update_value)
        self.top_slider.grid(row=0, column=0, sticky=S)
        self.top_slider.set(12)
        self.top_slider.bind("<ButtonRelease-1>", self.update_value)

        self.bot_slider = TickScale(self,
                                    style='Double.Horizontal.TScale',
                                    tickinterval=1,
                                    resolution=1,
                                    from_=1,
                                    to=12,
                                    length=400,
                                    labelpos='n',
                                    command=self.update_value)
        self.bot_slider.grid(row=1, column=0, sticky=N)

    def reset(self):
        self.top_slider.set(12)
        self.bot_slider.set(1)
        # self.update_value(None)

    def update_value(self, event):
        try:
            entries_len = len(self.master.entries) // 14
            total = [0] * (entries_len)

            if self.top_slider.get() <= self.bot_slider.get():
                smaller_slider = int(self.top_slider.get())
                bigger_slider = int(self.bot_slider.get())
            else:
                smaller_slider = int(self.bot_slider.get())
                bigger_slider = int(self.top_slider.get())

            for i in range(1, 13):
                self.master.labels[i].configure(foreground="black")

            for i in range(smaller_slider, bigger_slider + 2):

                if i < bigger_slider + 1 and i >= smaller_slider:
                    self.master.labels[i].configure(foreground="#0099ff")

                if i > smaller_slider:
                    for j in range(1, entries_len + 1):
                        entry = self.master.entries[i, j].get()
                        if ":" in entry:
                            delta = int(entry.split(":")[0]) * 3600 + int(
                                entry.split(":")[1]) * 60
                            total[j - 1] += delta
                        else:
                            total[j - 1] += int(entry) * 3600

            for i in range(1, entries_len + 1):
                self.master.entries[14, i].delete(0, 10)
                self.master.entries[14, i].insert(
                    0,
                    str(int((total[i - 1] // 3600))) + ":" +
                    str(int(total[i - 1] % 3600) // 60).zfill(2))
        except AttributeError:
            pass

    # returns the range of sliders e.g (2,7)
    def get_range(self):
        less = min(int(self.top_slider.get()), int(self.bot_slider.get()))
        more = max(int(self.top_slider.get()), int(self.bot_slider.get()))
        return ((less, more))