Skip to content

Commit

Permalink
Merge pull request #8 from Moosems/click-drag-bugs
Browse files Browse the repository at this point in the history
Fix drag issues
  • Loading branch information
Moosems committed Jul 12, 2023
2 parents f7d9a1c + 075ff3f commit 9fe06de
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 164 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ dist/
tklinenums.egg-info/
tklinenums/__pycache__/
.DS_Store
.ruff_cache/
34 changes: 12 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,36 @@
- Clicking on line numbers will set the insert to the beginning of the line.
- Shift clicking will select all text from the end of the line clicked by cursor and the insert position.
- Scrolling the linebar will scroll the text widget (and vice versa).
- Supports ttk themes (by usage of the `.set_to_ttk_style()` method)
- Supports left, right, and center alignment with the `-justify` option
- Clicking and then dragging the mouse will scroll the text widget (see [#8](https://github.com/Moosems/TkLineNums/pull/8))
- Can handle elided lines (elided lines are lines that are not visible in the text widget).
- Supports ttk themes and allows easy color customization.
- Supports left, right, and center alignment with the `-justify` option.
- Clicking and then dragging the mouse will scroll the text widget (see [#8](https://github.com/Moosems/TkLineNums/pull/8)).

# Installation
`pip install tklinenums`

In the Command Line, paste the following: `pip install tklinenums`.

## Documentation
### `TkLineNums` Widget
|Options|Description|Type|
|---|---|---|
|master|The parent widget|Tkinter widget (defaults to `tkinter.Misc`)|
|editor|The `Text` widget the line numbers will connect to|Tkinter `Text` widget (or child class)|
|textwidget|The `Text` widget the line numbers will connect to|Tkinter `Text` widget (or child class)|
|justify|The alignment of the line numbers|A string as either `"left"`, `"right"`, or `"center"`|
|colors|A way to provide coloring to the line numbers|A function, `(foreground, background)` tuple, or None. None (default) makes it use the `Text` widget's coloring. The function should return a `(foreground, background)` tuple: it will be called whenever the colors are needed, and it is useful when the colors can change.|
|*args|Arguments for the `Canvas` widget|Any arguments used for the `Canvas` widget|
|**kwargs|Keyword arguments for the `Canvas` widget|Any keyword arguments used for the `Canvas` widget|

### Basic Usage:
```python
from platform import system
from tkinter import Text, Tk
from tkinter.ttk import Style

from tklinenums import TkLineNumbers

# This is to make the example work on both Windows and Mac
if system() == "Darwin":
contmand: str = "Command"
else:
contmand: str = "Control"

# Create the root window
root = Tk()

# Set the ttk style (tkinter's way of styling) for the line numbers
style = Style()
style.configure("TLineNumbers", background="#ffffff", foreground="#2197db")

# Create the Text widget and pack it to the right
text = Text(root)
text.pack(side="right")
Expand All @@ -60,18 +52,16 @@ for i in range(50):
text.insert("end", f"Line {i+1}\n")

# Create the TkLineNumbers widget and pack it to the left
linenums = TkLineNumbers(root, text)
linenums = TkLineNumbers(root, text, justify="center", colors=("#2197db", "#ffffff"))
linenums.pack(fill="y", side="left")

# Create binds to redraw the line numbers for most common events that change the text in the Text widget
text.bind("<Key>", lambda event: root.after_idle(linenums.redraw), add=True)
text.bind("<BackSpace>", lambda event: root.after_idle(linenums.redraw), add=True)
text.bind(f"<{contmand}-v>", lambda event: root.after_idle(linenums.redraw), add=True)
# Redraw the line numbers when the text widget contents are modified
text.bind("<<Modified>>", lambda event: root.after_idle(linenums.redraw), add=True)

# Start the mainloop for the root window
root.mainloop()
```
For a more complete example, see the [example.py](./tests/example.py) file.
For a more complete example, see [example.py](./tests/example.py).

## How to run and contribute

Expand Down
7 changes: 5 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
[tool.black]
line-length = 110
line-length = 88

[tool.isort]
line_length = 100
line_length = 88
profile = "black"
multi_line_output = 3

[tool.ruff]
line-length = 125
37 changes: 20 additions & 17 deletions tests/example.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,37 @@
from platform import system
from tkinter import Text, Tk
from tkinter.font import Font
from tkinter.ttk import Style

from tklinenums import TkLineNumbers

if system() == "Darwin":
contmand: str = "Command"
else:
contmand: str = "Control"
# Create the root window
root = Tk()

root: Tk = Tk()
# Set the ttk style (tkinter's way of styling) for the line numbers
style = Style()
style.configure("TkLineNumbers", foreground="#2197db", background="#ffffff")

style: Style = Style()
style.configure("TLineNumbers", background="#ffffff", foreground="#2197db")

font: Font = Font(family="Courier New bold", size=15, name="TkLineNumsFont")
# Create a style_provider function that returns the ttk style for the line numbers
def ttk_style_provider():
fg: str = style.lookup("TkLineNumbers", "foreground", default="black")
bg: str = style.lookup("TkLineNumbers", "background", default="white")
return (fg, bg)

text: Text = Text(root, wrap="char", font=font)

# Create the Text widget and pack it to the right
text = Text(root)
text.pack(side="right")

# Insert 50 lines of text into the Text widget
for i in range(50):
text.insert("end", f"Line {i+1}\n")

linenums: TkLineNumbers = TkLineNumbers(root, text, font, justify="left")
linenums.pack(fill="y", side="left", expand=True)
linenums.reload(font)
# Create the TkLineNumbers widget and pack it to the left
linenums = TkLineNumbers(root, text, justify="center", colors=ttk_style_provider)
linenums.pack(fill="y", side="left")

text.bind("<Key>", lambda event: root.after_idle(linenums.redraw), add=True)
text.bind(f"<BackSpace>", lambda event: root.after_idle(linenums.redraw), add=True)
text.bind(f"<{contmand}-v>", lambda event: root.after_idle(linenums.redraw), add=True)
# Redraw the line numbers when the text widget contents are modified
text.bind("<<Modified>>", lambda _: root.after_idle(linenums.redraw), add=True)

# Start the mainloop for the root window
root.mainloop()
2 changes: 1 addition & 1 deletion tklinenums/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from .tklinenums import TkLineNumbers
from .tklinenums import TkLineNumbers # noqa: F401
Loading

0 comments on commit 9fe06de

Please sign in to comment.