Linux 中的 Tkinter 外观(主题)

新手上路,请多包涵

我知道 Tkinter 不是那么现代,不是那么酷,使用 PyQt 等可能更好。

但对我来说有趣的是 Tkinter 在 Ubuntu (Linux) 中看起来不是那么丑。看起来 python 的 Tkinter 的 brew 版本(在 OS X 中)是用内置主题编译的,看起来不错:

Mac OS X 的 Tkinter

但是 Ubuntu 的 Tkinter 让我哭了:

Ubuntu Tkinter

我已经读过,为了获得好的主题,我需要使用 ttk,但我不知道具体如何。我的代码如下所示:

 from Tkinter import *

class App():
  def __init__(self, master):
    frame = Frame(master)
    frame.pack()

    master.title("Just my example")
    self.label = Label(frame, text="Type very long text:")

    self.entry = Entry(frame)

    self.button = Button(frame,
                         text="Quit", fg="red", width=20,
                         command=frame.quit)

    self.slogan = Button(frame,
                         text="Hello", width=20,
                         command=self.write_slogan)

    self.label.grid(row=0, column=0)
    self.entry.grid(row=0, column=1)
    self.slogan.grid(row=1, column=0)
    self.button.grid(row=1, column=1)

  def write_slogan(self):
    print "Tkinter is easy to use!"

root = Tk()
app = App(root)
root.mainloop()

如何应用标准的 ubuntu 主题或至少更好的主题?

谢谢。

原文由 ipeacocks 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 978
2 个回答

可以使用以下命令查看 ttk 的所有可用主题:

 $ python
>>> import ttk
>>> s=ttk.Style()
>>> s.theme_names()
('clam', 'alt', 'default', 'classic')

因此,您可以在您的 Tkinter 版本中使用“clam”、“alt”、“default”、“classic”主题。

在尝试了所有这些之后,我认为最好的是“蛤蜊”。您可以通过以下方式使用这个或任何其他方式:

 from Tkinter import *
from ttk import *

class App():
  def __init__(self, master):
    frame = Frame(master)
    frame.pack()

    master.title("Just my example")
    self.label = Label(frame, text="Type very long text:")

    self.entry = Entry(frame)

    self.button = Button(frame,
                         text="Quit", width=15,
                         command=frame.quit)

    self.slogan = Button(frame,
                         text="Hello", width=15,
                         command=self.write_slogan)

    self.label.grid(row=0, column=0)
    self.entry.grid(row=0, column=1)
    self.slogan.grid(row=1, column=0, sticky='e')
    self.button.grid(row=1, column=1, sticky='e')

  def write_slogan(self):
    print "Tkinter is easy to use!"

root = Tk()
root.style = Style()
#('clam', 'alt', 'default', 'classic')
root.style.theme_use("clam")

app = App(root)
root.mainloop()

结果:

在此处输入图像描述

OS X 使用预编译主题“aqua”,因此小部件看起来更好。

此外,Ttk 小部件不支持纯 Tkinter 支持的所有选项。

原文由 ipeacocks 发布,翻译遵循 CC BY-SA 4.0 许可协议

不要将样式硬编码到您的应用程序。

  1. 选择主题
  2. 安装 tcl-ttkthemes , python3-ttkthemes 包。
  3. 添加 *TkTheme: your_theme_name~/.Xresources
  4. 重新加载 X 服务器或执行: xrdb -merge ~/.Xresources && source ~/.profile

原文由 Anton Abrosimov 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏