TkLineNums icon indicating copy to clipboard operation
TkLineNums copied to clipboard

Adding tilde character parameter

Open lorenzolpandolfo opened this issue 1 year ago • 32 comments

With my contribution, user can add a tilde character parameter to the TkLineNums widget. If user doesn't send this parameter, TkLineNums will work normally, without a tilde character. example: self.line_counter = TkLineNumbers(master, self, justify="right", colors=("#e3ba68", "#1D1E1E"),tilde="~", bd=0)

Example of tilde character in Vim text editor: intro-vim-unix-text-editor-every-hacker-should-be-familiar-with w1456 In this case, "~" is the tilde character, and it represents a non-existent line.

In my code, there is a visual bug that I could not find a fix. If user have a wrapped line (a big line that don't fit in the text widget resolution, so It "breaks" into 2+ visible lines), the tilde algorithm will try to add a tilde character in that counter position, causing a visual bug.

example: image

this tilde character overlapping with the "7" in the line counter was created because of the wrapped line space.

This bug only happen if: image

If we fix this visual problem, tilde characters will work flawlessly! =)

lorenzolpandolfo avatar Jan 18 '24 16:01 lorenzolpandolfo

I am getting issues like this: Screenshot 2024-01-18 at 12 11 23 PM

Moosems avatar Jan 18 '24 17:01 Moosems

I am getting issues like this: Screenshot 2024-01-18 at 12 11 23 PM

this error happens because the font size was not correctly treated in the tilde algorithm. If you use font="monospace 11" or font=tk.Font(family="Consolas", size=25), this error persists?

lorenzolpandolfo avatar Jan 18 '24 17:01 lorenzolpandolfo

No but I made a PR with changes that worked for me. Something about this PR also makes the <<Modified>> event not work, something to look into eventually.

Moosems avatar Jan 18 '24 22:01 Moosems

No but I made a PR with changes that worked for me. Something about this PR also makes the <<Modified>> event not work, something to look into eventually.

Exactly bro! I don't know why, but the line counter only updates when i click on it in this example, so I need to use <KeyRelease> event. Also, I was trying to fix font problems and I got some weird results. I will write them for you soon.

lorenzolpandolfo avatar Jan 18 '24 22:01 lorenzolpandolfo

I think maybe there is a easier way to make this works. Also, I don't understand why the code works really well in Pytext but doesn't in his __name__ == "__main__" example!

image

image

In Pytext, when we run type(self.textwidget.cget("font")), it returns <class 'customtkinter.windows.widgets.font.ctk_font.CTkFont'>, and not str (I'm not using tkinter, but customtkinter).

lorenzolpandolfo avatar Jan 18 '24 22:01 lorenzolpandolfo

Is there any CTK interop going on?

Moosems avatar Jan 19 '24 01:01 Moosems

Is there any CTK interop going on?

No, I just used a CTkFont instead of tk.Font. I tryied to import CTK and use CTkFont instead of tk.Font in your example, but same error. Tomorrow I will analyze more deeper

lorenzolpandolfo avatar Jan 19 '24 01:01 lorenzolpandolfo

Hello, @Moosems ! How are you? I hope you're doing great! I think I found a fix for these font problems and, here in my computer, both tkinter and customtkinter are working flawlessly! I added a example for each library. Check the "better perfomance" commit that I created and test it. :)

lorenzolpandolfo avatar Jan 19 '24 13:01 lorenzolpandolfo

Lets not add ctk to the main file, please. The plan and hope is to use no external libraries.

Moosems avatar Jan 19 '24 20:01 Moosems

Lets not add ctk to the main file, please. The plan and hope is to use no external libraries.

no problem. I will adapt code for only using tkinter Font class

lorenzolpandolfo avatar Jan 19 '24 20:01 lorenzolpandolfo

@Moosems Also, how the line_elided works? It always show False to me, even if there is wrapped lines.

lorenzolpandolfo avatar Jan 19 '24 20:01 lorenzolpandolfo

line_elided isn't for wrapped lines. You can hide lines in tkinter so it handles that. Wrapped lines are handled differently :).

Moosems avatar Jan 19 '24 22:01 Moosems

@Moosems I removed customtkinter from TkLineNums, but, consequently, using customtkinter with a tuple type font, as: font = ("Consolas", 20), will result in font linespaces inconsistencies, so I decided to use a raise TypeError in this case, telling the user to use a CTkFont instance instead, like this: font = ctk.CTkFont("Consolas", 20). In this case, won't have font linespace inconsistencies. This TypeError only happens if user is using customtkinter to create his GUI and necessarily using a tuple type font, so it will be very rare and in a specific situation that can be easily avoided.

lorenzolpandolfo avatar Jan 19 '24 23:01 lorenzolpandolfo

So if I understand it right, this or would require me to use a Font object and wouldn't allow a tuple?

Moosems avatar Jan 19 '24 23:01 Moosems

So if I understand it right, this or would require me to use a Font object and wouldn't allow a tuple?

If you're using customtkinter, yes, you will need a CTkFont instance. Otherwise, like using only tkinter, It will work normally with tuples, tk.Font() and str, as "Consolas 20".

lorenzolpandolfo avatar Jan 19 '24 23:01 lorenzolpandolfo

But not font=("Courier New Bold", 15)?

Moosems avatar Jan 19 '24 23:01 Moosems

But not font=("Courier New Bold", 15)?

In customtkinter no, it should be font=CTkFont("Courier New Bold", 15). In tkinter it will work.

lorenzolpandolfo avatar Jan 19 '24 23:01 lorenzolpandolfo

and, of course, this limitation only happens if user is using a tilde char, otherwise it won't make any difference.

lorenzolpandolfo avatar Jan 19 '24 23:01 lorenzolpandolfo

Sounds good to me! Personally ctk support is a secondary priority so I'm fine with letting things like that go :).

Moosems avatar Jan 19 '24 23:01 Moosems

@lorenzolpandolfo Take a look at this ;)

Moosems avatar Jan 19 '24 23:01 Moosems

Sounds good to me! Personally ctk support is a secondary priority so I'm fine with letting things like that go :).

All right! Of course, with more time I can analyze more about this fonts inconsistencies, but, at time, in a situation that can be easily avoided (just using a font instance of ctk), i think its the best thing to do at time.

For some context, using tk.Font(family=tuple[0], size=tuple[1]) (if user is using CTK) will result in linespace inconsistencies. I think that tkinter and customtkinter deal with different forms the linespaces and sizes.

lorenzolpandolfo avatar Jan 19 '24 23:01 lorenzolpandolfo

@lorenzolpandolfo Take a look at this ;)

Thank you! Before I found your library, I was creating my own line counter for my text editor. But it was not dealing correctly with wrapped lines. Even researching in a lot of places, I could not to make it work. 😅 I will read It with time later!

lorenzolpandolfo avatar Jan 19 '24 23:01 lorenzolpandolfo

Also, @Moosems I was thinking here, this PR is to your main branch. I think we should change that to a unstable branch or something, to make things more safer

lorenzolpandolfo avatar Jan 20 '24 00:01 lorenzolpandolfo

A dev branch?

Moosems avatar Jan 20 '24 00:01 Moosems

A dev branch?

yes, or a tilde-unstable, or something like this. So people can test it and it won't possibly break projects that use this repository on main branch.

lorenzolpandolfo avatar Jan 20 '24 00:01 lorenzolpandolfo

Hello, @Moosems ! I fixed the tilde char being added to wrapped lines and now everything is working correctly. example: image

Sadly, CTK support is no longer available, because we need the count() method to check wrapped lines, which is a tk.Text method only, and it is not existent in the CTkTextbox class. I opened a issue in customtkinter to see if there is a way to use the .count method in a CTkTextbox instance, since customtkinter was made on top of tkinter.

A way to go through this problem is to, in a customtkinter project, the textwidget must be a tkinter Text widget. Unless I find a way to use .count in CTkTextbox, of course!

lorenzolpandolfo avatar Jan 20 '24 14:01 lorenzolpandolfo

Sounds great! If you could run ruff, black, and isort I'll make sure to take a look at it later today and make any final changes :).

Moosems avatar Jan 20 '24 14:01 Moosems

Sounds great! If you could run ruff, black, and isort I'll make sure to take a look at it later today and make any final changes :).

I used ruff and fixed all observations, then black and isort. With my changes, the "line_elided" variable was not being used, so I commented it. Try the Arial to TkFixedFont commit and tell me what do you think! :)

Also, if user does not provide a font to the textwidget, it will be "TkFixedFont". But, I don't think there is a way to access this font size, since self.textwidget.cget("font") returns a str TkFixedFont, without any size. So, if user would like to use the tilde char but is using TkFixedFont, the tilde char size will be 10. Here, in my computer, it looks ok, but maybe it can cause font sizes inconsistencies if the TkFixedFont size is not 10. But this is a very specific problem, I think that most people won't use TkFixedFont, since it is just a placeholder font.

lorenzolpandolfo avatar Jan 20 '24 16:01 lorenzolpandolfo

@Moosems really good news: CTkTextbox support added! =)

lorenzolpandolfo avatar Jan 20 '24 21:01 lorenzolpandolfo

hello, @Moosems ! When you have time, check my new commit =)

lorenzolpandolfo avatar Jan 22 '24 16:01 lorenzolpandolfo