Linux中的Tkinter外观(主题) [英] Tkinter look (theme) in Linux

查看:435
本文介绍了Linux中的Tkinter外观(主题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Tkinter并不那么现代,不那么酷,也许最好使用PyQt等.

I know that Tkinter is not so modern, not so cool and maybe better to use PyQt or etc.

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

But it is interesting for me can Tkinter looks not so ugly in Ubuntu (Linux). Looks that brew version (in OS X) of python's Tkinter compiled with built-in theme and looks good:

但是Ubuntu的Tkinter让我哭泣:

But Ubuntu's Tkinter makes me cry:

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

I've read that for good theme I need to use ttk, but I dont know exactly how. My code looks as follow:

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主题或至少更好的主题?

How to apply standard ubuntu theme or at least better theme?

谢谢.

推荐答案

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

All available themes of ttk can be seen with such commands:

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

因此,您可以在您的Tkinter版本中使用'clam','alt','default','classic'主题.

So you can use 'clam', 'alt', 'default', 'classic' themes with your version of Tkinter.

尝试了所有这些之后,我认为最好的是蛤".您可以通过以下方式使用此功能:

After trying all of them I think the best one is 'clam'. You can use this one or any other in following way:

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",因此小部件看起来更好.

OS X uses precompiled theme "aqua" so widgets are looking better.

Ttk窗口小部件也不支持纯Tkinter所支持的所有选项.

Also Ttk widgets do not support all option which pure Tkinter does.

这篇关于Linux中的Tkinter外观(主题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆