【问题标题】:Python 3 /Tkinter: changing the font type, size, and color of a pre-defined labelPython 3 /Tkinter:更改预定义标签的字体类型、大小和颜色
【发布时间】:2018-09-02 16:52:14
【问题描述】:

我将 Tkinter 与 Python 3.6 (Windows 10) 一起使用。我在画布上有一个框架,框架包含一个标签。标签属性(即字体类型、颜色、大小)将由用户在使用 GUI 期间从下拉菜单中选择。起始帧及其标签为:

    self.defaultLeftStringValue                     = StringVar()       
    self.defaultRightStringValue                    = StringVar()
    self.leftFrame                                  = tf.TextInFrame(self.main_container, bg = 'white', 
                                                                     height = 128, width = 128)
    self.leftFrame.place( x = 10, y = 10)
    self.leftFrame.pack_propagate(False)
    self.leftLabel                                  = Label(self.leftFrame, textvariable=self.defaultLeftStringValue).pack()

后来,用户用另一种方法(doEnter:)选择了字体类型、大小和颜色

def doEnter(self):

    thisFontSize                                    = self.tkvarFontSize.get()
    thisFontColor                                   = self.tkvarFontFgColor.get()
    thisFontType                                    = self.tkvarFontType.get()
    self.leftFrame.config(bg=self.tkvarFontBgColor.get())
    self.rightFrame.config(bg=self.tkvarFontBgColor.get())
    font                                            = Font( family = thisFontType, size = thisFontSize )
    splitText                                       = self.text_entry.get()

    self.splitTextLeft                              = " "
    self.splitTextRight                             = " "
    if ";" in splitText:
        splitText                                   = splitText.split(";")
        self.splitTextLeft                          = splitText[0]
        self.splitTextRight                         = splitText[1]
        splitTextLeftLength                         = font.measure(self.splitTextLeft)
        splitTextRightLength                        = font.measure(self.splitTextRight)
        splitTextHeight                             = font.metrics("linespace")
        self.defaultLeftStringValue.set(self.splitTextLeft)
        self.defaultRightStringValue.set(self.splitTextRight)

这一切都很好;输入的文本出现在左右框架的标签中。我什至可以更改框架的背景颜色。但是,我无法弄清楚如何使用 thisFontSize(值 = 10,type = class int)和 thisFontColor(黑色,class str)和 thisFontType(agencyfb,class str)来更改标签属性。希望有人能告诉我如何更改插入到self.splitTextLeft中的文本的属性等。

【问题讨论】:

  • 你试过self.leftLabel.config(bg="black")self.leftLabel.config(font=("agencyfb", 10))吗?

标签: python tkinter fonts label frame


【解决方案1】:

这一行将None 分配给self.leftLabel

self.leftLabel = Label(self.leftFrame, textvariable=self.defaultLeftStringValue).pack()

您不能链接几何管理方法并保留对小部件的引用。分别执行这些步骤。在您的情况下,您没有引用标签实例,因此以后无法设置其属性。

举例说明:

x = tk.Label(root, text="ok").pack()
type(x)
<class 'NoneType'>
x = tk.Label(root, text="ok")
type(x)
<class 'tkinter.Label'>

【讨论】:

  • 我明白了。我没有意识到使用终止小部件方法(在这种情况下为 .pack() )会导致方法的不可逆转丢失。谢谢。
  • 只是给一位评论者的评论:只有 P3 的历史,而从来没有 P2 的历史,是否有理由期望仅 P3 的用户会知道这样的问题应该由 P3 授权?跨度>
  • @BenjaminLevy:我认为 OP(实际上是你)提到 P3 作为他的环境并不需要 P3 标签。使用P3引起的问题没有问题。这是一个 python tkinter 问题。根据“如果您的问题不是特定于版本的,请使用更通用的 [python] 标签。”所以我觉得标题很好不用提。
猜你喜欢
  • 2017-11-02
  • 2017-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-19
相关资源
最近更新 更多