Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tkinter look (theme) in Linux

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.

like image 768
ipeacocks Avatar asked Feb 16 '15 23:02

ipeacocks


People also ask

Can tkinter be used in Linux?

Tkinter can be installed in other Linux based distributions from the package manager. You can also install Tkinter packages in Linux by following installation instructions available here.

Can you style tkinter with CSS?

There are no options to style it the same way if you could use CSS. I recently reported to their bug tracker to consider this - at the least to support the same (limited) subset that gtk3 supports, e. g. margin, padding, border, colours.

How do I use tkinter in Ubuntu?

Instructions for Ubuntu Install Tkinter: apt-get install python-tk (restart after) Install setuptools: sudo apt-get install python-setuptools. Install suds: sudo easy_install suds. Install matplotlib: sudo apt-get install python-matplotlib.


2 Answers

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.

like image 169
ipeacocks Avatar answered Oct 24 '22 20:10

ipeacocks


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

like image 24
Sam Avatar answered Oct 24 '22 21:10

Sam