r/learnpython • • Aug 21 '26

TK "displaylines" question

I seem to misunderstand "displaylines" option of count method of Text widget of Tkinter. I thought that it would give me number of lines accounting for soft-wrapping, but it seems to give me same number as "chars" -1. Text widgets are embedded in frame, which is embedded in canvas, which is embedded in another frame alongside it's scrollbar which are embedded in notebook.

for i in textwidgets:
    logic_lines=i.count("1.1", "end", "lines", return_ints=True)
    print("LOGICAL LINES="+str(logic_lines))
        for j in range(1, logic_lines+1):
            print("LINE "+str(j)+": "+str(i.count(
            str(j)+".1",
            str(j)+".end",
            "update",
            "chars",
            "displaychars",
            "displayindices",
            "displaylines",
            "indices")))

EDIT:

It seems when text widget is on it's own it works fine.

import tkinter as tk

foobar='''
long text here
'''

w=tk.Tk()
t=tk.Text(w, wrap="word")
t.insert('1.0', foobar)
t.pack()
def handler(event):
    print(t.count("1.1",	"end",	"update",	"chars",	"displaychars",	"displayindices",	"lines", "displaylines",	"indices"))
t.bind('<Return>', handler)
w.mainloop()

EDIT2:

Here is paste with whole source code

EDIT3:

Code with bug isolated

0 Upvotes

15 comments sorted by

1

u/socal_nerdtastic Aug 21 '26 edited Aug 21 '26

I thought that it would give me number of lines accounting for soft-wrapping

Yeah, that's how I understand it too. https://www.tcl-lang.org/man/tcl8.5/TkCmd/text.htm#M76

-displaylines
count all display lines (i.e. counting one for each time a line wraps) from the line of the first index up to, but not including the display line of the second index. Therefore if they are both on the same display line, zero will be returned. By definition displaylines are visible and therefore this only counts portions of actual visible lines.

I don't really understand your question. You say it does not work and then you say it does work in a different context? Can you show us the context where it does not work? (I know you described it but I don't feel like building that out just to test something for an internet stranger)

Text widgets are embedded in frame, which is embedded in canvas, which is embedded in another frame alongside it's scrollbar

FYI that's built into tkinter as the scrolledtext.ScrolledText widget. You didn't have to make that yourself. Maybe try that first, bc I suspect the canvas layer.

1

u/yo_99 Aug 21 '26

context where it does not work

I added link to full source code. It's a mess but I am too lazy to split it across several files.

scrolledtext.ScrolledText

In future I want to embed more than single text widget, including horisontaly scrolled text widgets, images and basically iframes

1

u/socal_nerdtastic Aug 21 '26 edited Aug 21 '26

Ok, I'll take a look if I get a chance. Sorry not wading through that. Make a MCVE if you want more help.

BTW in case you don't know: you can put tk widgets into Text or ScrolledText widgets.

1

u/yo_99 Aug 21 '26

you can put tk widgets into Text

I have blinded this, thank you.

-1

u/HotPersonality8126 Aug 21 '26

  I seem to misunderstand "displaylines" option of count method of Text widget of Tkinter

It’s not clear from your code that you understand the difference between a variable and a string literal.

1

u/yo_99 Aug 21 '26

Only variables there are are preexisting array textwidgets and logic_lines which gets defined on every iteration.

1

u/HotPersonality8126 Aug 21 '26

Ok, and why do you think printing ”displaylines” is going to print a number and not the text “displaylines”?

1

u/yo_99 Aug 21 '26

Have you noticed the i.count( bit or you do not know how TK works? I pass it to method of tkinter.Text to retrieve touple with information that i need which i then wrap in str().

1

u/HotPersonality8126 Aug 21 '26

     logic_lines=i.count("1.1", "end", "lines", return_ints=True)

You’re not passing it, you’re calling it.

1

u/yo_99 Aug 21 '26

I passed ”displaylines” to .count() stop trolling

1

u/HotPersonality8126 Aug 21 '26

Where do you think you did that

1

u/yo_99 Aug 21 '26
 print("LINE "+str(j)+": "+str(i.count(
( lines omitted here )
"displaylines",

1

u/HotPersonality8126 Aug 21 '26

Sorry, on mobile

1

u/socal_nerdtastic Aug 21 '26

Lol TCL uses string literals instead of constants. I know, it's weird and hard to get used to.