From 8e4087df5c0f7f14090cfbbead4110f81a552cac Mon Sep 17 00:00:00 2001 From: sdasda7777 <17746796+sdasda7777@users.noreply.github.com> Date: Thu, 20 Jun 2024 22:11:18 +0200 Subject: [PATCH 1/6] Began tkinter implementation --- devices/linux/Files/main.py | 78 ++++++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 32 deletions(-) diff --git a/devices/linux/Files/main.py b/devices/linux/Files/main.py index 9c09e537a..114a29ede 100644 --- a/devices/linux/Files/main.py +++ b/devices/linux/Files/main.py @@ -351,6 +351,7 @@ def ask(self, question: str, prompt: str = '', default: bool = None) -> int: return 0 if i == nay: return 1 + # TODO: tkinter command = [] if self.graphics == "zenity": command = ['zenity', '--title=' + Config.title, '--width=500', @@ -378,6 +379,7 @@ def show_info(self, data: str) -> None: if self.graphics == 'tty': print(data) return + # TODO: tkinter if self.graphics == "zenity": command = ['zenity', '--info', '--width=500', '--text=' + data] elif self.graphics == "kdialog": @@ -403,6 +405,7 @@ def alert(self, text: str) -> None: if self.graphics == 'tty': print(text) return + # TODO: tkinter if self.graphics == 'zenity': command = ['zenity', '--warning', '--text=' + text] elif self.graphics == "kdialog": @@ -429,6 +432,7 @@ def prompt_nonempty_string(self, show: int, prompt: str, val: str = '') -> str: output = inp.strip() if output != '': return output + # TODO: tkinter command = [] if self.graphics == 'zenity': if val == '': @@ -474,37 +478,41 @@ def __get_username_password_atomic(self) -> None: """ output_fields_separator = "\n\n\n\n\n" while True: - if self.graphics == 'zenity': - command = ['zenity', '--forms', '--width=500', - f"--title={Config.title}", -# f"--text={Messages.credentials_prompt}", - f"--add-entry={Messages.username_prompt}", - f"--add-password={Messages.enter_password}", - f"--add-password={Messages.repeat_password}", - "--separator", output_fields_separator] - elif self.graphics == 'yad': - command = ['yad', '--form', - f"--title={Config.title}", -# f"--text={Messages.credentials_prompt}", - f"--field={Messages.username_prompt}", self.username, - f"--field={Messages.enter_password}:H", - f"--field={Messages.repeat_password}:H", - "--separator", output_fields_separator] - - output = '' - while not output: - shell_command = subprocess.Popen(command, stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - out, _ = shell_command.communicate() - output = out.decode('utf-8') - if self.graphics == 'yad': - output = output[:-(len(output_fields_separator)+1)] - output = output.strip() - if shell_command.returncode == 1: - self.confirm_exit() - - if self.graphics in ('zenity', 'yad'): - self.username, password, password1 = output.split(output_fields_separator) + if self.graphics == 'tkinter': + # TODO: tkinter + self.username, password, password1 = ??? + else: + if self.graphics == 'zenity': + command = ['zenity', '--forms', '--width=500', + f"--title={Config.title}", +# f"--text={Messages.credentials_prompt}", + f"--add-entry={Messages.username_prompt}", + f"--add-password={Messages.enter_password}", + f"--add-password={Messages.repeat_password}", + "--separator", output_fields_separator] + elif self.graphics == 'yad': + command = ['yad', '--form', + f"--title={Config.title}", +# f"--text={Messages.credentials_prompt}", + f"--field={Messages.username_prompt}", self.username, + f"--field={Messages.enter_password}:H", + f"--field={Messages.repeat_password}:H", + "--separator", output_fields_separator] + + output = '' + while not output: + shell_command = subprocess.Popen(command, stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + out, _ = shell_command.communicate() + output = out.decode('utf-8') + if self.graphics == 'yad': + output = output[:-(len(output_fields_separator)+1)] + output = output.strip() + if shell_command.returncode == 1: + self.confirm_exit() + + if self.graphics in ('zenity', 'yad'): + self.username, password, password1 = output.split(output_fields_separator) if not self.__validate_user_name(): continue @@ -531,7 +539,7 @@ def __get_username_password(self) -> None: """ if self.silent: return - elif self.graphics in ('zenity', 'yad'): + elif self.graphics in ('tkinter', 'zenity', 'yad'): self.__get_username_password_atomic() else: password = "a" @@ -569,6 +577,12 @@ def __check_graphics(self, command) -> bool: def __get_graphics_support(self) -> None: if os.environ.get('DISPLAY') is not None: + try: + import tkinter + self.graphics = 'tkinter' + return + except: + pass for cmd in ('yad', 'zenity', 'kdialog'): if self.__check_graphics(cmd): return From 57aa316577706a3ca472b40f5e00c4554f42a292 Mon Sep 17 00:00:00 2001 From: sdasda7777 <17746796+sdasda7777@users.noreply.github.com> Date: Thu, 20 Jun 2024 23:30:30 +0200 Subject: [PATCH 2/6] Implemented rest of prompts using tkinter --- devices/linux/Files/main.py | 206 ++++++++++++++++++++++-------------- 1 file changed, 126 insertions(+), 80 deletions(-) diff --git a/devices/linux/Files/main.py b/devices/linux/Files/main.py index 114a29ede..9e2c9f10b 100644 --- a/devices/linux/Files/main.py +++ b/devices/linux/Files/main.py @@ -351,24 +351,27 @@ def ask(self, question: str, prompt: str = '', default: bool = None) -> int: return 0 if i == nay: return 1 - # TODO: tkinter - command = [] - if self.graphics == "zenity": - command = ['zenity', '--title=' + Config.title, '--width=500', - '--question', '--text=' + question + "\n\n" + prompt] - elif self.graphics == 'kdialog': - command = ['kdialog', '--yesno', question + "\n\n" + prompt, - '--title=' + Config.title] - elif self.graphics == 'yad': - command = ['yad', '--image="dialog-question"', - '--button=gtk-yes:0', - '--button=gtk-no:1', - '--width=500', - '--wrap', - '--text=' + question + "\n\n" + prompt, - '--title=' + Config.title] - returncode = subprocess.call(command, stderr=subprocess.DEVNULL) - return returncode + elif self.graphics == 'tkinter': + from tkinter import messagebox + return 0 if messagebox.askyesno(Config.title, question + "\n\n" + prompt) else 1 + else: + command = [] + if self.graphics == "zenity": + command = ['zenity', '--title=' + Config.title, '--width=500', + '--question', '--text=' + question + "\n\n" + prompt] + elif self.graphics == 'kdialog': + command = ['kdialog', '--yesno', question + "\n\n" + prompt, + '--title=' + Config.title] + elif self.graphics == 'yad': + command = ['yad', '--image="dialog-question"', + '--button=gtk-yes:0', + '--button=gtk-no:1', + '--width=500', + '--wrap', + '--text=' + question + "\n\n" + prompt, + '--title=' + Config.title] + returncode = subprocess.call(command, stderr=subprocess.DEVNULL) + return returncode def show_info(self, data: str) -> None: """ @@ -378,17 +381,19 @@ def show_info(self, data: str) -> None: return if self.graphics == 'tty': print(data) - return - # TODO: tkinter - if self.graphics == "zenity": - command = ['zenity', '--info', '--width=500', '--text=' + data] - elif self.graphics == "kdialog": - command = ['kdialog', '--msgbox', data, '--title=' + Config.title] - elif self.graphics == "yad": - command = ['yad', '--button=OK', '--width=500', '--text=' + data] + elif self.graphics == 'tkinter': + from tkinter import messagebox + messagebox.showinfo(Config.title, data) else: - sys.exit(1) - subprocess.call(command, stderr=subprocess.DEVNULL) + if self.graphics == "zenity": + command = ['zenity', '--info', '--width=500', '--text=' + data] + elif self.graphics == "kdialog": + command = ['kdialog', '--msgbox', data, '--title=' + Config.title] + elif self.graphics == "yad": + command = ['yad', '--button=OK', '--width=500', '--text=' + data] + else: + sys.exit(1) + subprocess.call(command, stderr=subprocess.DEVNULL) def confirm_exit(self) -> None: """ @@ -404,17 +409,19 @@ def alert(self, text: str) -> None: return if self.graphics == 'tty': print(text) - return - # TODO: tkinter - if self.graphics == 'zenity': - command = ['zenity', '--warning', '--text=' + text] - elif self.graphics == "kdialog": - command = ['kdialog', '--sorry', text, '--title=' + Config.title] - elif self.graphics == "yad": - command = ['yad', '--text=' + text] + elif self.graphics == 'tkinter': + from tkinter import messagebox + messagebox.showinfo(Config.title, text) else: - sys.exit(1) - subprocess.call(command, stderr=subprocess.DEVNULL) + if self.graphics == 'zenity': + command = ['zenity', '--warning', '--text=' + text] + elif self.graphics == "kdialog": + command = ['kdialog', '--sorry', text, '--title=' + Config.title] + elif self.graphics == "yad": + command = ['yad', '--text=' + text] + else: + sys.exit(1) + subprocess.call(command, stderr=subprocess.DEVNULL) def prompt_nonempty_string(self, show: int, prompt: str, val: str = '') -> str: """ @@ -432,45 +439,50 @@ def prompt_nonempty_string(self, show: int, prompt: str, val: str = '') -> str: output = inp.strip() if output != '': return output - # TODO: tkinter - command = [] - if self.graphics == 'zenity': - if val == '': - default_val = '' - else: - default_val = '--entry-text=' + val - if show == 0: - hide_text = '--hide-text' - else: - hide_text = '' - command = ['zenity', '--entry', hide_text, default_val, - '--width=500', '--text=' + prompt] - elif self.graphics == 'kdialog': - if show == 0: - hide_text = '--password' - else: - hide_text = '--inputbox' - command = ['kdialog', hide_text, prompt, '--title=' + Config.title] - elif self.graphics == 'yad': - if show == 0: - hide_text = ':H' - else: - hide_text = '' - command = ['yad', '--form', '--field=' + hide_text, - '--text=' + prompt, val] - - output = '' - while not output: - shell_command = subprocess.Popen(command, stdout=subprocess.PIPE, - stderr=subprocess.PIPE) - out, _ = shell_command.communicate() - output = out.decode('utf-8') - if self.graphics == 'yad': - output = output[:-2] - output = output.strip() - if shell_command.returncode == 1: - self.confirm_exit() - return output + elif self.graphics == 'tkinter': + from tkinter import simpledialog + simpledialog.askstring("Input", "Enter a string:", + initialvalue=val + show="*" if show == 0 else "") + else: + command = [] + if self.graphics == 'zenity': + if val == '': + default_val = '' + else: + default_val = '--entry-text=' + val + if show == 0: + hide_text = '--hide-text' + else: + hide_text = '' + command = ['zenity', '--entry', hide_text, default_val, + '--width=500', '--text=' + prompt] + elif self.graphics == 'kdialog': + if show == 0: + hide_text = '--password' + else: + hide_text = '--inputbox' + command = ['kdialog', hide_text, prompt, '--title=' + Config.title] + elif self.graphics == 'yad': + if show == 0: + hide_text = ':H' + else: + hide_text = '' + command = ['yad', '--form', '--field=' + hide_text, + '--text=' + prompt, val] + + output = '' + while not output: + shell_command = subprocess.Popen(command, stdout=subprocess.PIPE, + stderr=subprocess.PIPE) + out, _ = shell_command.communicate() + output = out.decode('utf-8') + if self.graphics == 'yad': + output = output[:-2] + output = output.strip() + if shell_command.returncode == 1: + self.confirm_exit() + return output def __get_username_password_atomic(self) -> None: """ @@ -478,9 +490,43 @@ def __get_username_password_atomic(self) -> None: """ output_fields_separator = "\n\n\n\n\n" while True: + password = "a" + password1 = "b" if self.graphics == 'tkinter': - # TODO: tkinter - self.username, password, password1 = ??? + import tkinter as tk + + root = tk.Tk() + root.title("Login Prompt") + + username_label = tk.Label(root, text=Messages.username_prompt) + username_label.pack() + + username_entry = tk.Entry(root, textvariable=tk.StringVar(root, value=self.username)) + username_entry.pack() + + password_label = tk.Label(root, text=Messages.enter_password) + password_label.pack() + + password_entry = tk.Entry(root, show="*") + password_entry.pack() + + password1_label = tk.Label(root, text=Messages.repeat_password) + password1_label.pack() + + password1_entry = tk.Entry(root, show="*") + password1_entry.pack() + + def submit(installer): + def inner(): + nonlocal password, password1 + (installer.username, password, password1) = (username_entry.get(), password_entry.get(), password1_entry.get()) + root.destroy() + return inner + + login_button = tk.Button(root, text="Login", command=submit(self)) + login_button.pack() + + root.mainloop() else: if self.graphics == 'zenity': command = ['zenity', '--forms', '--width=500', @@ -581,7 +627,7 @@ def __get_graphics_support(self) -> None: import tkinter self.graphics = 'tkinter' return - except: + except ModuleNotFoundError: pass for cmd in ('yad', 'zenity', 'kdialog'): if self.__check_graphics(cmd): From 8926b5f93bb67593ff8096cf99cde9f1096be0e8 Mon Sep 17 00:00:00 2001 From: sdasda7777 <17746796+sdasda7777@users.noreply.github.com> Date: Fri, 21 Jun 2024 07:59:08 +0200 Subject: [PATCH 3/6] Fixed a typo, ordered elements into a grid --- devices/linux/Files/main.py | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/devices/linux/Files/main.py b/devices/linux/Files/main.py index 9e2c9f10b..aa8008cec 100644 --- a/devices/linux/Files/main.py +++ b/devices/linux/Files/main.py @@ -442,7 +442,7 @@ def prompt_nonempty_string(self, show: int, prompt: str, val: str = '') -> str: elif self.graphics == 'tkinter': from tkinter import simpledialog simpledialog.askstring("Input", "Enter a string:", - initialvalue=val + initialvalue=val, show="*" if show == 0 else "") else: command = [] @@ -496,25 +496,28 @@ def __get_username_password_atomic(self) -> None: import tkinter as tk root = tk.Tk() - root.title("Login Prompt") + root.title(Config.title) +# desc_label = tk.Label(root, text=Messages.credentials_prompt) +# desc_label.grid(row=0, column=0, columnspan=2, sticky=tk.W) + username_label = tk.Label(root, text=Messages.username_prompt) - username_label.pack() + username_label.grid(row=1, column=0, sticky=tk.W) username_entry = tk.Entry(root, textvariable=tk.StringVar(root, value=self.username)) - username_entry.pack() + username_entry.grid(row=1, column=1) password_label = tk.Label(root, text=Messages.enter_password) - password_label.pack() + password_label.grid(row=2, column=0, sticky=tk.W) password_entry = tk.Entry(root, show="*") - password_entry.pack() + password_entry.grid(row=2, column=1) password1_label = tk.Label(root, text=Messages.repeat_password) - password1_label.pack() + password1_label.grid(row=3, column=0, sticky=tk.W) password1_entry = tk.Entry(root, show="*") - password1_entry.pack() + password1_entry.grid(row=3, column=1) def submit(installer): def inner(): @@ -524,7 +527,7 @@ def inner(): return inner login_button = tk.Button(root, text="Login", command=submit(self)) - login_button.pack() + login_button.grid(row=4, column=0, columnspan=2) root.mainloop() else: From 9b6a0ac0fea485c9e4c9c1c6346d9c0875c57d23 Mon Sep 17 00:00:00 2001 From: sdasda7777 <17746796+sdasda7777@users.noreply.github.com> Date: Fri, 21 Jun 2024 08:03:45 +0200 Subject: [PATCH 4/6] `showinfo` -> `showwarning` in `alert` --- devices/linux/Files/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/devices/linux/Files/main.py b/devices/linux/Files/main.py index aa8008cec..327ba756a 100644 --- a/devices/linux/Files/main.py +++ b/devices/linux/Files/main.py @@ -411,7 +411,7 @@ def alert(self, text: str) -> None: print(text) elif self.graphics == 'tkinter': from tkinter import messagebox - messagebox.showinfo(Config.title, text) + messagebox.showwarning(Config.title, text) else: if self.graphics == 'zenity': command = ['zenity', '--warning', '--text=' + text] From 4d1a8de0a1fd0947fc6cf1a7bfda6c00378504c6 Mon Sep 17 00:00:00 2001 From: sdasda7777 <17746796+sdasda7777@users.noreply.github.com> Date: Fri, 21 Jun 2024 08:21:28 +0200 Subject: [PATCH 5/6] Removed forgotten strings --- devices/linux/Files/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/devices/linux/Files/main.py b/devices/linux/Files/main.py index 327ba756a..9501b82f6 100644 --- a/devices/linux/Files/main.py +++ b/devices/linux/Files/main.py @@ -441,7 +441,7 @@ def prompt_nonempty_string(self, show: int, prompt: str, val: str = '') -> str: return output elif self.graphics == 'tkinter': from tkinter import simpledialog - simpledialog.askstring("Input", "Enter a string:", + simpledialog.askstring(Config.title, prompt, initialvalue=val, show="*" if show == 0 else "") else: From 305d6100aa82e825622fd0967b2105c053c0c1fa Mon Sep 17 00:00:00 2001 From: sdasda7777 <17746796+sdasda7777@users.noreply.github.com> Date: Fri, 21 Jun 2024 12:28:41 +0200 Subject: [PATCH 6/6] Added ok string --- devices/linux/Files/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/devices/linux/Files/main.py b/devices/linux/Files/main.py index 9501b82f6..3ef80e7c4 100644 --- a/devices/linux/Files/main.py +++ b/devices/linux/Files/main.py @@ -229,6 +229,7 @@ class Messages: cert_error = "Certificate file not found, looks like a CAT error" unknown_version = "Unknown version" dbus_error = "DBus connection problem, a sudo might help" + ok = "OK" yes = "Y" nay = "N" p12_filter = "personal certificate file (p12 or pfx)" @@ -526,7 +527,7 @@ def inner(): root.destroy() return inner - login_button = tk.Button(root, text="Login", command=submit(self)) + login_button = tk.Button(root, text=Messages.ok, command=submit(self)) login_button.grid(row=4, column=0, columnspan=2) root.mainloop()