Home > Back-end >  Whole text doesn't appear as length of string increases in Tkinter Canvas Python
Whole text doesn't appear as length of string increases in Tkinter Canvas Python

Time:05-05

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.

CodePudding user response:

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

  • Related