android 小部件字体,如何在不知道小部件的字体系列/大小的情况下更改小部件的字体样式?...

有一种比使用.config()更改应用程序字体更好的方法,特别是当您的目标是更改整个小部件组(或所有小部件)的字体时。

Tk真正伟大的特性之一是“命名字体”的概念。命名字体的优点是,如果您更新字体,所有使用该字体的小部件都将自动更新。因此,只需将小部件配置为使用这些自定义字体,然后更改属性就很简单了。

下面是一个简单的例子:try:

import Tkinter as tk

import tkFont

# import ttk # not used here

except ImportError: # Python 3

import tkinter as tk

import tkinter.font as tkFont

# import tkinter.ttk as ttk # not used here

class App:

def __init__(self):

root=tk.Tk()

# create a custom font

self.customFont = tkFont.Font(family="Helvetica", size=12)

# create a couple widgets that use that font

buttonframe = tk.Frame()

label = tk.Label(root, text="Hello, world", font=self.customFont)

text = tk.Text(root, width=20, height=2, font=self.customFont)

buttonframe.pack(side="top", fill="x")

label.pack()

text.pack()

text.insert("end","press +/- buttons to change\nfont size")

# create buttons to adjust the font

bigger = tk.Button(root, text="+", command=self.OnBigger)

smaller = tk.Button(root, text="-", command=self.OnSmaller)

bigger.pack(in_=buttonframe, side="left")

smaller.pack(in_=buttonframe, side="left")

root.mainloop()

def OnBigger(self):

'''Make the font 2 points bigger'''

size = self.customFont['size']

self.customFont.configure(size=size+2)

def OnSmaller(self):

'''Make the font 2 points smaller'''

size = self.customFont['size']

self.customFont.configure(size=size-2)

app=App()

如果您不喜欢这种方法,或者如果您希望将自定义字体基于默认字体,或者您只是更改一个或两个字体来表示状态,则可以使用font.actual来获取给定小部件的实际字体大小。例如:import Tkinter as tk

import tkFont

root = tk.Tk()

label = tk.Label(root, text="Hello, world")

font = tkFont.Font(font=label['font'])

print font.actual()

当我运行上面的程序时,我得到以下输出:{'family': 'Lucida Grande',

'weight': 'normal',

'slant': 'roman',

'overstrike': False,

'underline': False,

'size': 13}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值