Alles anzeigenDer 15. Teil einer Einführung in die Python3-Bibliothek Tkinter und der lange Weg zum perfekten GUI.
Heute werden wir „fertig“. Wenn überhaupt, steht am Ende dieses Artikels eine Alpha-Version. Dies ist der 15. Teil der Reihe, und wer es bis hierhin geschafft hat, kann stolz auf sich sein, denn leicht war es sicher nicht.
Wer diesen Artikel durchgearbeitet hat, sollte so etwas vor sich haben:
[Blockierte Grafik: https://gnulinux.ch/bl-content…vom2024-12-1218-49-30.png]
Wie geht es jetzt weiter?
Zunächst einmal werde ich mir die Kommentare unter allen Artikeln zu Gemüte führen. Ihr wart fleißig und habt viele Verbesserungsvorschläge und Anmerkungen eingebracht, die ebenfalls umgesetzt werden sollen.
Pakete zählen
Soweit ich weiß, ist es kein Verbrechen bei sich selbst zu klauen. Im Mai habe ich einen kleinen Artikel darüber geschrieben, wie man mithilfe von Python eine eigene Terminal-Version von Neofetch schreibt.
Wir bedienen uns heute daran, denn in dem dort vorgestellten Code befindet sich etwas, das wir benötigen.
Gleich vorab sei gesagt, dass ich auf Debian programmiere. Wer es bis Teil 15 geschafft hat, dem traue ich zu, ein paar Modifikationen selbst vornehmen zu können. In klarer Sprache: Ich habe keine Ahnung, wie man unter anderen Paket-Managern eine Listung vornimmt, wie diese Leistungen strukturiert sind und wie man sie ausließt. Das ist eure kleine "Knobel-Aufgabe".
Codeimport subprocess@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Get installed Debian Packages@@@WCF_PRE_LINEBREAK@@@deb_count = subprocess.run(["dpkg", "--list"], stdout=subprocess.PIPE, text=True)@@@WCF_PRE_LINEBREAK@@@deb_counted = len(deb_count.stdout.splitlines()) - 5@@@WCF_PRE_LINEBREAK@@@print(f"Packages: {deb_counted} (Debian)")Logik:
- Der
Subprocessliest überdpkg --listdie Pakete aus, das nimmt nicht viel Zeit in Anspruch. Der Output sieht in gekürzter Form so aus:CodeGewünscht=Unbekannt/Installieren/R=Entfernen/P=Vollständig Löschen/Halten@@@WCF_PRE_LINEBREAK@@@| Status=Nicht/Installiert/Config/U=Entpackt/halb konFiguriert/@@@WCF_PRE_LINEBREAK@@@ Halb installiert/Trigger erWartet/Trigger anhängig@@@WCF_PRE_LINEBREAK@@@|/ Fehler?=(kein)/R=Neuinstallation notwendig (Status, Fehler: GROSS=schlecht)@@@WCF_PRE_LINEBREAK@@@||/ Name Version >@@@WCF_PRE_LINEBREAK@@@+++-==============================================-============================>@@@WCF_PRE_LINEBREAK@@@ii 7zip 23.01+dfsg-11 >@@@WCF_PRE_LINEBREAK@@@ii aardvark-dns 1.4.0-5 >@@@WCF_PRE_LINEBREAK@@@ii accountsservice 23.13.9-2ubuntu6 >@@@WCF_PRE_LINEBREAK@@@ii acl 2.3.2-1build1.1 >@@@WCF_PRE_LINEBREAK@@@ii adduser 3.137ubuntu1 >@@@WCF_PRE_LINEBREAK@@@ii adwaita-icon-theme 46.0-1 @@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@[...]
- Wir nutzen
len, umganz plump, die ausgegebenen Reihen zu zählen.stdoutmacht das ganze für uns besser nutzbar- Mit
splitlines()erhalten wir eine ordentliche Liste mit Zeilen- 5(minus fünf) rationalisiert die in der oberen Ausgabe zu sehende Header-Zeilen weg, welche keine Information über ein Paket beinhalten.Einbau ein unser GUI
Im Grunde ändern wir nicht viel. Das ganze landet in einer Funktion, damit wir den Code weiter benutzen können. Statt
Codedef get_pkg_count():@@@WCF_PRE_LINEBREAK@@@ # Get installed Debian Packages@@@WCF_PRE_LINEBREAK@@@ deb_count = subprocess.run(["dpkg", "--list"], stdout=subprocess.PIPE, text=True)@@@WCF_PRE_LINEBREAK@@@ deb_counted = len(deb_count.stdout.splitlines()) - 5@@@WCF_PRE_LINEBREAK@@@ return deb_countedDazu noch das obligatorische Label:
Code# Label mit Text Pakages:@@@WCF_PRE_LINEBREAK@@@pkgs_label = tk.Label(stat_frame,text=f"Packages: {get_pkg_count()} (Debian)",background="#FFFFFF",font=("Sans",14))@@@WCF_PRE_LINEBREAK@@@pkgs_label.pack(anchor=tk.NW)Der Terminal-Emulator
Ich hatte mich eigentlich darauf verlassen, dass jemand, der mehr Hirnschmalz besitzt als ich, schon irgendwo im Netz eine Methode bereitgestellt hat, um das "Terminal" auszulesen. Leider ist das nicht so.
Meine Hoffnung war, dass man über update-alternatives etwas auslesen kann. Nun ist es aber so – zumindest glaube ich –, dass openSUSE
update-alternativesgar nicht nutzt. Wie es bei anderen Betriebssystemen aussieht, kann ich auch nicht genau sagen. Deshalb hier gleich die Vorwarnung, dass der Code nicht absturzsicher ist.Codedef get_terminal_emu():@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@ result = subprocess.run(@@@WCF_PRE_LINEBREAK@@@ ["update-alternatives", "--query", "x-terminal-emulator"],@@@WCF_PRE_LINEBREAK@@@ stdout=subprocess.PIPE,@@@WCF_PRE_LINEBREAK@@@ text=True,@@@WCF_PRE_LINEBREAK@@@ check=True@@@WCF_PRE_LINEBREAK@@@ )@@@WCF_PRE_LINEBREAK@@@ # Den relevanten Teil filtern@@@WCF_PRE_LINEBREAK@@@ for line in result.stdout.splitlines():@@@WCF_PRE_LINEBREAK@@@ if line.startswith("Value:"):@@@WCF_PRE_LINEBREAK@@@ return line.split("bin/")[1].strip()Logik
- Wir holen uns über den Standard-Output den Wert von Value.
- Dazu lesen wir jede Zeile mit einem For-Loop aus.
- Und wenn eine Zeile Value enthält, soll diese Zeile angezeigt werden.
- Über
splitschneiden wir den Pfad ab, sodass wir nur noch den Dateinamen haben.Das ist keine saubere Methode, denn rein theoretisch könnte in der Ausgabe noch ein "Value" vorhanden sein. Sollte es bei euch nicht funktionieren, lasst die Ausgabe des Terminals vorerst weg oder versucht euch an eine eigene Lösung.
Der ganze Code
Python## Teil 14 ###@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@import tkinter as tk@@@WCF_PRE_LINEBREAK@@@from PIL import Image, ImageTk@@@WCF_PRE_LINEBREAK@@@import os@@@WCF_PRE_LINEBREAK@@@import socket@@@WCF_PRE_LINEBREAK@@@import distro@@@WCF_PRE_LINEBREAK@@@import platform@@@WCF_PRE_LINEBREAK@@@import psutil@@@WCF_PRE_LINEBREAK@@@import datetime@@@WCF_PRE_LINEBREAK@@@import subprocess@@@WCF_PRE_LINEBREAK@@@from os import popen@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Ließt den Desktop aus@@@WCF_PRE_LINEBREAK@@@def get_desktop_environment():@@@WCF_PRE_LINEBREAK@@@ xdg_current_desktop = os.environ.get("XDG_CURRENT_DESKTOP","").lower()@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@ if xdg_current_desktop == "x-cinnamon" or xdg_current_desktop == "cinnamon":@@@WCF_PRE_LINEBREAK@@@ return "CINNAMON"@@@WCF_PRE_LINEBREAK@@@ elif xdg_current_desktop == "unity":@@@WCF_PRE_LINEBREAK@@@ return "UNITY"@@@WCF_PRE_LINEBREAK@@@ elif xdg_current_desktop == "ubuntu:gnome":@@@WCF_PRE_LINEBREAK@@@ return "GNOME"@@@WCF_PRE_LINEBREAK@@@ elif "gnome" in xdg_current_desktop:@@@WCF_PRE_LINEBREAK@@@ return "GNOME"@@@WCF_PRE_LINEBREAK@@@ elif "plasma" == xdg_current_desktop or "kde" == xdg_current_desktop:@@@WCF_PRE_LINEBREAK@@@ return "KDE"@@@WCF_PRE_LINEBREAK@@@ elif "xfce" == xdg_current_desktop:@@@WCF_PRE_LINEBREAK@@@ return "XFCE"@@@WCF_PRE_LINEBREAK@@@ elif os.environ.get("DESKTOP_SESSION", "").lower() == "lxde-pi-wayfire":@@@WCF_PRE_LINEBREAK@@@ return "PI-WAYFIRE"@@@WCF_PRE_LINEBREAK@@@ elif "mate" == xdg_current_desktop:@@@WCF_PRE_LINEBREAK@@@ return "MATE"@@@WCF_PRE_LINEBREAK@@@ else:@@@WCF_PRE_LINEBREAK@@@ return "Unknown"@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Ließt das KDE Theme aus@@@WCF_PRE_LINEBREAK@@@def get_kde_theme_new():@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@ result = subprocess.run(['plasmashell', '--version'],capture_output=True,text=True, check=True@@@WCF_PRE_LINEBREAK@@@ )@@@WCF_PRE_LINEBREAK@@@ plasma_version = result.stdout.strip()@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@ if "plasmashell 5." in plasma_version:@@@WCF_PRE_LINEBREAK@@@ result = subprocess.run(['kreadconfig5', '--file', '~/.config/kdeglobals', '--group', 'KDE', '--key', 'LookAndFeelPackage'],stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) @@@WCF_PRE_LINEBREAK@@@ output = result.stdout.strip()@@@WCF_PRE_LINEBREAK@@@ return output@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@ if "plasmashell 6." in plasma_version:@@@WCF_PRE_LINEBREAK@@@ result = subprocess.run(['kreadconfig6', '--file', '~/.config/kdeglobals', '--group', 'KDE', '--key', 'LookAndFeelPackage'],stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) @@@WCF_PRE_LINEBREAK@@@ output = result.stdout.strip()@@@WCF_PRE_LINEBREAK@@@ return output@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Ließt die KDE-Icons aus@@@WCF_PRE_LINEBREAK@@@def get_kde_icons():@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@ result = subprocess.run(['plasmashell', '--version'],capture_output=True,text=True, check=True@@@WCF_PRE_LINEBREAK@@@ )@@@WCF_PRE_LINEBREAK@@@ plasma_version = result.stdout.strip()@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@ if "plasmashell 5." in plasma_version:@@@WCF_PRE_LINEBREAK@@@ result = subprocess.run(['kreadconfig5', '--file', '~/.config/kdeglobals', '--group', 'Icons', '--key', 'Theme'],stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) @@@WCF_PRE_LINEBREAK@@@ output = result.stdout.strip()@@@WCF_PRE_LINEBREAK@@@ return output@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@ if "plasmashell 6." in plasma_version:@@@WCF_PRE_LINEBREAK@@@ result = subprocess.run(['kreadconfig6', '--file', '~/.config/kdeglobals', '--group', 'Icons', '--key', 'Theme'],stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) @@@WCF_PRE_LINEBREAK@@@ output = result.stdout.strip()@@@WCF_PRE_LINEBREAK@@@ return output@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@#print(get_kde_icons())@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Ließt das DE-Theme aus@@@WCF_PRE_LINEBREAK@@@def get_desktop_theme():@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@ if get_desktop_environment() == "GNOME":@@@WCF_PRE_LINEBREAK@@@ result = subprocess.run(['gsettings', 'get', 'org.gnome.desktop.interface', 'gtk-theme'],capture_output=True,text=True, check=True@@@WCF_PRE_LINEBREAK@@@ )@@@WCF_PRE_LINEBREAK@@@ return result.stdout.strip().strip("'")@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@ if get_desktop_environment() == "CINNAMON":@@@WCF_PRE_LINEBREAK@@@ result = subprocess.run(['gsettings', 'get', 'org.cinnamon.desktop.interface', 'gtk-theme'],capture_output=True,text=True, check=True@@@WCF_PRE_LINEBREAK@@@ )@@@WCF_PRE_LINEBREAK@@@ return result.stdout.strip().strip("'") @@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@ if get_desktop_environment() == "MATE":@@@WCF_PRE_LINEBREAK@@@ result = subprocess.run(['gsettings', 'get', 'org.mate.interface', 'gtk-theme'],capture_output=True,text=True, check=True@@@WCF_PRE_LINEBREAK@@@ )@@@WCF_PRE_LINEBREAK@@@ return result.stdout.strip().strip("'")@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@ if get_desktop_environment() == "XFCE":@@@WCF_PRE_LINEBREAK@@@ result = subprocess.run(['xfconf-query', '-c', 'xsettings', '-p','/Net/ThemeName'],capture_output=True,text=True, check=True@@@WCF_PRE_LINEBREAK@@@ )@@@WCF_PRE_LINEBREAK@@@ return result.stdout.strip().strip("'")@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@ if get_desktop_environment() == "PI-WAYFIRE":@@@WCF_PRE_LINEBREAK@@@ result = subprocess.run(['gsettings', 'get', 'org.gnome.desktop.interface', 'gtk-theme'],capture_output=True,text=True, check=True@@@WCF_PRE_LINEBREAK@@@ )@@@WCF_PRE_LINEBREAK@@@ return result.stdout.strip().strip("'")@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@ if get_desktop_environment() == "KDE":@@@WCF_PRE_LINEBREAK@@@ return get_kde_theme_new()@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@def get_desktop_icons():@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@ if get_desktop_environment() == "GNOME":@@@WCF_PRE_LINEBREAK@@@ result = subprocess.run(['gsettings', 'get', 'org.gnome.desktop.interface', 'icon-theme'],capture_output=True,text=True, check=True@@@WCF_PRE_LINEBREAK@@@ )@@@WCF_PRE_LINEBREAK@@@ return result.stdout.strip().strip("'")@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@ if get_desktop_environment() == "CINNAMON":@@@WCF_PRE_LINEBREAK@@@ result = subprocess.run(['gsettings', 'get', 'org.cinnamon.desktop.interface', 'icon-theme'],capture_output=True,text=True, check=True@@@WCF_PRE_LINEBREAK@@@ )@@@WCF_PRE_LINEBREAK@@@ return result.stdout.strip().strip("'") @@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@ if get_desktop_environment() == "MATE":@@@WCF_PRE_LINEBREAK@@@ result = subprocess.run(['gsettings', 'get', 'org.mate.interface', 'icon-theme'],capture_output=True,text=True, check=True@@@WCF_PRE_LINEBREAK@@@ )@@@WCF_PRE_LINEBREAK@@@ return result.stdout.strip().strip("'")@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@ if get_desktop_environment() == "XFCE":@@@WCF_PRE_LINEBREAK@@@ result = subprocess.run(['xfconf-query', '-c', 'xsettings', '-p'"/Net/IconThemeName","-s",],capture_output=True,text=True, check=True@@@WCF_PRE_LINEBREAK@@@ )@@@WCF_PRE_LINEBREAK@@@ return result.stdout.strip().strip("'")@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@ if get_desktop_environment() == "PI-WAYFIRE":@@@WCF_PRE_LINEBREAK@@@ result = subprocess.run(['gsettings', 'get', 'org.gnome.desktop.interface', 'icon-theme'],capture_output=True,text=True, check=True@@@WCF_PRE_LINEBREAK@@@ )@@@WCF_PRE_LINEBREAK@@@ return result.stdout.strip().strip("'")@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@ if get_desktop_environment() == "KDE":@@@WCF_PRE_LINEBREAK@@@ return get_kde_icons()@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Ließt den Window-Manager aus@@@WCF_PRE_LINEBREAK@@@def get_window_manager_name():@@@WCF_PRE_LINEBREAK@@@ try:@@@WCF_PRE_LINEBREAK@@@ result = subprocess.run(@@@WCF_PRE_LINEBREAK@@@ ["wmctrl", "-m"], capture_output=True, text=True, check=True@@@WCF_PRE_LINEBREAK@@@ )@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@ output_lines = result.stdout.strip().split("\n")@@@WCF_PRE_LINEBREAK@@@ for line in output_lines:@@@WCF_PRE_LINEBREAK@@@ if line.startswith("Name: "):@@@WCF_PRE_LINEBREAK@@@ window_manager_name = line.split("Name: ")[1]@@@WCF_PRE_LINEBREAK@@@ if window_manager_name == "GNOME Shell":@@@WCF_PRE_LINEBREAK@@@ return "Mutter"@@@WCF_PRE_LINEBREAK@@@ return window_manager_name@@@WCF_PRE_LINEBREAK@@@ except subprocess.CalledProcessError as e:@@@WCF_PRE_LINEBREAK@@@ print(f"Error running wmctrl: {e}")@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Macht die RAM-Größe lesbar@@@WCF_PRE_LINEBREAK@@@def get_size(bytes, suffix="B"):@@@WCF_PRE_LINEBREAK@@@ """@@@WCF_PRE_LINEBREAK@@@ Scale bytes to its proper format@@@WCF_PRE_LINEBREAK@@@ e.g:@@@WCF_PRE_LINEBREAK@@@ 1253656 => '1.20MB'@@@WCF_PRE_LINEBREAK@@@ 1253656678 => '1.17GB'@@@WCF_PRE_LINEBREAK@@@ """@@@WCF_PRE_LINEBREAK@@@ factor = 1024@@@WCF_PRE_LINEBREAK@@@ for unit in ["", "K", "M", "G", "T", "P"]:@@@WCF_PRE_LINEBREAK@@@ if bytes < factor:@@@WCF_PRE_LINEBREAK@@@ return f"{bytes:.2f}{unit}{suffix}"@@@WCF_PRE_LINEBREAK@@@ bytes /= factor@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Findet die Auflösung heraus@@@WCF_PRE_LINEBREAK@@@def get_screen_size():@@@WCF_PRE_LINEBREAK@@@ screen_width = root.winfo_screenwidth()@@@WCF_PRE_LINEBREAK@@@ screen_height = root.winfo_screenheight()@@@WCF_PRE_LINEBREAK@@@ return f"{screen_width}x{screen_height}"@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@def get_sys_uptime():@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@ # System-Startzeit ermitteln@@@WCF_PRE_LINEBREAK@@@ boot_time_timestamp = psutil.boot_time()@@@WCF_PRE_LINEBREAK@@@ boot_time = datetime.datetime.fromtimestamp(boot_time_timestamp)@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@ # Aktuelle Zeit@@@WCF_PRE_LINEBREAK@@@ now = datetime.datetime.now()@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@ # Uptime berechnen@@@WCF_PRE_LINEBREAK@@@ uptime = now - boot_time@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@ # Uptime in Stunden und Minuten umrechnen@@@WCF_PRE_LINEBREAK@@@ uptime_hours, remainder = divmod(uptime.total_seconds(), 3600)@@@WCF_PRE_LINEBREAK@@@ uptime_minutes = remainder // 60@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@ # Weist an das diese Funktion Stunden und Minuten ausgeben soll@@@WCF_PRE_LINEBREAK@@@ return f"{int(uptime_hours)} h , {int(uptime_minutes)} m"@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Setzt das korrekte Logo für die Distro@@@WCF_PRE_LINEBREAK@@@def get_distro_logo():@@@WCF_PRE_LINEBREAK@@@ if distro_id == "debian":@@@WCF_PRE_LINEBREAK@@@ distro_icon.configure(image=debian_logo)@@@WCF_PRE_LINEBREAK@@@ elif distro_id == "arch":@@@WCF_PRE_LINEBREAK@@@ distro_icon.configure(image=arch_logo) @@@WCF_PRE_LINEBREAK@@@ elif distro_id == "mint":@@@WCF_PRE_LINEBREAK@@@ distro_icon.configure(image=mint_logo)@@@WCF_PRE_LINEBREAK@@@ elif distro_id == "ubuntu":@@@WCF_PRE_LINEBREAK@@@ distro_icon.configure(image=ubuntu_logo)@@@WCF_PRE_LINEBREAK@@@ elif distro_id == "opensuse":@@@WCF_PRE_LINEBREAK@@@ distro_icon.configure(image=osuse_logo)@@@WCF_PRE_LINEBREAK@@@ elif distro_id == "fedora":@@@WCF_PRE_LINEBREAK@@@ distro_icon.configure(image=fedora_logo)@@@WCF_PRE_LINEBREAK@@@ else:@@@WCF_PRE_LINEBREAK@@@ distro_icon.configure(image=distro_logo)@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@def get_pkg_count():@@@WCF_PRE_LINEBREAK@@@ # Get installed Debian Packages@@@WCF_PRE_LINEBREAK@@@ deb_count = subprocess.run(["dpkg", "--list"], stdout=subprocess.PIPE, text=True)@@@WCF_PRE_LINEBREAK@@@ deb_counted = len(deb_count.stdout.splitlines()) - 5@@@WCF_PRE_LINEBREAK@@@ return deb_counted@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@def get_terminal_emu():@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@ result = subprocess.run(@@@WCF_PRE_LINEBREAK@@@ ["update-alternatives", "--query", "x-terminal-emulator"],@@@WCF_PRE_LINEBREAK@@@ stdout=subprocess.PIPE,@@@WCF_PRE_LINEBREAK@@@ text=True,@@@WCF_PRE_LINEBREAK@@@ check=True@@@WCF_PRE_LINEBREAK@@@ )@@@WCF_PRE_LINEBREAK@@@ # Den relevanten Teil filtern@@@WCF_PRE_LINEBREAK@@@ for line in result.stdout.splitlines():@@@WCF_PRE_LINEBREAK@@@ if line.startswith("Value:"):@@@WCF_PRE_LINEBREAK@@@ return line.split("bin/")[1].strip()@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Vars für die Labels@@@WCF_PRE_LINEBREAK@@@# Ließt den User aus@@@WCF_PRE_LINEBREAK@@@user = os.environ["USER"]@@@WCF_PRE_LINEBREAK@@@# Ließt den Host aus@@@WCF_PRE_LINEBREAK@@@hostname = socket.gethostname()@@@WCF_PRE_LINEBREAK@@@# Ließt den Pretty Name aus@@@WCF_PRE_LINEBREAK@@@os_release_pretty = distro.name(pretty=True)@@@WCF_PRE_LINEBREAK@@@# Ließt den Kernel aus@@@WCF_PRE_LINEBREAK@@@kernel_release = platform.release()@@@WCF_PRE_LINEBREAK@@@# Basis um den RAM auszulesen@@@WCF_PRE_LINEBREAK@@@svmem = psutil.virtual_memory()@@@WCF_PRE_LINEBREAK@@@# Basis um CPU-Werte auszulesen@@@WCF_PRE_LINEBREAK@@@cpu_freq = psutil.cpu_freq()@@@WCF_PRE_LINEBREAK@@@# Ließt Anzahl der CPU-Kerne aus@@@WCF_PRE_LINEBREAK@@@cpu_core_count = psutil.cpu_count(logical=False)@@@WCF_PRE_LINEBREAK@@@# Gibt die aktuelle Shell aus@@@WCF_PRE_LINEBREAK@@@active_shell = os.environ["SHELL"]@@@WCF_PRE_LINEBREAK@@@# Gibt die Distro-ID aus@@@WCF_PRE_LINEBREAK@@@distro_id = distro.id()@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Erstelle das Hauptfenster@@@WCF_PRE_LINEBREAK@@@root = tk.Tk()@@@WCF_PRE_LINEBREAK@@@root.title("Neofetch-Tk")@@@WCF_PRE_LINEBREAK@@@root.geometry("800x500")@@@WCF_PRE_LINEBREAK@@@root["background"]="#FFFFFF" # Weiß@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Distro Logos@@@WCF_PRE_LINEBREAK@@@distro_logo = tk.PhotoImage(file="images/test.png")@@@WCF_PRE_LINEBREAK@@@arch_logo = tk.PhotoImage(file="images/arch_logo_350.png")@@@WCF_PRE_LINEBREAK@@@debian_logo = tk.PhotoImage(file="images/debian_logo_350.png")@@@WCF_PRE_LINEBREAK@@@mint_logo = tk.PhotoImage(file="images/mint_logo_350.png")@@@WCF_PRE_LINEBREAK@@@suse_logo = tk.PhotoImage(file="images/osuse_logo_350.png")@@@WCF_PRE_LINEBREAK@@@ubuntu_logo = tk.PhotoImage(file="images/ubuntu_logo_350.png")@@@WCF_PRE_LINEBREAK@@@fedora_logo = tk.PhotoImage(file="images/fedora_logo_350.png")@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Einen Frame Zeichen@@@WCF_PRE_LINEBREAK@@@logo_frame = tk.Frame(root,background="#FFFFFF")@@@WCF_PRE_LINEBREAK@@@logo_frame.pack(fill="both",expand=False,side='left',padx=10,pady=10)@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Distro-Logo-Label@@@WCF_PRE_LINEBREAK@@@distro_icon = tk.Label(logo_frame,text="DISTRO LOGO",image=distro_logo,background="#FFFFFF")@@@WCF_PRE_LINEBREAK@@@distro_icon.pack(anchor=tk.NW)@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Einen Frame Zeichen@@@WCF_PRE_LINEBREAK@@@stat_frame = tk.Frame(root,background="#FFFFFF")@@@WCF_PRE_LINEBREAK@@@stat_frame.pack(fill="both",expand=True,side='left',padx=10,pady=10)@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Label mit Text USER@HOST@@@WCF_PRE_LINEBREAK@@@user_host_label = tk.Label(stat_frame,text=f"{user}@{hostname}",background="#FFFFFF",font=("Sans",14))@@@WCF_PRE_LINEBREAK@@@user_host_label.pack(anchor=tk.NW)@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Label mit Text OS:@@@WCF_PRE_LINEBREAK@@@os_label = tk.Label(stat_frame,text=f"OS: {os_release_pretty}",background="#FFFFFF",font=("Sans",14))@@@WCF_PRE_LINEBREAK@@@os_label.pack(anchor=tk.NW)@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Label mit Text Host:@@@WCF_PRE_LINEBREAK@@@host_label = tk.Label(stat_frame,text=f"Host: {hostname}",background="#FFFFFF",font=("Sans",14))@@@WCF_PRE_LINEBREAK@@@host_label.pack(anchor=tk.NW)@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Label mit Text Kernel:@@@WCF_PRE_LINEBREAK@@@kernel_label = tk.Label(stat_frame,text=f"Kernel: {kernel_release}",background="#FFFFFF",font=("Sans",14))@@@WCF_PRE_LINEBREAK@@@kernel_label.pack(anchor=tk.NW)@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Label mit Text Uptime:@@@WCF_PRE_LINEBREAK@@@uptime_label = tk.Label(stat_frame,text=f"Uptime: {get_sys_uptime()}",background="#FFFFFF",font=("Sans",14))@@@WCF_PRE_LINEBREAK@@@uptime_label.pack(anchor=tk.NW)@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Label mit Text Pakages:@@@WCF_PRE_LINEBREAK@@@pkgs_label = tk.Label(stat_frame,text=f"Packages: {get_pkg_count()} (Debian)",background="#FFFFFF",font=("Sans",14))@@@WCF_PRE_LINEBREAK@@@pkgs_label.pack(anchor=tk.NW)@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Label mit Text Shell:@@@WCF_PRE_LINEBREAK@@@shell_label = tk.Label(stat_frame,text=f"Shell: {active_shell}",background="#FFFFFF",font=("Sans",14))@@@WCF_PRE_LINEBREAK@@@shell_label.pack(anchor=tk.NW)@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Label mit Text Resolution:@@@WCF_PRE_LINEBREAK@@@res_label = tk.Label(stat_frame,text=f"Resolution: {get_screen_size()}",background="#FFFFFF",font=("Sans",14))@@@WCF_PRE_LINEBREAK@@@res_label.pack(anchor=tk.NW)@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Label mit Text DE:@@@WCF_PRE_LINEBREAK@@@de_label = tk.Label(stat_frame,text=f"DE: {get_desktop_environment()}",background="#FFFFFF",font=("Sans",14))@@@WCF_PRE_LINEBREAK@@@de_label.pack(anchor=tk.NW)@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Label mit Text Window-Manager@@@WCF_PRE_LINEBREAK@@@wm_label = tk.Label(stat_frame,text=f"WM: {get_window_manager_name()}",background="#FFFFFF",font=("Sans",14))@@@WCF_PRE_LINEBREAK@@@wm_label.pack(anchor=tk.NW)@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Label mit Text Theme@@@WCF_PRE_LINEBREAK@@@wm_theme_label = tk.Label(stat_frame,text=f"Theme: {get_desktop_theme()}",background="#FFFFFF",font=("Sans",14))@@@WCF_PRE_LINEBREAK@@@wm_theme_label.pack(anchor=tk.NW)@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Label mit Text Icons@@@WCF_PRE_LINEBREAK@@@icon_theme_label = tk.Label(stat_frame,text=f"Icons: {get_desktop_icons()}",background="#FFFFFF",font=("Sans",14))@@@WCF_PRE_LINEBREAK@@@icon_theme_label.pack(anchor=tk.NW)@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Label mit Text Terminal@@@WCF_PRE_LINEBREAK@@@terminal_label = tk.Label(stat_frame,text=f"Terminal: {get_terminal_emu()}",background="#FFFFFF",font=("Sans",14))@@@WCF_PRE_LINEBREAK@@@terminal_label.pack(anchor=tk.NW)@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Label mit Text CPU:@@@WCF_PRE_LINEBREAK@@@cpu_label = tk.Label(stat_frame,text=f"CPU: ({cpu_core_count}) @ {cpu_freq.max:.2f} Mhz",background="#FFFFFF",font=("Sans",14))@@@WCF_PRE_LINEBREAK@@@cpu_label.pack(anchor=tk.NW)@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Label mit Text Memory:@@@WCF_PRE_LINEBREAK@@@mem_label = tk.Label(stat_frame,text=f"Memory: {(get_size(svmem.used))}/{get_size(svmem.total)}",background="#FFFFFF",font=("Sans",14))@@@WCF_PRE_LINEBREAK@@@mem_label.pack(anchor=tk.NW)@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Führt get_distro_logo aus@@@WCF_PRE_LINEBREAK@@@get_distro_logo()@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@# Starte die Hauptschleife@@@WCF_PRE_LINEBREAK@@@root.mainloop()@@@WCF_PRE_LINEBREAK@@@@@@WCF_PRE_LINEBREAK@@@Link zu den vorangegangenen Teilen
GNU/Linux.ch ist ein Community-Projekt. Bei uns kannst du nicht nur mitlesen, sondern auch selbst aktiv werden. Wir freuen uns, wenn du mit uns über die Artikel in unseren Chat-Gruppen oder im Fediverse diskutierst. Auch du selbst kannst Autor werden. Reiche uns deinen Artikelvorschlag über das Formular auf unserer Webseite ein.
Quelle: https://gnulinux.ch/python-tk-…-selbst-schreiben-teil-15