From 1a8afa706365d9b32913323d97bd2af89aeda741 Mon Sep 17 00:00:00 2001 From: Willy Date: Wed, 13 Sep 2023 20:39:04 -0400 Subject: [PATCH] Added about menu and exit notification --- python launcher.bat | 2 + requirements.txt | 1 + windows_usbkill.py | 114 ++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 python launcher.bat diff --git a/python launcher.bat b/python launcher.bat new file mode 100644 index 0000000..8c0afd6 --- /dev/null +++ b/python launcher.bat @@ -0,0 +1,2 @@ +@echo off +python "C:\Example\Directory\windows_usbkill\windows_usbkill.py" \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 7f1442b..729366b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ Pillow==10.0.0 pystray==0.19.4 pywin32==306 +win11toast==0.32 WMI==1.5.1 diff --git a/windows_usbkill.py b/windows_usbkill.py index b8e7068..8b43780 100644 --- a/windows_usbkill.py +++ b/windows_usbkill.py @@ -12,7 +12,12 @@ import PIL.Image import ctypes import logging import pythoncom +import time +import tkinter as tk +from tkinter import ttk +from tkinter.ttk import Label +from win11toast import toast from pystray import MenuItem as item if sys.platform != 'win32': @@ -28,14 +33,44 @@ logging.basicConfig(filename="log.txt", format='%(asctime)s | %(message)s', level=logging.INFO) -# Load icon +# Load Image 512x512 current_dir_image = os.path.dirname(os.path.abspath(__file__)) -image_file = os.path.join(current_dir_image, 'assets', 'icon.png') +image_file = os.path.join(current_dir_image, 'assets', 'icon_512x512.png') try: image = PIL.Image.open(image_file) except FileNotFoundError: - asset_error = (None, 'Asset folder missing!', 'Fatal Error!', 0x10) - logging.info("[ERROR] Asset folder missing!") + asset_error = (None, 'File missing! Try to pull from Git!', 'Windows usbkill | Fatal Error!', 0x10) + logging.info("[ERROR] File missing!") + ctypes.windll.user32.MessageBoxW(*asset_error) + os._exit(0) +# Load Image 128x128 +current_dir_image = os.path.dirname(os.path.abspath(__file__)) +image_file2 = os.path.join(current_dir_image, 'assets', 'icon_128x128.png') +try: + image2 = PIL.Image.open(image_file2) +except FileNotFoundError: + asset_error = (None, 'File missing! Try to pull from Git!', 'Windows usbkill | Fatal Error!', 0x10) + logging.info("[ERROR] File missing!") + ctypes.windll.user32.MessageBoxW(*asset_error) + os._exit(0) +# Load ico +current_dir_image = os.path.dirname(os.path.abspath(__file__)) +image_icon = os.path.join(current_dir_image, 'assets', 'icon.ico') +try: + image3 = PIL.Image.open(image_icon) +except FileNotFoundError: + asset_error = (None, 'File missing! Try to pull from Git!', 'Windows usbkill | Fatal Error!', 0x10) + logging.info("[ERROR] File missing!") + ctypes.windll.user32.MessageBoxW(*asset_error) + os._exit(0) +# load exit-icon +current_dir_image = os.path.dirname(os.path.abspath(__file__)) +exit_icon = os.path.join(current_dir_image, 'assets', 'exit-icon_512x512.png') +try: + image4 = PIL.Image.open(exit_icon) +except FileNotFoundError: + asset_error = (None, 'File missing! Try to pull from Git!', 'Windows usbkill | Fatal Error!', 0x10) + logging.info("[ERROR] File missing!") ctypes.windll.user32.MessageBoxW(*asset_error) os._exit(0) @@ -80,19 +115,88 @@ def toggle_pause(): scan_thread = threading.Thread(target=start_scan) scan_thread.start() + +# About Page +def about_section(): + root = tk.Tk() + root.geometry('600x450') + root.resizable(False, False) + root.title('About | Windows usbkill') + root.iconbitmap(image_icon) + + # Declare Exit + def close_window(): + root.destroy() + + # Image + + photo = tk.PhotoImage(file=image_file2) + image_label = ttk.Label( + root, + image=photo, + padding=5 +) + image_label.pack() + + # Label 1 + label = ttk.Label( + root, + text='Windows usbkill', + font=("Helvetica", 14, "bold")) + + label.pack(ipadx=10, ipady=10) + + label2 = ttk.Label( + root, + text = ''' + Author: Willy + Version: 0.2 BETA (9/12/2023) + License: GNU GENERAL PUBLIC LICENSE 3 + ''', + font=("Helvetica", 10)) + + # Label 2 + label2.pack(ipadx=200, ipady=20) + + # Exit Button + button = tk.Button(root, text="Close Window", command=close_window) + button.pack() + + # loop + root.mainloop() + +# Trigger about section +def trigger_about_section(): + about_thread = threading.Thread(target=about_section) + about_thread.start() + # Exit def terminate(): pythoncom.CoInitialize() print("[Info] Exited!") icon = pystray.Icon icon.visible = False - os._exit(0) + def exit_toast(): + toast(app_id="Windows usbkill", + icon=exit_icon, + title='Exited!', + body='Windows usbkill has exited! It will no longer detect changes in the USB ports!' + ) + def os_exit(): + os._exit(0) + + exit_toast_thread = threading.Thread(target=exit_toast) + os_exit_thread = threading.Thread(target=os_exit) + exit_toast_thread.start() + time.sleep(1) + os_exit_thread.start() # Tray def create_tray_icon(): pythoncom.CoInitialize() icon = pystray.Icon("Test", image, 'Windows usbkill', menu=pystray.Menu( pystray.MenuItem("Pause", toggle_pause, checked=lambda item: is_paused), + pystray.MenuItem("About", trigger_about_section), pystray.MenuItem("Exit", terminate) )) icon.run()