Tkinter Look (Theme) in Linux

Tkinter look (theme) in Linux

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:

Sample Image

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

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

Making Tk look like a native Linux app

Tk has basically two sets of widgets, the classic widgets and the themed widgets. The classic widgets are default because of the need to support old applications, but new code should use the themed widgets. (They can't be overloaded because they work in quite different ways, and there's a lot of code out there that depends on the old classic way. Alas.) With the right theme, the themed widgets (typically referred to as Ttk) look native and behave natively on Windows and OSX.

There are partial Ttk themes that delegate to Gtk and Qt available (this presentation from last year shows them in action) but they're partial, in large part because of the poor quality of the themes (Gtk/Qt themes, not Ttk themes; this is confusing!) in typical use with those toolkits. The major problem is apparently in the reporting of metrics, which often seems to be wrong (resulting in widgets getting the wrong size or visual elements being put in the wrong place); I understand from talking to the author of that paper that a significant fraction of themes only happen to work with Gtk/Qt, but cannot verify this from personal experience. (I'm on OSX so I use Ttk's direct native support.)

How do I change a Tkinter theme globally?

As @j_4321 said, I am obviously a Tkinter novice (only about 6 months of experience!) as I didn't know that I had to use TTK widgets. Thank you @j_4321 !

Are Python tkinter ttk themes available based on the operating system

I don't think they are (all) cross-platform, not only from my experience but also from the following excerpt from the paper "The Tile Widget Set" (pp. 1-2) by Joe English, 2004 (a little bit old, but I think these things still hold today).

Figure 1 shows 3 different buttons. The first is a classic Motif-style button. Notice the outer highlight ring (indicating keyboard focus) and the thick inner border (indicating that this is the "default" button).
The second is a Windows-style button: it has a slightly different border and the focus indicator is a dashed box drawn inside the border instead of a solid box on the outside.
The third button is a different possibility altogether.

Except those aren't really three different buttons: it's actually the same button drawn under three different themes.

Sample Image

The Tile package includes several built-in themes. The default theme on X11 has a new, streamlined look; a classic theme implementing Tk's current Motif-like appearance is also available.

On Windows XP, the xpnative theme uses the Windows "Visual Styles" API to make Tk widgets indistinguishable from native controls. On other versions of Windows, the winnative theme matches the Microsoft Windows User Experience guidelines.

On Mac OSX, the aqua theme uses the Carbon Appearance Manager for
(almost-)native appearance on that platform.

New themes can be implemented as add-on packages written in Tcl or in C, depending on the level of customization required.

A Tile theme is a collection of elements, layouts, and styles, which are combined with widget options to determine the look and feel.

So, in conclusion, there are custom themes for different operating systems. You can also implement yours using C or Tcl.

Linux - Synchronising tkinter GUI background and foreground colors with system theme colors

This answer is what I had discovered after trying the instructions described in this Reference. Ubuntu 20.04 distro uses the gnome display manager and its appearances are defined by gnome-shell theme that are stored in CSS format. To access them, the gresource command has to be used to extract it.

Steps:

Firstly, make a copy of /usr/share/gnome-shell/theme/Yaru/gnome-shell-theme.gresource, e.g.:

$ cp /usr/share/gnome-shell/theme/Yaru/gnome-shell-theme.gresource ~/tmp
$ cd ~/tmp.

Next run the following command. From the command output, identify the CSS file of interest. In my case, it is /org/gnome/shell/theme/Yaru-dark/gnome-shell.css.

$ gresource list gnome-shell-theme.gresource
/org/gnome/shell/theme/Yaru-dark/gnome-shell-high-contrast.css
/org/gnome/shell/theme/Yaru-dark/gnome-shell.css
/org/gnome/shell/theme/Yaru/gnome-shell-high-contrast.css
/org/gnome/shell/theme/Yaru/gnome-shell.css
/org/gnome/shell/theme/calendar-today.svg
/org/gnome/shell/theme/checkbox-dark.svg
/org/gnome/shell/theme/checkbox-focused-dark.svg
/org/gnome/shell/theme/checkbox-focused.svg
/org/gnome/shell/theme/checkbox-off-dark.svg
/org/gnome/shell/theme/checkbox-off-focused-dark.svg
/org/gnome/shell/theme/checkbox-off-focused.svg
/org/gnome/shell/theme/checkbox-off.svg
/org/gnome/shell/theme/checkbox.svg
/org/gnome/shell/theme/dash-placeholder.svg
/org/gnome/shell/theme/dmb_trees.jpg
/org/gnome/shell/theme/gdm3.css
/org/gnome/shell/theme/icons/scalable/actions/pointer-double-click-symbolic.svg
/org/gnome/shell/theme/icons/scalable/actions/pointer-drag-symbolic.svg
/org/gnome/shell/theme/icons/scalable/actions/pointer-primary-click-symbolic.svg
/org/gnome/shell/theme/icons/scalable/actions/pointer-secondary-click-symbolic.svg
/org/gnome/shell/theme/icons/scalable/status/eye-not-looking-symbolic.svg
/org/gnome/shell/theme/icons/scalable/status/eye-open-negative-filled-symbolic.svg
/org/gnome/shell/theme/icons/scalable/status/keyboard-caps-lock-filled-symbolic.svg
/org/gnome/shell/theme/icons/scalable/status/keyboard-enter-symbolic.svg
/org/gnome/shell/theme/icons/scalable/status/keyboard-hide-symbolic.svg
/org/gnome/shell/theme/icons/scalable/status/keyboard-layout-filled-symbolic.svg
/org/gnome/shell/theme/icons/scalable/status/keyboard-shift-filled-symbolic.svg
/org/gnome/shell/theme/icons/scalable/status/message-indicator-symbolic.svg
/org/gnome/shell/theme/no-events.svg
/org/gnome/shell/theme/no-notifications.svg
/org/gnome/shell/theme/pad-osd.css
/org/gnome/shell/theme/process-working.svg
/org/gnome/shell/theme/toggle-off-dark.svg
/org/gnome/shell/theme/toggle-off-hc.svg
/org/gnome/shell/theme/toggle-off.svg
/org/gnome/shell/theme/toggle-on-dark.svg
/org/gnome/shell/theme/toggle-on-hc.svg
/org/gnome/shell/theme/toggle-on.svg

To extract that CSS file, run this command:

gresource extract gnome-shell-theme.gresource /org/gnome/shell/theme/Yaru-dark/gnome-shell.css > output_yaru_dark.css

Using any text editor, I was able to view output_yaru_dark.css to find the global background colour is defined under:

/* Global Values */
stage {
font-size: 11pt;
color: #3D3D3D; } <--- global background colour for the Yaru theme.

How do I change the overall theme of a tkinter application?

To change the theme, call .theme_use() with the theme's name as the argument.

From https://infohost.nmt.edu/tcc/help/pubs/tkinter/web/ttk-theme-layer.html

A number of operations related to themes require that you have
available an instance of the ttk.Style() class (in the Python sense of
class). For example, to obtain a list of the available themes in your
installation:

>>> import ttk  # import tkinter.ttk as ttk for Python 3
>>> s=ttk.Style()
>>> s.theme_names()
('clam', 'alt', 'default', 'classic')

The .theme_names() method returns a tuple containing the names of the
available styles. The 'classic' theme gives you the original, pre-ttk
appearance.

To determine which theme you get by default, use the .theme_use()
method with no arguments. To change the current theme, call this same
method with the desired theme name as the argument:

>>> s.theme_use()
'default'
>>> s.theme_use('alt')
>>> s.theme_use()
'alt'

TK and TTK looks very different, is possibly to make TTK looks like TK?

Here I found a solution and is use of pmw toolkit, is like an extension for tk that provides tab controls with the main tk style.

A Tab with tk styled gtk in linux



Related Topics



Leave a reply



Submit