
Hey! Today, we’ll go over a simple Python program that allows you to change themes in DEKUVE. We’ll explain each part of the code in detail so that even those new to programming can understand it.
This program uses GTK – a library that helps create graphical user interfaces. Now, let’s take a look at the code itself.
Importing the necessary modules
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gio
import os
import subprocess
- import gi – this is the standard way to import the library for working with GTK in Python.
- gi.require_version('Gtk', '3.0') – specifies that we will be using the third version of GTK.
- from gi.repository import Gtk, Gio – imports the Gtk and Gio modules so we can create windows and add controls.
- import os and import subprocess – are required for working with files and executing commands in the system.
Creating the main window of the program
Next, we will create the class ThemeChangerWindow, which will be our main window.
class ThemeChangerWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="DEKUVE Themes")
self.set_default_size(300, 400)
Here, we create a new Gtk.Window object and name it DEKUVE Themes. The window size is set to 300x400 pixels.
Adding a container and interface elements
We will create a vertical container vbox, where we will add the elements:
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
vbox.set_margin_top(0)
vbox.set_margin_bottom(25)
vbox.set_margin_start(25)
vbox.set_margin_end(25)
self.add(vbox)
Here, Gtk.Box is a container where the elements will be arranged vertically. We also add padding for a nicer appearance.
Adding images, text, and dropdown lists
image = Gtk.Image.new_from_file("/opt/dekuve-p/dekuve-themes.svg")
vbox.pack_start(image, False, False, 10)
Gtk.Image.new_from_file loads an image. This line adds the DEKUVE logo at the top of the window.
Next, we add text and dropdown lists for selecting themes:
label = Gtk.Label("Select a theme:")
vbox.pack_start(label, False, False, 0)
self.theme_combobox = Gtk.ComboBoxText()
vbox.pack_start(self.theme_combobox, False, False, 0)
label_icons = Gtk.Label("Select an icon theme:")
vbox.pack_start(label_icons, False, False, 0)
self.icon_theme_combobox = Gtk.ComboBoxText()
vbox.pack_start(self.icon_theme_combobox, False, False, 0)
Here, we create two text fields with labels and two dropdown lists (ComboBoxText). One is for selecting a theme, and the other is for selecting an icon theme.
"Apply Theme" Button
The last element is a button to apply the selected theme:
apply_button = Gtk.Button(label="Apply Theme")
apply_button.connect("clicked", self.apply_button_clicked)
vbox.pack_start(apply_button, False, False, 0)
- Gtk.Button creates a button with the label Apply Theme.
- apply_button.connect("clicked", self.apply_button_clicked) – binds the button click to the apply_button_clicked function, which is responsible for applying the theme.
Filling the theme and icon theme lists
def populate_theme_list(self):
themes_dir = os.path.expanduser("~/.themes")
if os.path.exists(themes_dir):
themes = [name for name in os.listdir(themes_dir) if os.path.isdir(os.path.join(themes_dir, name))]
self.theme_combobox.remove_all()
for theme in themes:
self.theme_combobox.append_text(theme)
self.theme_combobox.set_active(0)
This function checks if the themes folder exists and adds all available themes to the theme_combobox list.
Applying the selected theme
The apply_button_clicked function is triggered when the user clicks the Apply Theme button. It takes the selected theme and icon theme and applies them:
def apply_button_clicked(self, button):
active_theme = self.theme_combobox.get_active_text()
active_icon_theme = self.icon_theme_combobox.get_active_text()
if active_theme:
try:
subprocess.run(["xfconf-query", "-c", "xfwm4", "-p", "/general/theme", "-s", active_theme])
subprocess.run(["xfconf-query", "-c", "xsettings", "-p", "/Net/ThemeName", "-s", active_theme])
subprocess.run(["xfconf-query", "-c", "xsettings", "-p", "/Net/IconThemeName", "-s", active_icon_theme])
print(f"XFWM4 and XFCE theme '{active_theme}' and icon theme '{active_icon_theme}' applied successfully.")
os.environ["GSETTINGS_SCHEMA_DIR"] = "/usr/share/glib-2.0/schemas"
subprocess.run(["flatpak", "override", "--user", "--env=GTK_THEME=" + active_theme, "--env=ICON_THEME=" + active_icon_theme])
except Exception as e:
print(f"Error applying theme: {e}")
- xfconf-query – this is the command to change settings in XFCE. We use it to change the window theme (/general/theme), the overall theme (/Net/ThemeName), and the icon theme (/Net/IconThemeName).
- os.environ["GSETTINGS_SCHEMA_DIR"] sets the environment variable for applying the theme in Flatpak.
This is what the full process of creating the program looks like!