63 lines
2.2 KiB
Python
63 lines
2.2 KiB
Python
import tkinter as tk
|
|
from tkinter import ttk, messagebox
|
|
|
|
class PasswordDialog(tk.Toplevel):
|
|
def __init__(self, parent, title="Password Required", confirm=True):
|
|
super().__init__(parent)
|
|
self.title(title)
|
|
self.parent = parent
|
|
self.password = None
|
|
self.save_to_keyring = tk.BooleanVar()
|
|
self.confirm = confirm
|
|
|
|
self.transient(parent)
|
|
self.grab_set()
|
|
|
|
ttk.Label(self, text="Please enter the password for the encrypted backup:").pack(padx=20, pady=10)
|
|
self.password_entry = ttk.Entry(self, show="*")
|
|
self.password_entry.pack(padx=20, pady=5, fill="x", expand=True)
|
|
self.password_entry.focus_set()
|
|
|
|
if self.confirm:
|
|
ttk.Label(self, text="Confirm password:").pack(padx=20, pady=10)
|
|
self.confirm_entry = ttk.Entry(self, show="*")
|
|
self.confirm_entry.pack(padx=20, pady=5, fill="x", expand=True)
|
|
|
|
self.save_to_keyring_cb = ttk.Checkbutton(self, text="Save password to system keyring", variable=self.save_to_keyring)
|
|
self.save_to_keyring_cb.pack(padx=20, pady=10)
|
|
|
|
button_frame = ttk.Frame(self)
|
|
button_frame.pack(pady=10)
|
|
|
|
ok_button = ttk.Button(button_frame, text="OK", command=self.on_ok)
|
|
ok_button.pack(side="left", padx=5)
|
|
cancel_button = ttk.Button(button_frame, text="Cancel", command=self.on_cancel)
|
|
cancel_button.pack(side="left", padx=5)
|
|
|
|
self.bind("<Return>", lambda event: self.on_ok())
|
|
self.bind("<Escape>", lambda event: self.on_cancel())
|
|
|
|
self.wait_window(self)
|
|
|
|
def on_ok(self):
|
|
password = self.password_entry.get()
|
|
|
|
if not password:
|
|
messagebox.showerror("Error", "Password cannot be empty.", parent=self)
|
|
return
|
|
|
|
if self.confirm:
|
|
confirm = self.confirm_entry.get()
|
|
if password != confirm:
|
|
messagebox.showerror("Error", "Passwords do not match.", parent=self)
|
|
return
|
|
|
|
self.password = password
|
|
self.destroy()
|
|
|
|
def on_cancel(self):
|
|
self.password = None
|
|
self.destroy()
|
|
|
|
def get_password(self):
|
|
return self.password, self.save_to_keyring.get() |