-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomponent_builder.py
More file actions
77 lines (57 loc) · 2.38 KB
/
component_builder.py
File metadata and controls
77 lines (57 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import tkinter as tk
import tkinter.ttk as ttk
from tkinter import PhotoImage, Text
from tkinter.constants import CENTER, END, LEFT, TOP
from paths import get_image_path
class BasicText(Text):
def __init__(self, parent: tk.Misc, initial_text: str):
super().__init__(parent)
self.insert(END, initial_text)
def get_text(self) -> str:
return self.get("1.0", END).strip()
class ReadOnlyText(BasicText):
def __init__(self, parent: tk.Misc, initial_text: str):
super().__init__(parent, initial_text)
self.config(state='disabled')
def set_text(self, text: str) -> None:
self.config(state='normal')
self.delete('1.0', END)
self.insert(END, text)
self.config(state='disabled')
class BasicEntry(ttk.Entry):
def __init__(self, parent: tk.Misc, initial_text: str, is_secret: bool = False):
super().__init__(parent, show="●" if is_secret else "")
self.insert(END, initial_text)
def get_text(self) -> str:
return self.get().strip()
def build_button(parent: tk.Misc, image_name: str, command: ...) -> ttk.Button:
image = build_image(image_name)
btn = ttk.Button(parent, image=image, compound=LEFT, command=command)
btn.img = image # I @$%^ing hate having to write this hack
return btn
def build_option_row(parent: tk.Misc, option_name: str, initial_value: str, is_secret: bool = False) -> BasicEntry:
row = ttk.Frame(parent)
name = ttk.Label(row, text=option_name, anchor='center', width=20)
value = BasicEntry(row, initial_value, is_secret)
name.pack(side=LEFT, expand=False)
value.pack(side=LEFT, expand=True, fill='x')
row.pack(side=TOP, expand=False, fill='x', padx=2, pady=2)
return value
def build_tabs(parent: ttk.Notebook, names: list[str]) -> list[ttk.Frame]:
tabs = []
for name in names:
tab = ttk.Frame(parent)
parent.add(tab, text=name, padding=(10, 10))
tabs.append(tab)
return tabs
def build_image(name: str) -> PhotoImage:
return PhotoImage(file=str(get_image_path(name)))
def build_horizontal_centered_frame(parent: tk.Misc) -> ttk.Frame:
outer = ttk.Frame(parent)
outer.pack(side=TOP, expand=True, fill='x')
outer.columnconfigure(0, weight=1)
outer.columnconfigure(1, weight=1)
outer.columnconfigure(2, weight=1)
inner = ttk.Frame(outer)
inner.grid(row=0, column=1)
return inner