Customising ttk.Frame widget
我希望能够调整
我想使用
我不使用上面链接中提出的方法的原因是因为我的代码中还有其他小部件需要(并且可以)使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | import tkinter as tk from tkinter import ttk class Window(): def __init__(self, parent): self.parent = parent self.parent.minsize(width=600, height=400) self.widgets() def widgets(self): s1 = ttk.Style() s1.configure('My.TNotebook.Tab', padding=5, background='red') s1.map('My.TNotebook.Tab', background=[('selected', 'green')]) self.nb1 = ttk.Notebook(self.parent, style='My.TNotebook.Tab') self.tab1 = ttk.Frame(self.nb1) self.tab2 = ttk.Frame(self.nb1) self.tab3 = ttk.Frame(self.nb1) self.nb1.add(self.tab1, text='Tab1') self.nb1.add(self.tab2, text='Tab2') self.nb1.add(self.tab3, text='Tab3') self.nb1.place(relx=0.1, rely=0.1, width=500, height=200) self.b1 = ttk.Button(self.parent, text='Quit', command=self.quit) self.b1.place(relx=0.4, rely=0.7, height=70, width=150) def quit(self): self.parent.destroy() root = tk.Tk() app = Window(root) root.mainloop() |
我进行了更多搜索,然后在这里找到了一个有趣且简单的解决方案:http://page.sourceforge.net/html/themes.html。解决问题的关键似乎是需要告诉Python使用哪个主题(我在Windows上运行Python)。在我的答案中,我使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import tkinter as tk from tkinter import ttk class Window(): def __init__(self, parent): self.parent = parent self.parent.minsize(width=600, height=400) self.widgets() def widgets(self): s1 = ttk.Style() s1.theme_use('classic') s1.configure('TNotebook.Tab', background='navajo white') s1.map('TNotebook.Tab', background=[('selected', 'goldenrod'), ('active', 'goldenrod')]) self.nb1 = ttk.Notebook(self.parent) self.nb1.place(relx=0.1, rely=0.1, width=500, height=200) self.tab1 = ttk.Frame(self.nb1) self.tab2 = ttk.Frame(self.nb1) self.tab3 = ttk.Frame(self.nb1) self.nb1.add(self.tab1, text='Tab1') self.nb1.add(self.tab2, text='Tab2') self.nb1.add(self.tab3, text='Tab3') self.b1 = ttk.Button(self.parent, text='Quit', command=self.quit) self.b1.place(relx=0.4, rely=0.7, height=70, width=150) def quit(self): self.parent.destroy() root = tk.Tk() app = Window(root) root.mainloop() |