Search code examples
pythontkintertk-toolkitttk

Ttk style application onto tk class


Using ttk bootstrap I've created a theme and I wish to apply it on an application. So far I got this piece of code :

class MainAppGui(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.title("Interpreter")

        self.buildGUI()

        self.style = ttk.Style(self)
        self.style.configure('dark1', themes_file='..\\themes\\ttkbootstrap_themes_dark1.json')

    def buildGUI(self):

        self.InterBox = tk.Text(self)
        self.but1 = ttk.Button(text='ABC')

        self.InterBox.grid()
        self.but1.grid()

But the style doesn't apply. It might be easy but I don't know what is wrong. First time I try style change.


Solution

  • You have to specify from ttkbootstrap import Style
    https://github.com/israel-dryer/ttkbootstrap/blob/master/docs/tutorial.rst

    #! /usr/bin/env python3
    from ttkbootstrap import Style
    import tkinter.ttk as ttk
    import tkinter as tk
    import os
    
    ##  pip3 install ttkbootstrap
    ##  sudo apt install fonts-symbola
    
    ##  python3 -m ttkbootstrap
    ##  python3 -m ttkcreator
    
    class MainAppGui(tk.Tk):
        def __init__(self):
            tk.Tk.__init__(self)
            self.title("Interpreter")
    
            cwd = os.getcwd()  ##  parent = os.path.dirname( cwd )
            filename = 'ttkbootstrap_themes_dark1.json'
            fullpath = os.path.join( cwd, 'themes', filename )
    
            self.style = Style( theme='dark1', themes_file=fullpath )
            self.buildGUI()
    
        def buildGUI(self):
            self.InterBox = tk.Text(self)
            self.but1 = ttk.Button( text='ABC' )
    
            self.InterBox.grid()
            self.but1.grid()
    
    MainAppGui()
    tk.mainloop()