Reputation: 597
I have tkinter root window
and toplevel window
with different themes but when i open the toplevel window
it changes the root window
to the themes set for the toplevel window
I want to maintain the themes set for each window. This results in error _tkinter.TclError: Theme MyStyle already exists
when i close the toplevel window and open it again.
import tkinter as tk
import tkinter.ttk as ttk
def test2():
rt1 = tk.Toplevel()
rt1.geometry("500x500")
s = ttk.Style()
s.theme_create("MyStyle", parent="alt", settings={
"TNotebook": {"configure": {"tabmargins": [2, 5, 2, 0]}},
"TNotebook.Tab": {"configure": {"padding": [50, 8] }}})
s.theme_use("MyStyle")
notebook = ttk.Notebook(rt1)
f1 = tk.Frame(notebook, width=200, height=200)
f2 = tk.Frame(notebook, width=200, height=200)
notebook.add(f1, text="tab 1")
notebook.add(f2, text="tab 2")
notebook.grid(row=0, column=0, sticky="nw")
root = tk.Tk()
root.geometry("500x500")
tree = ttk.Treeview(root, column=("col1", "col2"))
tree.insert("", tk.END, values=("deee", "fjfj","fjjf", "jfjfjf"))
tree.pack()
b3 = tk.Button(root, text="new", command=test2)
b3.place(x=200, y=200)
root.mainloop()
Upvotes: 0
Views: 1722
Reputation: 15236
Right now you are trying to create a style every time the button is pressed and this is what is causing the problem.
Instead just move the theme creation to the the global namespace where it can be created once and it wont be an issue anymore.
import tkinter as tk
import tkinter.ttk as ttk
def test2():
rt1 = tk.Toplevel()
rt1.geometry("500x500")
notebook = ttk.Notebook(rt1)
f1 = tk.Frame(notebook, width=200, height=200)
f2 = tk.Frame(notebook, width=200, height=200)
notebook.add(f1, text="tab 1")
notebook.add(f2, text="tab 2")
notebook.grid(row=0, column=0, sticky="nw")
root = tk.Tk()
root.geometry("500x500")
tree = ttk.Treeview(root, column=("col1", "col2"))
tree.insert("", tk.END, values=("deee", "fjfj","fjjf", "jfjfjf"))
tree.pack()
s = ttk.Style()
s.theme_create("MyStyle", parent="alt", settings={
"TNotebook": {"configure": {"tabmargins": [2, 5, 2, 0]}},
"TNotebook.Tab": {"configure": {"padding": [50, 8] }}})
s.theme_use("MyStyle")
b3 = tk.Button(root, text="new", command=test2)
b3.place(x=200, y=200)
root.mainloop()
Upvotes: 1