Reputation: 31
I'm making a GUI in Tkinter and want the sun valley theme (https://github.com/rdbende/Sun-Valley-ttk-theme) to effect all my windows.
When I run my code only the first window in this case the Test class has the theme, when I click the button and run my next window Main the theme isn't applied.
I've tried it with and without Test.destroy(), not using sv_ttk.use_dark_theme() in Main() and I have also tried switching the theme back and forth with no change.
Here are two screen shots of both windows afterwards is my code:
Test() window:
Main() window:
import tkinter as tk
from tkinter import ttk
import sv_ttk
class Test(tk.Tk):
def __init__(self):
super().__init__()
sv_ttk.use_dark_theme()
self.geometry('250x150')
self.title("Test")
self.resizable(0,0)
self.columnconfigure(0, weight=1)
self.columnconfigure(1, weight=3)
testButton = ttk.Button(self, text="Press me!", command=lambda : self.newWindow()).grid(column=0, row=3, sticky=tk.E, padx=5, pady=5)
def newWindow(self):
self.newWindow = Main()
Test.destroy()
class Main(tk.Tk):
def __init__(self):
super().__init__()
sv_ttk.use_dark_theme()
self.geometry('1200x800')
self.title("Main")
self.resizable(0,0)
self.columnconfigure(0, weight=1)
self.columnconfigure(1, weight=1)
self.test = ttk.Checkbutton(self, text="testing", style="Toggle.TButton")
self.test.grid(column=0, row=0, sticky=tk.W, padx=5, pady=5)
if __name__ =="__main__":
Test=Test()
Test.mainloop()
Upvotes: 0
Views: 491
Reputation: 21
The problem is that you are using tk.Tk()
for both Test and Main classes, which creates separate Tkinter instances for each window. what you can do instead is you should use tk.Toplevel()
for additional windows after the initial main window. This worked for me and should work for you too:
import tkinter as tk
from tkinter import ttk
import sv_ttk
class Test(tk.Tk):
def __init__(self):
super().__init__()
sv_ttk.use_dark_theme()
self.geometry('250x150')
self.title("Test")
self.resizable(0,0)
self.columnconfigure(0, weight=1)
self.columnconfigure(1, weight=3)
testButton = ttk.Button(self, text="Press me!", command=lambda :
self.newWindow())
testButton.grid(column=0, row=3, sticky=tk.E, padx=5, pady=5)
def newWindow(self):
self.newWindow = Main(self)
self.withdraw()
class Main(tk.Toplevel):
def __init__(self, master=None):
super().__init__(master)
sv_ttk.use_dark_theme()
self.geometry('1200x800')
self.title("Main")
self.resizable(0,0)
self.columnconfigure(0, weight=1)
self.columnconfigure(1, weight=1)
self.test = ttk.Checkbutton(self, text="testing", style="Toggle.TButton")
self.test.grid(column=0, row=0, sticky=tk.W, padx=5, pady=5)
self.protocol("WM_DELETE_WINDOW", self.on_close)
def on_close(self):
self.master.deiconify()
self.destroy()
if __name__ =="__main__":
app = Test()
app.mainloop()
Upvotes: 0