├── LICENSE ├── README.md ├── TkVisualizer ├── __init__.py └── audio_visualizer.py └── example.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Akash Bora 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TkVisualizer 2 | **Simple audio visualizer widget made with tkinter python.** 3 | 4 | ![Screenshot](https://github.com/Akascape/TkVisualizer/assets/89206401/5f16df2c-0dce-42a6-ac1c-78664314a35a) 5 | 6 | ## TkAudioVisualizer 7 | ### [GitHub repo size](https://github.com/Akascape/TkVisualizer/archive/refs/heads/main.zip) 8 | ### Example: 9 | ```python 10 | from TkVisualizer import TkAudioVisualizer 11 | import customtkinter 12 | 13 | root = customtkinter.CTk() 14 | root.geometry("900x500") 15 | viz = TkAudioVisualizer(root) 16 | viz.pack(fill="both", expand=True, padx=10, pady=10) 17 | 18 | customtkinter.CTkButton(root, text="Start", command=viz.start).pack(fill="x", expand=True, side="left", pady=10, padx=10) 19 | customtkinter.CTkButton(root, text="Stop", command=viz.stop).pack(fill="x", expand=True, side="right", pady=10, padx=10) 20 | root.mainloop() 21 | ``` 22 | 23 | **Valid arguments:** `master`, `bar_width`, `bar_color`, `gradient`, `width`, `height` 24 | 25 | **Valid methods:** `.start()` and `.stop()` 26 | 27 | Note: This widget is in beta stage, further development needed! 28 | -------------------------------------------------------------------------------- /TkVisualizer/__init__.py: -------------------------------------------------------------------------------- 1 | """ 2 | Tk Visualizers 3 | Author: Akash Bora (Akascape) 4 | License: MIT 5 | Homepage: https://github.com/Akascape/TkVisualizer 6 | """ 7 | 8 | __version__ = '0.1' 9 | 10 | from .audio_visualizer import TkAudioVisualizer 11 | -------------------------------------------------------------------------------- /TkVisualizer/audio_visualizer.py: -------------------------------------------------------------------------------- 1 | import tkinter as tk 2 | import random 3 | 4 | class TkAudioVisualizer(tk.Frame): 5 | def __init__(self, 6 | master: any, 7 | gradient: list=["cyan","blue"], 8 | bar_color: str="white", 9 | bar_width: int=7, 10 | **kwargs): 11 | 12 | tk.Frame.__init__(self, master) 13 | self.viz = draw_bars(self, gradient[0], gradient[1], bar_width, bar_color, relief="sunken", **kwargs) 14 | self.viz.pack(fill="both", expand=True) 15 | 16 | def start(self): 17 | """ start the vizualizer """ 18 | if not self.viz._running: 19 | self.viz._running = True 20 | self.viz.update() 21 | 22 | def stop(self): 23 | """ stop the visualizer """ 24 | self.viz._running = False 25 | 26 | class draw_bars(tk.Canvas): 27 | '''A gradient frame which uses a canvas to draw the background''' 28 | def __init__(self, parent, color1, color2, bar_width, bar_color, **kwargs): 29 | tk.Canvas.__init__(self, parent, bg=bar_color, bd=0, highlightthickness=0, **kwargs) 30 | self._color1 = color1 31 | self._color2 = color2 32 | self._bar_width = bar_width 33 | self._running = False 34 | self.after(100, lambda: self._draw_gradient()) 35 | self.bind("", lambda e: self._draw_gradient() if not self._running else None) 36 | 37 | def _draw_gradient(self, event=None): 38 | '''Draw the gradient spectrum ''' 39 | self.delete("gradient") 40 | width = self.winfo_width() 41 | height = self.winfo_height() 42 | limit = width+10 43 | 44 | (r1,g1,b1) = self.winfo_rgb(self._color1) 45 | (r2,g2,b2) = self.winfo_rgb(self._color2) 46 | r_ratio = float(r2-r1) / limit 47 | g_ratio = float(g2-g1) / limit 48 | b_ratio = float(b2-b1) / limit 49 | 50 | for i in range(0, limit, self._bar_width): 51 | bar_height = random.randint(int(limit/8),int(limit/2.5)) 52 | if not self._running: 53 | bar_height = height 54 | nr = int(r1 + (r_ratio * i)) 55 | ng = int(g1 + (g_ratio * i)) 56 | nb = int(b1 + (b_ratio * i)) 57 | 58 | color = "#%4.4x%4.4x%4.4x" % (nr,ng,nb) 59 | self.create_line(i,0,i,bar_height, tags=("gradient",), width=self._bar_width, fill=color) 60 | 61 | self.lower("gradient") 62 | 63 | if self._running: 64 | self.after(150, self._draw_gradient) 65 | 66 | def update(self): 67 | self._draw_gradient() 68 | -------------------------------------------------------------------------------- /example.py: -------------------------------------------------------------------------------- 1 | from TkVisualizer import TkAudioVisualizer 2 | import customtkinter 3 | 4 | root = customtkinter.CTk() 5 | root.geometry("900x500") 6 | viz = TkAudioVisualizer(root) 7 | viz.pack(fill="both", expand=True, padx=10, pady=10) 8 | 9 | customtkinter.CTkButton(root, text="Start", command=viz.start).pack(fill="x", expand=True, side="left", pady=10, padx=10) 10 | customtkinter.CTkButton(root, text="Stop", command=viz.stop).pack(fill="x", expand=True, side="right", pady=10, padx=10) 11 | root.mainloop() 12 | --------------------------------------------------------------------------------