diff --git a/label_printer_gui.py b/label_printer_gui.py index b02f671..495f24b 100644 --- a/label_printer_gui.py +++ b/label_printer_gui.py @@ -19,6 +19,7 @@ from kivy.graphics import Color, Rectangle import os import threading import platform +import time from print_label import print_label_standalone, get_available_printers from kivy.clock import Clock @@ -36,11 +37,50 @@ class LabelPrinterApp(App): def __init__(self, **kwargs): super().__init__(**kwargs) self.available_printers = self.get_available_printers() + # Clean old PDF backup files on startup + self.cleanup_old_pdfs() def get_available_printers(self): """Get list of available printers (cross-platform)""" return get_available_printers() + def cleanup_old_pdfs(self, days=5): + """ + Delete PDF files older than specified days from pdf_backup folder. + + Args: + days (int): Delete files older than this many days (default: 5) + """ + pdf_backup_dir = 'pdf_backup' + + # Create folder if it doesn't exist + if not os.path.exists(pdf_backup_dir): + os.makedirs(pdf_backup_dir, exist_ok=True) + return + + try: + current_time = time.time() + cutoff_time = current_time - (days * 24 * 3600) # Convert days to seconds + + for filename in os.listdir(pdf_backup_dir): + file_path = os.path.join(pdf_backup_dir, filename) + + # Only process PDF files + if not filename.endswith('.pdf'): + continue + + # Check if file is older than cutoff + if os.path.isfile(file_path): + file_mtime = os.path.getmtime(file_path) + if file_mtime < cutoff_time: + try: + os.remove(file_path) + print(f"Deleted old PDF: {filename}") + except Exception as e: + print(f"Failed to delete {filename}: {e}") + except Exception as e: + print(f"Error during PDF cleanup: {e}") + def build(self): """Build the simplified single-column UI""" self.title = "Label Printing" @@ -212,7 +252,7 @@ class LabelPrinterApp(App): # Use Clock.schedule_once to update UI from main thread Clock.schedule_once(lambda dt: popup.dismiss(), 0) Clock.schedule_once(lambda dt: self.show_popup("Success", "Label printed successfully!"), 0.1) - # Clear inputs after successful print + # Clear inputs after successful print (but keep printer selection) Clock.schedule_once(lambda dt: self.clear_inputs(), 0.2) else: Clock.schedule_once(lambda dt: popup.dismiss(), 0) @@ -226,10 +266,11 @@ class LabelPrinterApp(App): thread.start() def clear_inputs(self): - """Clear all input fields""" + """Clear only the input fields, preserving printer selection""" self.sap_input.text = '' self.qty_input.text = '' self.cable_id_input.text = '' + # Printer selection is NOT cleared - it persists until user changes it def show_popup(self, title, message): """Show a popup message"""