python换行符号长度_创建带换行符的可调整大小/多行Tkinter/ttk标签

是否可以使用自动换行创建多行标签,该标签的大小与其父级宽度同步?换句话说,当您更改记事本窗口的宽度时,记事本的换字行为。

用例是一个对话框,它需要完整地呈现多行文本(指令)块,而无需剪裁文本或求助于滚动条。父容器将有足够的垂直空间来容纳窄宽度。

我一直在尝试Tkinter标签和消息小部件以及ttk标签小部件,但没有成功。似乎我需要硬编码一个像素wrapeningth值,而当这些控件的文本到达容器的右边缘时,它们会自动换行。当然,Tkinters几何管理器可以帮助我自动调整标签的大小并相应地更新它们的wrapength值?

我应该改为查看文本小部件吗?如果是,是否可以隐藏文本小部件的边框,以便我可以将其用作带换行符的多行标签?from Tkinter import *

master = Tk()

text = """

If tkinter is 8.5 or above you'll want the selection background to appear like it does when the widget is activated. Comment this out for older versions of Tkinter.

This is even more text.

The final line of our auto-wrapping label that supports clipboard copy.

""".strip()

frameLabel = Frame( master, padx=20, pady=20 )

frameLabel.pack()

w = Text( frameLabel, wrap='word', font='Arial 12 italic' )

w.insert( 1.0, text )

w.pack()

# - have selection background appear like it does when the widget is activated (Tkinter 8.5+)

# - have label background color match its parent background color via .cget('bg')

# - set relief='flat' to hide Text control borders

# - set state='disabled' to block changes to text (while still allowing selection/clipboard copy)

w.configure( bg=master.cget('bg'), relief='flat', state='disabled' )

mainloop()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值