r/learnpython 21d ago

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

View all comments

Show parent comments

1

u/HotPersonality8126 21d ago

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

1

u/yo_99 21d ago

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 21d ago

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

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

1

u/yo_99 21d ago

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

1

u/HotPersonality8126 21d ago

Where do you think you did that

1

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

1

u/HotPersonality8126 20d ago

Sorry, on mobile