整个文本并非作为字符串的长度增加,在TKINTER CANVAS PYTHON中增加

发布于 2025-01-25 06:06:20 字数 1568 浏览 2 评论 0原文

我是Python的GUI的新手。我一直在尝试在画布上显示一些文本。但是,只有它的最后一部分出现,而其余部分则隐藏在画布的边界之外。 我的代码如下:

首先:画布和滚动条定义:

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()

第二:create_text的代码部分,

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

我试图更改<<代码> create_text 函数多次。但是,如果它适用于一个字符串,则不适用于另一个字符串。同样,它仅用于一个非常简单的一行字符串,但是我从未成功地将其调整为更复杂的诸如我的代码中发生的更复杂的字符串。

另一个问题(我认为这可能是相关的) 滚动条无法正常运行。当垂直方向始终处于活动状态时,水平从不活跃。

抱歉,这么长时间。 如果有人能告诉我怎么了,我会很高兴。

请先感谢

PS:我的代码的目的是显示一个单词及其定义,其定义可能需要几行。一开始我使用了标签。但是,它们永远不符合很长的定义,也不适合滚动条。因此,我去了我发现大多数程序员建议哪个是画布的其他选项。所以,请原谅我,但我对此有些一无所知。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久随 2025-02-01 06:06:20

多亏了我们的同事Billy,

我改用了文本小部件,一切都按照我想要的方式修复了。

文字小部件更适合我的要求(阅读我的问题以了解我的要求)。

“您是否尝试过使用TKINTER的文字“小部件?我认为它更适合您的需求。您可以为文本小部件设置包装并更改其状态,以使其不允许编辑。”

- &nbsp; Billy

Thanks to our colleague Billy,

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

The text widget suited my requirement better (read my question to understand my requirement).

"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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文