Whole text doesn’t appear as length of string increases in Tkinter Canvas Python

I’m new to Python’s GUI. I’ve been trying to display some text onto a Canvas. However, only the last part of it appears, while the rest is hidden beyond the borders of the canvas.
My code is as follow:

first: the canvas, and the scrollbars definition:

window = Tk() #instantiate a window
window.geometry("650x520")
window.title("Practice")
window.config(background="white")
frame = Frame(window, width=600, height=400)
canvas = Canvas(frame, bg='white', width=500, height=300, scrollregion=(0,0,500,500))
hbar = Scrollbar(frame, orient=HORIZONTAL)
vbar = Scrollbar(frame, orient=VERTICAL)
prompt = Label(window, text="Enter the word you'd like to find its definition:", bg="white", font="5")
entry = Entry(window, font="5")
submit = Button(window, text="Search!", font="5", command=lambda: search_gui(words,entry.get().lower(),canvas))
prompt.pack()
entry.pack()
submit.pack()
frame.pack(expand=True, fill=BOTH)
hbar.pack(side=BOTTOM, fill=X)
hbar.config(command=canvas.xview)
vbar.pack(side=RIGHT, fill=Y)
vbar.config(command=canvas.yview)
canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set)
canvas.pack(side=LEFT, expand=True, fill=BOTH)
window.mainloop()

second: the code portion where the create_text is called

#string is a text of several lines (includes new line characters)
disp.create_text(10,10 text= string)

I’ve tried to change the values of the x and y parameters of the create_text function several times. But if it worked for one string, it doesn’t work for the other. Also, it has only worked for a very simple one line string but I’ve never succeeded in adjusting it for more complex ones like the ones occurring in my code.

Another issue (I think it might be related)
the scroll bars aren’t functioning correctly. The horizontal one is never active while the vertical one is always active.

Sorry for being that long.
I’d be so grteful if anybody could tell me what’s wrong.

Thanks in advance

PS: the purpose of my code is to display a word and its defintion, where its definition might take several lines. I’ve used labels at first. However, they never fit the very long definitions and don’t take scrollbars. So, I’ve went to the other option I’ve found most of programmers suggesting which is the canvas. So, pardon me but I’m a little bit ignorant about it.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Thanks to our colleague Billy,

I’ve used the text widget instead and everything got fixed up the way I wanted.

“Have you tried using tkinter’s Text widget? I think it suits your needs better. You can set wrap for the text widget and change its state so that it will not allow editing.”

– Billy


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x