Tkinter look (theme) in Linux
Asked Answered
T

5

16

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

But it is interesting for me can Tkinter look 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:

Mac OS X Tkinter

But Ubuntu's Tkinter makes me cry:

Ubuntu Tkinter

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()

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

Thanks.

Transit answered 16/2, 2015 at 23:9 Comment(0)
T
15

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

$ python
>>> import ttk
>>> s=ttk.Style()
>>> s.theme_names()
('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()

Result:

enter image description here

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

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

Transit answered 7/3, 2015 at 17:57 Comment(1)
is it still up to date? it doesn't work for me at all (i'm on ubuntu 18.04.3)Odlo
S
1

To use ttk you have to import it.

from tkinter import *
from tkinter import ttk

After that you should use tkinter widgets like this-label=ttk.Label() or button = ttk.Button()

Schouten answered 9/9, 2017 at 19:35 Comment(0)
A
1

Do not hardcode style to your app.

  1. Choice theme.
  2. Install tcl-ttkthemes, python3-ttkthemes package.
  3. Add *TkTheme: your_theme_name to ~/.Xresources.
  4. Reload X server or execute: xrdb -merge ~/.Xresources && source ~/.profile
Anaclinal answered 23/11, 2022 at 14:9 Comment(0)
C
0

From documentation if you want to use ttk instead of regular Tki widgets:

from Tkinter import *
from ttk import *

several ttk widgets (Button, Checkbutton, Entry, Frame, Label, LabelFrame, Menubutton, PanedWindow, Radiobutton, Scale and Scrollbar) will automatically substitute for the Tk widgets.

You dont seem to use any other widget not covered by ttk. So this should help and enable themed ttk for you. If you want to check what themes are avaliable and how to check a theme, have a look here as well.

Cocks answered 16/2, 2015 at 23:19 Comment(2)
you might select theme manually as in the second link. Also ttk does not necessarily mean a re-skin of widgets. Its benefit is to use e.g. to specify background color for all widgets, or all buttons at once, rather than doing it separately for each widget..Cocks
Actually it is not critical that theme is not exactly the same as i have in my distro. I just want not so ugly theme. How do I need to use this with my code? This variant as I can see is not correct pastebin.com/U9FxEupq I`ve got 2 windows after code launch i.imgur.com/bJAlOQf.pngTransit
K
0

ttkthemes is a module that has 25 themes available you can easily apply all the themes in a ttk widget.

Install the module using following commands:-

Commands:-

1.pip install ttkthemes in cmd or powershell (if in windows)

2.pip3 install ttkthemes in terminal (if in Linux)

And ttkthemes module will be installed

Here is an example:

# example from: https://ttkthemes.readthedocs.io/en/latest/example.html
from tkinter import ttk  # Normal Tkinter.* widgets are not themed!
from ttkthemes import ThemedTk
window = ThemedTk(theme="arc")# you can set any available theme. 
ttk.Button(window, text="Quit", command=window.destroy).pack()
window.mainloop()
Kato answered 16/7, 2021 at 6:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.