import random
import tkinter
from tkinter import ttk
from tkinter import messagebox
class App(object):
def __init__(self):
self.root = tkinter.Tk()
self.style = ttk.Style()
available_themes = self.style.theme_names()
random_theme = random.choice(available_themes)
self.style.theme_use(random_theme)
self.root.title(random_theme)
frm = ttk.Frame(self.root)
frm.pack(expand=True, fill='both')
# create a Combobox with themes to choose from
self.combo = ttk.Combobox(frm, values=available_themes)
self.combo.pack(padx=32, pady=8)
# make the Enter key change the style
self.combo.bind('<Return>', self.change_style)
# make a Button to change the style
button = ttk.Button(frm, text='OK')
button['command'] = self.change_style
button.pack(pady=8)
def change_style(self, event=None):
"""set the Style to the content of the Combobox"""
content = self.combo.get()
try:
self.style.theme_use(content)
except tkinter.TclError as err:
messagebox.showerror('Error', err)
else:
self.root.title(content)
app = App()
app.root.mainloop()
4 回答
我发现TkDocs教程非常有用 . 它描述了使用Python和
Tkinter
和ttk
构建Tk
接口,并记录了Python 2和3之间的差异 . 它还有Perl,Ruby和Tcl中的示例,因为目标是教授Tk本身,而不是特定语言的绑定 .我从头到尾都没有经历过整个事情,而是只使用了许多主题作为我坚持的事情的例子,但它非常具有教学性和舒适性 . 今天阅读介绍和前几节让我觉得我将开始研究其余部分 .
最后,它是最新的,该网站有一个非常漂亮的外观 . 他还有一些其他页面值得一试(Widgets,Resources,Blog) . 这家伙做了很多事情,不仅教授Tk,还提高了人们的理解,认为它不是曾经的丑陋野兽 .
我推荐NMT Tkinter 8.5 reference .
Themed widgets
Customizing and creating ttk themes and styles
Finding and using themes
Using and customizing ttk styles
The ttk element layer
某些示例中使用的模块名称是Python 2.7中使用的模块名称 .
这是Python 3中名称更改的参考:link
ttk的一个便利是你可以选择一个预先存在的theme,
这是一整套Styles应用于ttk小部件 .
这是我写的一个例子(对于Python 3),它允许您从Combobox中选择任何可用的主题:
旁注:我注意到使用Python 3.3时有一个'vista'主题(但不是2.7) .
我建议阅读documentation . 它简单而有权威,对初学者有益 .
它并不是很新鲜,但是this很简洁,而且从我看到的Python 2和3的有效性 .