简体   繁体   English

Tkinter,enclosing_frame.grid_propogate(0)以防止调整大小也阻止更新标签文本

[英]Tkinter, enclosing_frame.grid_propogate(0) to prevent resizing also prevents updating label text

This is pretty simple. 这很简单。

I have a label along the bottom of my app. 我的应用程序底部有一个标签。 As a user scrolls through a treeview, the content of that label changes. 当用户滚动浏览树状视图时,该标签的内容会更改。 Sometimes, the content is multiple lines long . 有时,内容长多行

Previously, the label would grow/shsrink to accommodate content. 以前,标签会增加/缩小以容纳内容。 I didn't like this, so I wrapped the label in a frame, gave said frame a fixed height, and set grid_propagate(0) . 我不喜欢这样,所以我将标签包裹在框架中,为所述框架设置了固定高度,并设置了grid_propagate(0) It looks great, but now my text has disappeared. 看起来不错,但现在我的文字消失了。

How can I give the label a fixed size and retain the ability to update its text? 如何给标签定大小并保留更新其文本的功能?

You can give your label a fixed width 您可以给标签固定width

lbl1 = Label(root, text = "Stuff....", width = 10)
lbl1.configure(text = "Another longer stuff....")

if you give it a fixed width value, it won't expand automatically. 如果您给它一个固定的width值,它将不会自动扩展。

Update 更新

Here's what I've tried and worked. 这就是我尝试过的工作。

from tkinter import *
root = Tk()
canv = Canvas(root,height=17)
canv.grid_propagate(0)
lbl1 = Label(canv,text="First line\nSecond Line")
lbl1.grid(column=0,row=0)
canv.grid(column=0,row=0)
root.mainloop()

I wrapped the label in a canvas instead of a frame and it worked (only the text "First line" appears). 我将标签包裹在画布中而不是框架中,并且可以工作(仅显示“第一行”文本)。 Play around with the height value of the canvas. 试一下画布的height值。

Rather then a Label use a Text inside the Frame and add a vertical scrollbar with the Text. 相反,Label在框架内使用文本,并在文本上添加垂直滚动条。 Pack both the Text and the Scrollbar in the Frame, using examples online, and connect them up. 在线使用示例,将“文本”和“滚动条”打包到框架中,然后将它们连接起来。 Once you cancel propagate for the Frame it won't resize. 一旦您取消了框架的传播,它就不会调整大小。 You can then set the Frame to whatever size you want, even set the Text for a specific number of lines and columns of text before you cancel propagate for the Frame, and then you can display as much text as you want at the bottom allowing the user to scroll through if it is too big. 然后,您可以将框架设置为任意大小,甚至在取消框架的传播之前,甚至可以为文本的特定行数和列数设置文本,然后可以在底部显示任意数量的文本,从而允许如果太大,用户可以滚动浏览。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM