refactored error message dialog

This commit is contained in:
2025-04-19 13:31:10 +02:00
parent 2f30baeed2
commit 5e071503d0

View File

@@ -9,7 +9,13 @@ from tkinter import messagebox
from tkinter import ttk from tkinter import ttk
import json import json
from PyInstaller.utils.hooks import collect_data_files
def show_error(message_title: str, message: str, parent: tk.Tk | tk.Toplevel):
messagebox.showwarning(
title=message_title,
message=message,
parent=parent
)
class SettingsWindow(tk.Toplevel): class SettingsWindow(tk.Toplevel):
@@ -32,7 +38,8 @@ class SettingsWindow(tk.Toplevel):
tk.Label(path_frame, text="Datenpfad JSON Datei").grid(row=0, column=0) tk.Label(path_frame, text="Datenpfad JSON Datei").grid(row=0, column=0)
tk.Entry(path_frame, textvariable=self.json_file, width=50).grid(row=1, column=0) tk.Entry(path_frame, textvariable=self.json_file, width=50).grid(row=1, column=0)
tk.Button(path_frame, text="Pfad", command=lambda: self.set_json_path(self.json_file.get())).grid(row=1, column=1) tk.Button(path_frame, text="Pfad", command=lambda: self.set_json_path(self.json_file.get())).grid(row=1,
column=1)
tk.Label(path_frame, text="Datenpfad CSV Export Datei").grid(row=2, column=0) tk.Label(path_frame, text="Datenpfad CSV Export Datei").grid(row=2, column=0)
tk.Entry(path_frame, textvariable=self.csv_file, width=50).grid(row=3, column=0) tk.Entry(path_frame, textvariable=self.csv_file, width=50).grid(row=3, column=0)
@@ -76,7 +83,9 @@ class SettingsWindow(tk.Toplevel):
def ok(self): def ok(self):
if self.json_file.get() == "" or self.csv_file.get() == "": if self.json_file.get() == "" or self.csv_file.get() == "":
messagebox.showwarning(title="Fehlerhafte Konfiguration", message="Pfad für JSON oder CSV Datei fehlt") show_error(message_title="Fehlerhafte Konfiguration",
message="Pfad für JSON oder CSV Datei fehlt",
parent=self)
return return
self.config.set("json", "path", self.json_file.get()) self.config.set("json", "path", self.json_file.get())
self.config.set("csv", "path", self.csv_file.get()) self.config.set("csv", "path", self.csv_file.get())
@@ -167,8 +176,10 @@ class Application:
# leave application if settings are bad # leave application if settings are bad
if not self.config_good: if not self.config_good:
self.show_error("Fehler Konfiguration", show_error(message_title="Fehler Konfiguration",
"Die Konfiguration ist fehlerhaft, bitte prüfe deine config.ini") message="Die Konfiguration ist fehlerhaft, bitte prüfe deine config.ini",
parent=self.root
)
sys.exit() sys.exit()
top_frame = tk.Frame(self.root) top_frame = tk.Frame(self.root)
@@ -241,9 +252,9 @@ class Application:
self.show_settings() self.show_settings()
def show_config_error(self): def show_config_error(self):
if self.show_error("Fehlerhafte Konfiguration", show_error(message_title="Fehlerhafte Konfiguration",
"Konnte benötigte Parameter in config.ini nicht finden"): message="Konnte benötigte Parameter in config.ini nicht finden",
print("Fehlerhafte Konfiguration") parent=self.root)
def on_close(self): def on_close(self):
self.root.destroy() self.root.destroy()
@@ -364,9 +375,10 @@ class Application:
self.address_list = json.load(f) self.address_list = json.load(f)
self.address_list.sort(key=lambda x: (x[0], x[1])) self.address_list.sort(key=lambda x: (x[0], x[1]))
except FileNotFoundError: except FileNotFoundError:
self.show_error( show_error(
message_title="Datei nicht gefunden", message_title="Datei nicht gefunden",
message=f"{self.json_file_name} nicht gefunden, erstelle leere Datei unter {self.json_path}" message=f"{self.json_file_name} nicht gefunden, erstelle leere Datei unter {self.json_path}",
parent=self.root
) )
self.address_list = [["", "firma", "name", "adresse", "plz/ort"]] self.address_list = [["", "firma", "name", "adresse", "plz/ort"]]
with open(self.json_file, "w", encoding="utf-8") as f: with open(self.json_file, "w", encoding="utf-8") as f:
@@ -379,8 +391,10 @@ class Application:
with open(self.json_file, "w", encoding="utf-8") as f: with open(self.json_file, "w", encoding="utf-8") as f:
json.dump(self.address_list, f, indent=4, sort_keys=True) json.dump(self.address_list, f, indent=4, sort_keys=True)
except FileNotFoundError: except FileNotFoundError:
self.show_error( show_error(
message_title="Unexpected Error: File not found?!", message=f"{self.json_file_name} not found" message_title="Unexpected Error: File not found?!",
message=f"{self.json_file_name} not found",
parent=self.root
) )
def export_csv(self): def export_csv(self):
@@ -395,7 +409,10 @@ class Application:
del address[0] del address[0]
writer.writerow(address) writer.writerow(address)
except FileNotFoundError: except FileNotFoundError:
self.show_error("Unexpected error", f"Could not write file {self.csv_file}") show_error(message_title="Unexpected error",
message=f"Could not write file {self.csv_file}",
parent=self.root
)
def populate_table(self): def populate_table(self):
self.delete_all_table_items() self.delete_all_table_items()
@@ -413,10 +430,6 @@ class Application:
for item in self.table.get_children(): for item in self.table.get_children():
self.table.delete(item) self.table.delete(item)
@staticmethod
def show_error(message_title: str, message: str):
messagebox.showwarning(title=message_title, message=message)
if __name__ == '__main__': if __name__ == '__main__':
Application() Application()