added a scrollbar and made the text widget resizable

This commit is contained in:
Faraphel 2022-07-29 21:01:52 +02:00
parent 1c64f5288d
commit 51a6e46930

View file

@ -10,26 +10,33 @@ ModConfig: any
class Window(tkinter.Toplevel): class Window(tkinter.Toplevel):
def __init__(self, mod_config: "ModConfig"): def __init__(self, mod_config: "ModConfig"):
super().__init__() super().__init__()
self.grid_rowconfigure(2, weight=1)
self.grid_columnconfigure(1, weight=1)
self.mod_config = mod_config self.mod_config = mod_config
self.entry_format_input = ttk.Entry(self, width=100) self.entry_format_input = ttk.Entry(self, width=100)
self.entry_format_input.grid(row=1, column=1, sticky="NEWS") self.entry_format_input.grid(row=1, column=1, columnspan=2, sticky="NEWS")
self.entry_format_input.bind("<Return>", self.preview) self.entry_format_input.bind("<Return>", self.preview)
self.track_preview = tkinter.Text(self, background="black", foreground=MKWColor("off").color_code) self.text_track_preview = tkinter.Text(self, background="black", foreground=MKWColor("off").color_code)
self.track_preview.grid(row=2, column=1, sticky="NEWS") self.text_track_preview.grid(row=2, column=1, sticky="NEWS")
self.scrollbar_track_preview = ttk.Scrollbar(self, command=self.text_track_preview.yview)
self.scrollbar_track_preview.grid(row=2, column=2, sticky="NEWS")
self.text_track_preview.configure(yscrollcommand=self.scrollbar_track_preview.set)
for color in MKWColor.get_all_colors(): for color in MKWColor.get_all_colors():
self.track_preview.tag_configure(color.bmg, foreground=color.color_code) self.text_track_preview.tag_configure(color.bmg, foreground=color.color_code)
self.track_preview.tag_configure("error", background="red", foreground="white") self.text_track_preview.tag_configure("error", background="red", foreground="white")
def preview(self, event: tkinter.Event = None): def preview(self, event: tkinter.Event = None):
""" """
Preview all the tracks name with the track format Preview all the tracks name with the track format
:return: :return:
""" """
self.track_preview.delete(1.0, tkinter.END) self.text_track_preview.delete(1.0, tkinter.END)
# insert all the tracks representation # insert all the tracks representation
for track in self.mod_config.get_tracks(): for track in self.mod_config.get_tracks():
@ -53,14 +60,14 @@ class Window(tkinter.Toplevel):
return "" # remove the tag return "" # remove the tag
# insert into the text the track_repr without the tags # insert into the text the track_repr without the tags
self.track_preview.insert( self.text_track_preview.insert(
tkinter.END, tkinter.END,
re.sub(r"\\c{(?P<color_name>.*?)}", tag_format, track_repr) + "\n" re.sub(r"\\c{(?P<color_name>.*?)}", tag_format, track_repr) + "\n"
) )
# color every part of the track_repr with the position and color got in the re.sub # color every part of the track_repr with the position and color got in the re.sub
for (pos_start, tag_start), (pos_end, tag_end) in zip(tags, tags[1:] + [(None, None)]): for (pos_start, tag_start), (pos_end, tag_end) in zip(tags, tags[1:] + [(None, None)]):
self.track_preview.tag_add( self.text_track_preview.tag_add(
tag_start, tag_start,
f"end-1c-1l+{pos_start}c", f"end-1c-1l+{pos_start}c",
"end-1c" + (f"-1l+{pos_end}c" if pos_end is not None else "") "end-1c" + (f"-1l+{pos_end}c" if pos_end is not None else "")
@ -68,5 +75,5 @@ class Window(tkinter.Toplevel):
except Exception as exc: except Exception as exc:
formatted_exc = str(exc).replace('\n', ' ') formatted_exc = str(exc).replace('\n', ' ')
self.track_preview.insert(tkinter.END, f"< Error: {formatted_exc} >\n") self.text_track_preview.insert(tkinter.END, f"< Error: {formatted_exc} >\n")
self.track_preview.tag_add("error", "end-1c-1l", "end-1c") self.text_track_preview.tag_add("error", "end-1c-1l", "end-1c")