
Let's go through this code step by step. We are creating a program to change the video on the login screen. It allows the user to choose a video from a list or upload their own.
Importing libraries
import subprocess
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gio
import shutil
import os
- subprocess — allows executing system commands, such as copying a file with administrator privileges.
- gi — a library for using GTK+ in Python.
- Gtk, Gio — libraries from GTK that help create a graphical interface.
- shutil and os — modules for working with files and the operating system.
Creating the settings window
class GreeterSettingsWindow(Gtk.Window):
This class is responsible for creating the main window of the program.
def __init__(self):
Gtk.Window.__init__(self, title="DEKUVE Greeter Settings")
self.set_default_size(480, 640)
self.set_resizable(False)
The init method is the constructor of the class. It sets the initial parameters of the window:
- Sets the window title.
- Sets the window size to 480x640.
- Makes the window a fixed size so that it cannot be resized.
Adding interface elements
main_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
self.add(main_box)
main_box — the main container that arranges elements vertically. All elements will be added to this container.
main_box.set_margin_top(0)
main_box.set_margin_bottom(25)
main_box.set_margin_start(25)
main_box.set_margin_end(25)
This sets the container's margins from the edges of the window.
Adding images and buttons
vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10)
main_box.pack_start(vbox, True, True, 0)
vbox — a nested container to which other elements will be added.
image = Gtk.Image.new_from_file("/opt/dekuve-p/dekuve-greeter.svg")
vbox.pack_start(image, False, False, 10)
Gtk.Image displays the image located at /opt/dekuve-p/dekuve-greeter.svg.
upload_button = Gtk.Button(label="Upload and Apply Custom Video")
upload_button.connect("clicked", self.upload_button_clicked)
vbox.pack_start(upload_button, False, False, 0)
A button is created to upload a video. When clicked, it triggers the upload_button_clicked function.
Creating a scrollable area and a video list
scrolled_window = Gtk.ScrolledWindow()
vbox.pack_start(scrolled_window, True, True, 0)
A scrollable area is created here for the video list.
self.files_liststore = Gtk.ListStore(str)
self.files_list = Gtk.TreeView(model=self.files_liststore)
scrolled_window.add(self.files_list)
self.files_liststore — the list to which video names are added. self.files_list — the widget that displays this list.
renderer_text = Gtk.CellRendererText()
column = Gtk.TreeViewColumn("Default Videos", renderer_text, text=0)
self.files_list.append_column(column)
A column titled "Default Videos" is added, which will display the list of available videos.
Adding a button to apply the selected video
apply_button = Gtk.Button(label="Apply Selected Default One")
apply_button.connect("clicked", self.apply_button_clicked)
vbox.pack_start(apply_button, False, False, 0)
The "Apply Selected Default One" button triggers the apply_button_clicked function, which changes the welcome screen video.
The upload_button_clicked function — uploads and copies the video
def upload_button_clicked(self, button):
This function is executed when the video upload button is clicked.
file_chooser = Gtk.FileChooserDialog(
"Choose the video to apply", None, Gtk.FileChooserAction.OPEN,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OPEN, Gtk.ResponseType.OK)
)
Gtk.FileChooserDialog — a file chooser dialog. The user can select a file to upload.
file_filter = Gtk.FileFilter()
file_filter.set_name("Video files")
file_filter.add_mime_type("video/mp4")
file_chooser.add_filter(file_filter)
A filter is created here to allow only .mp4 files to be selected.
response = file_chooser.run()
if response == Gtk.ResponseType.OK:
file_path = file_chooser.get_filename()
self.upload_file(file_path)
file_chooser.destroy()
If the user selects a file and clicks "Open," the file is uploaded using the upload_file function.
The refresh_file_list function — refreshes the video list
def refresh_file_list(self):
self.files_liststore.clear()
video_files = self.get_video_files()
for file in video_files:
self.files_liststore.append([file])
This function clears and refreshes the list of videos displayed in the program.
The upload_file function — copies the video with administrator rights
def upload_file(self, file_path):
dest_path = "/usr/share/web-greeter/themes/dekuve/video/movie.mp4"
try:
subprocess.run(['pkexec', 'cp', file_path, dest_path], check=True)
print("File uploaded successfully")
self.refresh_file_list()
except Exception as e:
print("File upload error:", e)
The selected video is copied to the welcome screen folder using pkexec, which requests administrator rights. After the video is successfully copied, the video list is refreshed.
The get_video_files function — retrieves the list of available videos
def get_video_files(self):
video_dir = "/usr/share/web-greeter/themes/dekuve/video/src/"
try:
video_files = [file for file in os.listdir(video_dir) if file.endswith(".mp4")]
video_files.sort()
return video_files
except Exception as e:
print("Error getting list of files:", e)
return []
This function searches for all .mp4 files in the src folder and returns them as a list.
The apply_button_clicked function — applies the selected video
def apply_button_clicked(self, button):
selection = self.files_list.get_selection()
model, treeiter = selection.get_selected()
if treeiter is not None:
selected_file = model[treeiter][0]
selected_path = os.path.join("/usr/share/web-greeter/themes/dekuve/video/src/", selected_file)
self.upload_file(selected_path)
This function is executed when the "Apply Selected Default One" button is clicked. The video selected by the user is copied to the login screen using upload_file.
Running the Program
win = GreeterSettingsWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
This creates the GreeterSettingsWindow, which launches the main window of the program and exits when closed.
This code provides the user with a convenient graphical interface for changing the video on the login screen, allowing them to choose and upload videos.