简体   繁体   中英

Calling back fonts in Tkinter

I'm working on a notepad program in tkinter, and I want to add fonts, so far I have gotten fonts to work fine, but I come into a problem when I go to save. My program does not remember fonts upon closing (obviously). Is there any way to get the font that is being used on the text pad, and save it to the first line of the file so that it can be remembered when opening the note again.

Here is my code for the font menu:

fontmenu=Menu(menubar,tearoff=0)
fontmenu.add_command(label="Times New Roman", command=timesnewroman)
fontmenu.add_command(label="Arial", command=arial)
fontmenu.add_command(label="Georgia", command=georgia)
fontmenu.add_command(label="Trebuchet MS", command=trebuchet)
fontmenu.add_command(label="Verdana", command=verdana)
fontmenu.add_command(label="MS Sans Serif", command=mssans)
fontmenu.add_separator()
fontmenu.add_command(label="Comic Sans MS", command=comicsansms)
fontmenu.add_command(label="Courier New", command=couriernew)
fontmenu.add_command(label="Impact", command=impact)
fontmenu.add_command(label="Lucida Console", command=lucidaconsole)
fontmenu.add_command(label="Lucida Sans Unicode", command=lucidaunicode)
fontmenu.add_command(label="Plalatino Linotype", command=palatino)
fontmenu.add_command(label="Tahoma", command=tahoma)
fontmenu.add_command(label="Symbol", command=symbol)
fontmenu.add_command(label="Webdings", command=webdings)
fontmenu.add_command(label="Wingdings", command=wingdings)
fontmenu.add_command(label="MS Serif", command=msserif)
menubar.add_cascade(label="Fonts", menu=fontmenu)

And here are my many functions that change the font:

#functions font---
def timesnewroman():
    text.config(font="{Times New Roman} 12")
def arial():
    text.config(font="{Arial} 12")
def comicsansms():
    text.config(font="{Comic Sans MS} 12")
def couriernew():
    text.config(font="{Courier New} 12")
def georgia():
    text.config(font="{Georgia} 12")
def impact():
    text.config(font="{Impact} 12")
def lucidaconsole():
    text.config(font="{Lucida Console} 12")
def lucidaunicode():
    text.config(font="{Lucida Sans Unicode} 12")
def palatino():
    text.config(font="{Palatino Linotype} 12")
def tahoma():
    text.config(font="{Tahoma} 12")
def trebuchet():
    text.config(font="{Trebuchet} 12")
def verdana():
    text.config(font="{Verdana} 12")
def symbol():
    text.config(font="{Symbol} 12")
def webdings():
    text.config(font="{Webdings} 12")
def wingdings():
    text.config(font="{Wingdings} 12")
def mssans():
    text.config(font="{MS Sans} 12")
def msserif():
    text.config(font="{MS Serif} 12")

Is there an obvious command to recall the font and store it as a variable? My eventual goal is to have the font stored in the file like {{'font name'}} and a new line where the note starts. Alse, how would I search a .txt file in order to find {{'font name'}} and use it as a variable?

I was thinking of something along the lines of:

def open():
    f = tkFileDialog.askopenfile(parent=root, mode='rb', title='Select file')
    contents = str(f.read())
    fonttype = #here is where I would look for {{'font name'}} and store it as a variable
    fonttype = fonttype.replace("{", "").replace("}", "") #the replace command leaves the variable as {'font name'}
    text.delete(1.0, Tkinter.END)
    text.insert('1.0', contents)
    text.config(font=fonttype)
    f.close()

EDIT: Please disregard the first part of the question, global variables solved my problem, but now I am still having troubles when reading the file and finding the text that is in the {{ }}.

If you make sure that you are always knowing the {{ }} line and can easily use:

contents= file.readlines()
font = content[0].strip().replace('{{', '').replace('}}', '')

So the variable font is your font only if you know the {{ }} line num. 0 is the line number here.

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