2021-03-14 17:45:02 +03:00
|
|
|
import tkinter
|
|
|
|
import time
|
|
|
|
|
2022-05-02 00:29:14 +03:00
|
|
|
from ..widgets.ctk_label import CTkLabel
|
|
|
|
from ..widgets.ctk_entry import CTkEntry
|
|
|
|
from ..widgets.ctk_frame import CTkFrame
|
|
|
|
from ..windows.ctk_toplevel import CTkToplevel
|
|
|
|
from ..widgets.ctk_button import CTkButton
|
2022-04-09 22:45:57 +03:00
|
|
|
from ..appearance_mode_tracker import AppearanceModeTracker
|
2022-05-17 22:44:59 +03:00
|
|
|
from ..theme_manager import ThemeManager
|
2021-03-14 17:45:02 +03:00
|
|
|
|
|
|
|
|
2022-03-08 01:00:50 +03:00
|
|
|
class CTkInputDialog:
|
2021-03-14 17:45:02 +03:00
|
|
|
def __init__(self,
|
|
|
|
master=None,
|
|
|
|
title="CTkDialog",
|
|
|
|
text="CTkDialog",
|
2022-02-23 00:38:40 +03:00
|
|
|
fg_color="default_theme",
|
2022-02-28 02:35:52 +03:00
|
|
|
hover_color="default_theme",
|
|
|
|
border_color="default_theme"):
|
2022-03-02 02:53:58 +03:00
|
|
|
|
|
|
|
self.appearance_mode = AppearanceModeTracker.get_mode() # 0: "Light" 1: "Dark"
|
2021-03-14 17:45:02 +03:00
|
|
|
self.master = master
|
|
|
|
|
|
|
|
self.user_input = None
|
|
|
|
self.running = False
|
|
|
|
|
|
|
|
self.height = len(text.split("\n"))*20 + 150
|
2021-08-25 18:02:16 +03:00
|
|
|
|
2022-05-22 02:55:58 +03:00
|
|
|
self.text = text
|
2022-05-17 22:44:59 +03:00
|
|
|
self.window_bg_color = ThemeManager.theme["color"]["window_bg_color"]
|
|
|
|
self.fg_color = ThemeManager.theme["color"]["button"] if fg_color == "default_theme" else fg_color
|
|
|
|
self.hover_color = ThemeManager.theme["color"]["button_hover"] if hover_color == "default_theme" else hover_color
|
|
|
|
self.border_color = ThemeManager.theme["color"]["button_hover"] if border_color == "default_theme" else border_color
|
2021-03-14 17:45:02 +03:00
|
|
|
|
2022-03-02 02:53:58 +03:00
|
|
|
self.top = CTkToplevel()
|
2022-05-02 00:29:14 +03:00
|
|
|
self.top.geometry(f"{280}x{self.height}")
|
2022-05-22 02:55:58 +03:00
|
|
|
self.top.minsize(280, self.height)
|
|
|
|
self.top.maxsize(280, self.height)
|
2021-03-14 17:45:02 +03:00
|
|
|
self.top.title(title)
|
2022-01-07 22:54:45 +03:00
|
|
|
self.top.lift()
|
|
|
|
self.top.focus_force()
|
|
|
|
self.top.grab_set()
|
2021-03-14 17:45:02 +03:00
|
|
|
|
2022-07-25 12:25:48 +03:00
|
|
|
self.top.protocol("WM_DELETE_WINDOW", self.on_closing)
|
|
|
|
|
2022-05-22 02:55:58 +03:00
|
|
|
self.top.after(10, self.create_widgets) # create widgets with slight delay, to avoid white flickering of background
|
|
|
|
|
|
|
|
def create_widgets(self):
|
2022-03-02 02:53:58 +03:00
|
|
|
self.label_frame = CTkFrame(master=self.top,
|
2022-03-05 15:05:39 +03:00
|
|
|
corner_radius=0,
|
|
|
|
fg_color=self.window_bg_color,
|
|
|
|
width=300,
|
|
|
|
height=self.height-100)
|
2021-03-14 17:45:02 +03:00
|
|
|
self.label_frame.place(relx=0.5, rely=0, anchor=tkinter.N)
|
|
|
|
|
2022-03-02 02:53:58 +03:00
|
|
|
self.button_and_entry_frame = CTkFrame(master=self.top,
|
2022-03-05 15:05:39 +03:00
|
|
|
corner_radius=0,
|
|
|
|
fg_color=self.window_bg_color,
|
|
|
|
width=300,
|
|
|
|
height=100)
|
2021-03-14 17:45:02 +03:00
|
|
|
self.button_and_entry_frame.place(relx=0.5, rely=1, anchor=tkinter.S)
|
|
|
|
|
|
|
|
self.myLabel = CTkLabel(master=self.label_frame,
|
2022-05-22 02:55:58 +03:00
|
|
|
text=self.text,
|
2021-03-14 17:45:02 +03:00
|
|
|
width=300,
|
2022-02-21 17:48:59 +03:00
|
|
|
fg_color=None,
|
2021-03-14 17:45:02 +03:00
|
|
|
height=self.height-100)
|
|
|
|
self.myLabel.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
|
|
|
|
|
|
|
|
self.entry = CTkEntry(master=self.button_and_entry_frame,
|
2022-01-07 22:54:45 +03:00
|
|
|
width=230)
|
2021-03-14 17:45:02 +03:00
|
|
|
self.entry.place(relx=0.5, rely=0.15, anchor=tkinter.CENTER)
|
|
|
|
|
|
|
|
self.ok_button = CTkButton(master=self.button_and_entry_frame,
|
|
|
|
text='Ok',
|
2022-01-07 22:54:45 +03:00
|
|
|
width=100,
|
2021-03-14 17:45:02 +03:00
|
|
|
command=self.ok_event,
|
|
|
|
fg_color=self.fg_color,
|
2022-02-28 02:35:52 +03:00
|
|
|
hover_color=self.hover_color,
|
|
|
|
border_color=self.border_color)
|
2022-01-07 22:54:45 +03:00
|
|
|
self.ok_button.place(relx=0.28, rely=0.65, anchor=tkinter.CENTER)
|
2021-03-14 17:45:02 +03:00
|
|
|
|
|
|
|
self.cancel_button = CTkButton(master=self.button_and_entry_frame,
|
|
|
|
text='Cancel',
|
2022-01-07 22:54:45 +03:00
|
|
|
width=100,
|
2021-03-14 17:45:02 +03:00
|
|
|
command=self.cancel_event,
|
|
|
|
fg_color=self.fg_color,
|
2022-02-28 02:35:52 +03:00
|
|
|
hover_color=self.hover_color,
|
|
|
|
border_color=self.border_color)
|
2022-01-07 22:54:45 +03:00
|
|
|
self.cancel_button.place(relx=0.72, rely=0.65, anchor=tkinter.CENTER)
|
2021-03-14 17:45:02 +03:00
|
|
|
|
2022-02-21 17:48:59 +03:00
|
|
|
self.entry.entry.focus_force()
|
|
|
|
self.entry.bind("<Return>", self.ok_event)
|
|
|
|
|
|
|
|
def ok_event(self, event=None):
|
2021-03-14 17:45:02 +03:00
|
|
|
self.user_input = self.entry.get()
|
|
|
|
self.running = False
|
|
|
|
|
2022-07-25 12:25:48 +03:00
|
|
|
def on_closing(self):
|
|
|
|
self.running = False
|
|
|
|
|
2021-03-14 17:45:02 +03:00
|
|
|
def cancel_event(self):
|
|
|
|
self.running = False
|
|
|
|
|
|
|
|
def get_input(self):
|
|
|
|
self.running = True
|
|
|
|
|
|
|
|
while self.running:
|
2022-06-30 16:53:32 +03:00
|
|
|
try:
|
|
|
|
self.top.update()
|
|
|
|
except Exception:
|
|
|
|
return self.user_input
|
|
|
|
finally:
|
|
|
|
time.sleep(0.01)
|
2021-03-14 17:45:02 +03:00
|
|
|
|
|
|
|
time.sleep(0.05)
|
|
|
|
self.top.destroy()
|
|
|
|
return self.user_input
|