Equalizer

This example demonstrates the use of styles to differentiate scale or “slider” functions. The ttk.Scale widget is one of several that include orientation in the style class. The overall theme is flatly and the following styles are applied to the widgets to create contrast:

Volume

success.Vertical.TScale

Gain

success.Vertical.TScale

Other

info.Vertical.TScale

Now for some comments on the code: Because I wanted the scale value to be reflected in a label below the scale, this application is a lot more complicated than it really needs to be due to some oddities of the ttk scale implementation. The ttk.Scale widget outputs a double type, which means that in order to display a nice rounded integer, that number has to be converted when updated. Fortunately, the scale widget has a command parameter for setting a callback. The callback will get the scale value, which can then be converted into a nice clean format.

../_images/equalizer.png

Note

For a vertical orientation, the from_ parameter corresponds to the top and to corresponds to the bottom of the widget, so you’ll need to take this into account when you set the minimum and maximum numbers for your scale range.

Run this code live on repl.it

"""
    Author: Israel Dryer
    Modified: 2021-04-07
"""
import tkinter
from random import randint
from tkinter import ttk
from ttkbootstrap import Style


class Application(tkinter.Tk):

    def __init__(self):
        super().__init__()
        self.title('Equalizer')
        self.style = Style()
        self.eq = Equalizer(self)
        self.eq.pack(fill='both', expand='yes')


class Equalizer(ttk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.configure(padding=20)
        controls = ['VOL', '31.25', '62.5', '125', '250', '500', '1K', '2K', '4K', '8K', '16K', 'GAIN']

        # create band widgets
        for c in controls:
            # starting random value
            value = randint(1, 99)
            self.setvar(c, value)

            # container
            frame = ttk.Frame(self, padding=5)
            frame.pack(side='left', fill='y', padx=10)

            # header
            ttk.Label(frame, text=c, anchor='center', font=('Helvetica 10 bold')).pack(side='top', fill='x', pady=10)

            # slider
            scale = ttk.Scale(frame, orient='vertical', from_=99, to=1, value=value)
            scale.pack(fill='y')
            scale.configure(command=lambda val, name=c: self.setvar(name, f'{float(val):.0f}'))

            # set slider style
            scale.configure(style='success.Vertical.TScale' if c in ['VOL', 'GAIN'] else 'info.Vertical.TScale')

            # slider value label
            ttk.Label(frame, textvariable=c).pack(pady=10)


if __name__ == '__main__':
    Application().mainloop()