简体   繁体   English

在Python中更改文本小部件字体

[英]Change Text Widget font in Python

I'm making a function that will increase the text widget's size by 1 every time it's called. 我正在制作一个函数,它将在每次调用时将文本小部件的大小增加1。 I cannot find a way to find the current font size of the widget. 我找不到找到小部件当前字体大小的方法。 I need something like: 我需要类似的东西:

textEntry.configure(font=(fontSize=fontSize+1))

If you call .config() on a widget without any parameters, it returns a dictionary containing the current configuration. 如果在没有任何参数的窗口小部件上调用.config() ,它将返回包含当前配置的字典。 So, textEntry.config() would get you a dictionary for the textEntry widget, and textEntry.config()['font'] would get you a tuple of values relating to your font settings. 因此, textEntry.config()将为您提供textEntry小部件的字典,而textEntry.config()['font']将为您提供与字体设置有关的值的元组。 Assuming your font settings consist only of a size parameter (eg font=10 ) 假设您的font设置包含一个size参数(例如font=10

curSize = int(textEntry.config()['font'][-1])

would get you an integer containing the current font size 会得到一个包含当前字体大小的整数

This is a quick and dirty solution but it works for all fonts and can contain any valid font parameter(s). 这是一种快速而肮脏的解决方案,但是它适用于所有字体,并且可以包含任何有效的字体参数。

def increaseSize():
    font = textEntry.cget('font')       #get font information
    info = font.split(' ')              #split it into chunks

    #find the font size entry
    for i in info:
        if i.isdigit():
            size = int(i)
            textEntry.config(font=font.replace(i, str(size + 1)))
            break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM