【发布时间】: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