Update utilities.py

This commit is contained in:
Saifeddine ALOUI 2024-10-08 12:00:40 +02:00 committed by GitHub
parent edba481804
commit 775806526b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -334,34 +334,45 @@ def show_custom_dialog(title, text, options):
except Exception as ex: except Exception as ex:
ASCIIColors.error(ex) ASCIIColors.error(ex)
return show_console_custom_dialog(title, text, options) return show_console_custom_dialog(title, text, options)
def show_yes_no_dialog(title, text):
def show_yes_no_dialog(title, text):
try: try:
import sys if sys.platform.startswith('win'):
print(sys.executable) return show_windows_dialog(title, text)
import pipmaster as pm elif sys.platform.startswith('darwin'):
pm.package_manager = sys.executable + " -m pip" return show_macos_dialog(title, text)
if not pm.is_installed("tk"): elif sys.platform.startswith('linux'):
pm.install("tk") return show_linux_dialog(title, text)
import tkinter as tk else:
from tkinter import messagebox return console_dialog(title, text)
# Create a new Tkinter root window and hide it
root = tk.Tk()
root.withdraw()
# Make the window appear on top
root.attributes('-topmost', True)
# Show the dialog box
result = messagebox.askyesno(title, text)
# Destroy the root window
root.destroy()
return result
except Exception as ex: except Exception as ex:
trace_exception(ex) print(f"Error: {ex}")
return yes_or_no_input(text) return console_dialog(title, text)
def show_windows_dialog(title, text):
from ctypes import windll
result = windll.user32.MessageBoxW(0, text, title, 4)
return result == 6 # 6 means "Yes"
def show_macos_dialog(title, text):
script = f'tell app "System Events" to display dialog "{text}" buttons {{"No", "Yes"}} default button "Yes" with title "{title}"'
result = subprocess.run(['osascript', '-e', script], capture_output=True, text=True)
return "Yes" in result.stdout
def show_linux_dialog(title, text):
zenity_path = Path('/usr/bin/zenity')
if zenity_path.exists():
result = subprocess.run([str(zenity_path), '--question', '--title', title, '--text', text], capture_output=True)
return result.returncode == 0
else:
return console_dialog(title, text)
def console_dialog(title, text):
print(f"{title}\n{text}")
while True:
response = input("Enter 'yes' or 'no': ").lower()
if response in ['yes', 'no']:
return response == 'yes'
print("Invalid input. Please enter 'yes' or 'no'.")
def show_message_dialog(title, text): def show_message_dialog(title, text):
import tkinter as tk import tkinter as tk