From 453ee0b7170f9a89af775864a59e0bee98853ef4 Mon Sep 17 00:00:00 2001 From: BrunoV21 Date: Fri, 25 Oct 2024 22:18:10 +0100 Subject: [PATCH 1/4] added support for windows resizing --- chlorophyll/codeview.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/chlorophyll/codeview.py b/chlorophyll/codeview.py index 1c54c4c..93089db 100644 --- a/chlorophyll/codeview.py +++ b/chlorophyll/codeview.py @@ -45,6 +45,7 @@ def __init__( lexer: LexerType = pygments.lexers.TextLexer, color_scheme: dict[str, dict[str, str | int]] | str | None = None, tab_width: int = 4, + font_size :int=11, linenums_theme: Callable[[], tuple[str, str]] | tuple[str, str] | None = None, autohide_scrollbar: bool = True, linenums_border: int = 0, @@ -56,7 +57,7 @@ def __init__( self._frame.grid_columnconfigure(1, weight=1) kwargs.setdefault("wrap", "none") - kwargs.setdefault("font", ("monospace", 11)) + kwargs.setdefault("font", ("monospace", font_size)) linenum_justify = kwargs.pop("justify", "left") @@ -333,3 +334,9 @@ def vertical_scroll(self, first: str | float, last: str | float) -> CodeView: def scroll_line_update(self, event: Event | None = None) -> CodeView: self.horizontal_scroll(*self.xview()) self.vertical_scroll(*self.yview()) + + def resize_window(self, width: int, height: int) -> None: + """Resize the CodeView widget based on the specified width and height in pixels.""" + self._frame.config(width=width, height=height) + self.config(width=width, height=height) + self._line_numbers.redraw() # Update line numbers after resizing \ No newline at end of file From b68336e956b238ef3f7ddbef30f610e1e67190e5 Mon Sep 17 00:00:00 2001 From: BrunoV21 Date: Fri, 25 Oct 2024 22:18:21 +0100 Subject: [PATCH 2/4] updated readme with windows resizing example --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/README.md b/README.md index ea10640..65b56da 100644 --- a/README.md +++ b/README.md @@ -47,3 +47,23 @@ codeview.pack(fill="both", expand=True) root.mainloop() ``` + +#### Resizing CodeView Window +You can dynamically resize the CodeView window by using the resize_window method: + +```python +from tkinter import Tk +import pygments.lexers +from chlorophyll import CodeView + +root = Tk() + +# Create a CodeView instance with a Python lexer +codeview = CodeView(root, lexer=pygments.lexers.PythonLexer, color_scheme="monokai") +codeview.pack(fill="both", expand=True) + +# Resize the CodeView widget to a specific width and height +codeview.resize_window(width=800, height=600) + +root.mainloop() +``` \ No newline at end of file From 6f23e089b50e6a80819783a48a9acb43c5448810 Mon Sep 17 00:00:00 2001 From: BrunoV21 Date: Sat, 23 Nov 2024 22:46:03 +0000 Subject: [PATCH 3/4] added support to hide line_numbers --- chlorophyll/codeview.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/chlorophyll/codeview.py b/chlorophyll/codeview.py index 93089db..fa27372 100644 --- a/chlorophyll/codeview.py +++ b/chlorophyll/codeview.py @@ -50,12 +50,15 @@ def __init__( autohide_scrollbar: bool = True, linenums_border: int = 0, default_context_menu: bool = False, + show_line_numbers: bool = True, **kwargs, ) -> None: self._frame = ttk.Frame(master) self._frame.grid_rowconfigure(0, weight=1) self._frame.grid_columnconfigure(1, weight=1) + self.show_line_numbers = show_line_numbers + kwargs.setdefault("wrap", "none") kwargs.setdefault("font", ("monospace", font_size)) @@ -64,19 +67,21 @@ def __init__( super().__init__(self._frame, **kwargs) super().grid(row=0, column=1, sticky="nswe") - self._line_numbers = TkLineNumbers( - self._frame, - self, - justify=linenum_justify, - colors=linenums_theme, - borderwidth=kwargs.get("borderwidth", linenums_border), - ) + self._line_numbers = None # Initialize with None + if self.show_line_numbers: # Conditionally create line numbers + self._line_numbers = TkLineNumbers( + self._frame, + self, + justify=linenum_justify, + colors=linenums_theme, + borderwidth=kwargs.get("borderwidth", linenums_border), + ) + self._line_numbers.grid(row=0, column=0, sticky="ns") self._vs = Scrollbar(self._frame, autohide=autohide_scrollbar, orient="vertical", command=self.yview) self._hs = Scrollbar( self._frame, autohide=autohide_scrollbar, orient="horizontal", command=self.xview ) - self._line_numbers.grid(row=0, column=0, sticky="ns") self._vs.grid(row=0, column=2, sticky="ns") self._hs.grid(row=1, column=1, sticky="we") @@ -98,7 +103,7 @@ def __init__( super().bind(f"<{contmand}-a>", self._select_all, add=True) super().bind(f"<{contmand}-Shift-Z>", self.redo, add=True) super().bind("<>", self.scroll_line_update, add=True) - super().bind("", self._line_numbers.redraw, add=True) + super().bind("", self._line_numbers.redraw, add=True) if self.show_line_numbers else ... self._orig = f"{self._w}_widget" self.tk.call("rename", self._w, self._orig) @@ -107,7 +112,7 @@ def __init__( self._set_lexer(lexer) self._set_color_scheme(color_scheme) - self._line_numbers.redraw() + self._line_numbers.redraw() if self.show_line_numbers else ... @property def context_menu(self) -> Menu: @@ -329,7 +334,7 @@ def horizontal_scroll(self, first: str | float, last: str | float) -> CodeView: def vertical_scroll(self, first: str | float, last: str | float) -> CodeView: self._vs.set(first, last) - self._line_numbers.redraw() + self._line_numbers.redraw() if self.show_line_numbers else ... def scroll_line_update(self, event: Event | None = None) -> CodeView: self.horizontal_scroll(*self.xview()) @@ -339,4 +344,8 @@ def resize_window(self, width: int, height: int) -> None: """Resize the CodeView widget based on the specified width and height in pixels.""" self._frame.config(width=width, height=height) self.config(width=width, height=height) - self._line_numbers.redraw() # Update line numbers after resizing \ No newline at end of file + self._line_numbers.redraw() if self.show_line_numbers else ... # Update line numbers after resizing + + def set_line_spacing(self, spacing: int) -> None: + """Set the vertical spacing between lines.""" + self.configure(spacing3=spacing) \ No newline at end of file From 8c4cb0a7f77cbc3aad9aa618040df7fb2a4c6df0 Mon Sep 17 00:00:00 2001 From: BrunoV21 Date: Sun, 24 Nov 2024 00:41:34 +0000 Subject: [PATCH 4/4] added support to hide horizontal / vertical scrollers --- chlorophyll/codeview.py | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/chlorophyll/codeview.py b/chlorophyll/codeview.py index fa27372..a1c63ec 100644 --- a/chlorophyll/codeview.py +++ b/chlorophyll/codeview.py @@ -51,6 +51,8 @@ def __init__( linenums_border: int = 0, default_context_menu: bool = False, show_line_numbers: bool = True, + show_vertical_scrollbar: bool = True, + show_horizontal_scrollbar: bool=True, **kwargs, ) -> None: self._frame = ttk.Frame(master) @@ -58,6 +60,8 @@ def __init__( self._frame.grid_columnconfigure(1, weight=1) self.show_line_numbers = show_line_numbers + self.show_vertical_scrollbar = show_vertical_scrollbar + self.show_horizontal_scrollbar = show_horizontal_scrollbar kwargs.setdefault("wrap", "none") kwargs.setdefault("font", ("monospace", font_size)) @@ -77,13 +81,20 @@ def __init__( borderwidth=kwargs.get("borderwidth", linenums_border), ) self._line_numbers.grid(row=0, column=0, sticky="ns") - self._vs = Scrollbar(self._frame, autohide=autohide_scrollbar, orient="vertical", command=self.yview) - self._hs = Scrollbar( - self._frame, autohide=autohide_scrollbar, orient="horizontal", command=self.xview - ) - - self._vs.grid(row=0, column=2, sticky="ns") - self._hs.grid(row=1, column=1, sticky="we") + + self._vs = None # Initialize with None + if self.show_vertical_scrollbar: # Conditionally create vertical scrollbar + self._vs = Scrollbar(self._frame, autohide=autohide_scrollbar, orient="vertical", command=self.yview) + self._vs.grid(row=0, column=2, sticky="ns") + self._vs = Scrollbar(self._frame, autohide=autohide_scrollbar, orient="vertical", command=self.yview) + self._vs.grid(row=0, column=2, sticky="ns") + + self._hs = None + if self.show_horizontal_scrollbar: + self._hs = Scrollbar( + self._frame, autohide=autohide_scrollbar, orient="horizontal", command=self.xview + ) + self._hs.grid(row=1, column=1, sticky="we") super().configure( yscrollcommand=self.vertical_scroll, @@ -330,10 +341,10 @@ def destroy(self) -> None: BaseWidget.destroy(self._frame) def horizontal_scroll(self, first: str | float, last: str | float) -> CodeView: - self._hs.set(first, last) + self._hs.set(first, last) if self.show_horizontal_scrollbar else ... def vertical_scroll(self, first: str | float, last: str | float) -> CodeView: - self._vs.set(first, last) + self._vs.set(first, last) if self.show_vertical_scrollbar else ... self._line_numbers.redraw() if self.show_line_numbers else ... def scroll_line_update(self, event: Event | None = None) -> CodeView: