【问题标题】:How to put multiple fonts in a widget如何在一个小部件中放置多种字体
【发布时间】:2017-04-09 15:44:45
【问题描述】:

如何将多种字体放入一个小部件中?

我发现这可能有效。

b=Button(frame, text="Big bold text\n" "Small text")

我尝试了很多方法,但我的代码无法正常工作。 简而言之,我想要一个大粗体文本和一个小文本在彼此下方。

感谢您的帮助和建议。

【问题讨论】:

  • 我不相信标准按钮小部件支持这一点。您需要创建自己的按钮小部件。
  • Button 不能这样做。您可以使用Frame 和许多具有不同字体的Labels 创建自己的小部件。
  • “我发现这可能有效。” - 你是从哪里发现的?那肯定行不通。

标签: python user-interface text fonts tkinter


【解决方案1】:

Tk 按钮小部件和 ttk 按钮小部件都不支持对文本元素使用多种字体。但是,您可以使用 Canvas 小部件构建一些内容,以在单个小部件中获取两个单独的文本元素。 This answer 关于制作垂直文本按钮提供了一个类似的示例,这将是一个很好的起点。

【讨论】:

    【解决方案2】:

    我知道这个线程在撰写本文时已经很老了,但想要这个确切的问题,但用 Label 代替。要解决这个问题,请尝试以下代码(希望它的使用非常容易理解):

    from tkinter import *
    from tkinter.font import *
    
    def example_method (): print ("Click!")
    
    class Button2:
    
        """
    Allows multiple fonts in a very simple button.
    Only supports 'master', 'text' and 'command' keywords
        ('master' is compulsory)
    Fonts are delared in < > with the following options:
        BOLD = Make the text bold
        ITALIC = Make the text italic
        STRIKEOUT = Strike through the text
        UNDERLINE = Underlines the text
        an integer = the text size
        any other keyword is assumed to be the text family
        For the default text style, leave the < > empty
    NOTE: Only supports the grid method due to internal handelling
        """
    
        def __init__ (self, master, text = "<>", command = None):
    
            self.f, self.command = Frame (root, relief = RAISED, borderwidth = 3), command
            self.f.bind ("<Button-1>", lambda event: self.__click ())
            self.f.bind ("<ButtonRelease-1>", lambda event: self.__release ())
    
            sections = [i.split (">") for i in text.split ("<") [1 : ]]
    
            row, column = 0, 0
            for section in sections:
    
                font_decomp, kw = section [0].split ("_"), {}
                for keyword in font_decomp:
                    if keyword == "STRIKEOUT": kw ["overstrike"] = True
                    elif keyword == "BOLD": kw ["weight"] = BOLD
                    elif keyword == "ITALIC": kw ["slant"] = ITALIC
                    elif keyword == "UNDERLINE": kw ["underline"] = True
                    try: kw ["size"] = int (keyword)
                    except: kw ["family"] = keyword
    
                temp_font = Font (**kw)
                l = Label (self.f, text = section [1].replace ("\n", ""), font = temp_font)
                l.grid (row = row, column = column)
                l.bind ("<Button-1>", lambda event: self.__click ())
                l.bind ("<ButtonRelease-1>", lambda event: self.__release ())
    
                if section [1].count ("\n") >= 1:
                    column = -1
                    for i in range (section [1].count ("\n") - 1):
                        l = Label (self.f, text = "", font = temp_font)
                        l.grid (row = row, column = column)
                        l.bind ("<Button-1>", lambda event: self.__click ())
                        l.bind ("<ButtonRelease-1>", lambda event: self.__release ())
                        row += 1
                    row += 1
                column += 1
    
        def __click (self):
            self.f.config (relief = SUNKEN)
            if self.command: self.command ()
    
        def __release (self): self.f.config (relief = RAISED)
    
        def grid (self, **kw): self.f.grid (**kw)
    
    root = Tk ()
    root.title ("Multiple fonts")
    Button2 (root, text = "<30_BOLD>Big bold text\n<10>Small text", command = example_method).grid ()
    root.mainloop ()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-12
      • 2011-03-23
      • 2020-07-16
      • 1970-01-01
      • 2011-01-18
      • 2011-01-27
      • 2011-06-10
      • 2020-09-19
      相关资源
      最近更新 更多