在 Tkinter 标签文本的末尾显示三个点 [英] Display three dots in the end of a Tkinter Label text

查看:66
本文介绍了在 Tkinter 标签文本的末尾显示三个点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在 CSS 的 text-overflow 属性中显示三个点,比如省略号?

Is there any way to show the three dots like ellipsis in CSS's text-overflow property?

这是一个示例标签:

Label(root, text = "This is some very long text!").pack()

另一个有宽度属性的:

Label(root, text = "This is some very long text!", width = 15).pack()

推荐答案

没有内置方式,但您可以轻松创建自己的方式:

No builtin way, but you could easily make your own:

import tkinter as tk

class AyoubLabel(tk.Label):
    '''A type of Label that adds an ellipsis to overlong text'''
    def __init__(self, master=None, text=None, width=None, **kwargs):
        if text and width and len(text) > width:
            text = text[:width-3] + '...'
        tk.Label.__init__(self, master, text=text, width=width, **kwargs)

现在只需使用 AyoubLabel 而不是 Label.

Now just use AyoubLabel instead of Label.

在制作标签或使用文本变量后更新标签不会对此做出反应,但如果需要,您可以添加这些功能.

This does not react to updating the Label after making it or using a textvariable, but you could add those abilities if you need them.

这篇关于在 Tkinter 标签文本的末尾显示三个点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆