简体   繁体   中英

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. 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. 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. Assuming your font settings consist only of a size parameter (eg 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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