问题
I am trying to incorporate a customizable theme choice in my program. I have been referring to this guide: http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/ttk-themes.html, but I am lost.
Here is a sample code I have been using to try to figure this out. I created a new theme, "Shadow," correctly, but the next part I'm struggling with. Let's just keep it simple and say within the "Shadow" theme I would like to change the frame background to black, the button background to navy and the button foreground to white.
How would I do this?
from tkinter import *
from tkinter import ttk
class Main:
def __init__(self, master):
self.master = master
self.main_button = ttk.Button(self.master, text = 'New', command = self.new_window)
self.main_button.grid()
def new_window(self):
pop_up = Top(self.master)
class Top:
def __init__(self, master):
pop_up = self.pop_up = Toplevel(master)
self.pop_up_frame = ttk.Frame(pop_up, height = 100, width = 100)
self.pop_up_frame.grid(sticky = E+W+S+N)
self.s = ttk.Style()
self.s.theme_create('shadow', parent = 'default')
print(self.s.theme_names())
self.c1_button = ttk.Button(pop_up, text = 'Default', command = self.get_default)
self.c2_button = ttk.Button(pop_up, text = 'Shadow', command = self.get_shadow)
self.c1_button.grid()
self.c2_button.grid()
def get_default(self):
self.s.theme_use('default')
def get_shadow(self):
self.s.theme_use('shadow')
root = Tk()
app = Main(root)
root.mainloop()
As always, thank you!
回答1:
I was making this far too difficult.
Here was the solution for anybody else trying to figure this out:
from tkinter import *
from tkinter import ttk
class Main:
def __init__(self, master):
self.master = master
self.main_button = ttk.Button(self.master, text = 'New', command = self.new_window)
self.main_button.grid()
def new_window(self):
pop_up = Top(self.master)
class Top:
def __init__(self, master):
pop_up = self.pop_up = Toplevel(master)
self.pop_up_frame = ttk.Frame(pop_up, height = 100, width = 100)
self.pop_up_frame.grid(sticky = E+W+S+N)
self.s = ttk.Style()
self.s.theme_create('shadow', parent = 'default')
print(self.s.theme_names())
self.c1_button = ttk.Button(pop_up, text = 'Default', command = self.get_default)
self.c2_button = ttk.Button(pop_up, text = 'Vista', command = self.get_shadow)
self.c1_button.grid()
self.c2_button.grid()
def get_default(self):
self.s.theme_use('default')
def get_shadow(self):
self.s.theme_use('vista')
self.s.configure('TButton', foreground = 'white', background = 'blue')
self.s.configure('TFrame', background = 'black')
root = Tk()
app = Main(root)
root.mainloop()
回答2:
To simplify Gregory6106's answer, you can modify your current theme theme using .configure()
.
from tkinter import *
from tkinter import ttk
class MainPage(ttk.Frame):
def __init__(self, master):
ttk.Frame.__init__(self, master)
self.main_label = ttk.Label(self, text="Pointless Text")
self.main_button = ttk.Button(self, text="Pointless Button")
self.main_label.pack(padx=5, pady=5)
self.main_button.pack(padx=5, pady=5)
# Configure custom theme
self.s = ttk.Style()
self.s.configure('TLabel', foreground='red', background='grey')
self.s.configure('TButton', foreground='white', background='blue')
self.s.configure('TFrame', background='black')
root = Tk()
app_frame = MainPage(root)
app_frame.pack()
root.mainloop()
Note: This solution works by configuring the pre-existing, theme rather than creating a new one. As a result, this may not work if you plan on swapping back to the original theme during runtime.
来源:https://stackoverflow.com/questions/35439598/ttk-creating-and-using-a-custom-theme