使用tkinter wraplength函数时出错

2024-10-03 04:32:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我想要标签自动换行内的输出。我看到很多人使用函数wrap length,但当我这样使用它时

label_out.place(relx=0, rely=0.05, relwidth=1, relheight=0.95, wraplengt=200) 

它表明

TclError: unknown option "-wraplength"

我知道我现在哪里做错了,我纠正我的代码到这个

    final_text = ' '.join([lemmatizer.lemmatize(w) for w in stemmed])
    label_out = Label(root,font="helvetica 14", wraplength=300,justify="center")
    label_out['text'] = final_text

现在我又犯了一个错误

AttributeError: 'xml.etree.ElementTree.Element' object has no attribute 'tk'

Tags: 函数textplace标签outlengthlabelfinal
2条回答

正如错误所说Label.place不支持wraplength选项wraplength用于小部件。您可以将代码更改为以下内容

label_out= Label(root, text="Your long text", font="helvetica 14",wraplength=300, justify="center")

我只是找到了解决办法。起初,我认为wraplength应该放在决赛中,但我错了(不是完全错),它不应该放在这样的位置

label_out = tk.Label(lower_frame, bg='white')
label_out.place(relx=0, rely=0.05, relwidth=1, relheight=0.95, wraplength=500)

相反,它应该这样说

label_out = tk.Label(lower_frame, bg='white', wraplength=500)
label_out.place(relx=0, rely=0.05, relwidth=1, relheight=0.95)

大部分答案都用一句话来表达。事实上,它可以这样写

label_out['text'] = final_text

label_out = tk.Label(lower_frame, bg='white', wraplength=500)
label_out.place(relx=0, rely=0.05, relwidth=1, relheight=0.95)

使用这种方法,您(我)可以将有关UI的所有内容放在代码的末尾,只在代码的上部留下变量

相关问题 更多 >