Python Tkinter文本编辑器不会将字体保存到文本文件 [英] Python Tkinter text editor does not save font to text file

查看:83
本文介绍了Python Tkinter文本编辑器不会将字体保存到文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在使用python和tkinter开发GUI文本编辑器.感谢SO的杰出人士(感谢Rinzler),我设法修改了文本的字体.但是,我无法将字体和字体大小保存到 txt 文件中.我知道这样做应该可行,因为记事本可以修改并保存具有指定字体的 txt 文件.

Currently, I am working on a GUI text editor with python and tkinter. Thanks to the great people at SO (thank you Rinzler), I have managed to modify the font of the text. However, I am unable to save the font and font size to the txt file. I know that this should be possible as Notepad can modify and save a txt file with a specified font.

这是保存到文件的代码:

This is the code to save to a file:

def file_saveas():
    filename = tkFileDialog.asksaveasfile(mode='w', defaultextension=".txt")
    if filename is None: # asksaveasfile return `None` if dialog closed with "cancel".
        return
    text2save = str(textPad.get(1.0, END)) # starts from `1.0`, not `0.0`
    filename.write(text2save)
    filename.close()
    print filename

这是更改字体的代码(由Rinzler提供):

This is the code (courtesy of Rinzler) to change the font:

def choose_font():
global root, textPad # I hate to use global, but for simplicity

t = Tkinter.Toplevel()
font_name = Tkinter.Label(t, text='Font Name: ')
font_name.grid(row=0, column=0, sticky='nsew')
enter_font = Tkinter.Entry(t)
enter_font.grid(row=0, column=1, sticky='nsew')
font_size = Tkinter.Label(t, text='Font Size: ')
font_size.grid(row=1, column=0, sticky='nsew')
enter_size = Tkinter.Entry(t)
enter_size.grid(row=1, column=1, sticky='nsew')

# associating a lambda with the call to text.config()
# to change the font of text (a Text widget reference)
ok_btn = Tkinter.Button(t, text='Apply Changes',
                   command=lambda: textPad.config(font=(enter_font.get(), 
                   enter_size.get())))
print font
ok_btn.grid(row=2, column=1, sticky='nsew')
done = Tkinter.Button(t, text='Get rid of Pushy!', command=t.destroy)
done.grid(row=4, column=1, sticky='nsew')
# just to make strechable widgets
# you don't strictly need this
for i in range(2):
    t.grid_rowconfigure(i, weight=1)
    t.grid_columnconfigure(i, weight=1)
t.grid_rowconfigure(2, weight=1)

最后,这是读取字体和其他配置信息的代码:

Finally, this is the code that reads the font and other configuration information:

font = (fontname, size)
textPad.config(
    borderwidth=0,
    font=font ,
    foreground="green",
    background="black",
    insertbackground="white", # cursor
    selectforeground="blue", # selection
    selectbackground="#008000",
    wrap="word", 
    width=64,
    undo=True, # Tk 8.4
    )

我在互联网上搜索时未找到关于为什么不保存字体和文本大小的任何答案.任何帮助将不胜感激.

I have searched the internet without coming up with any answers as to why the font and text size are not saved. Any help would be greatly appreciated.

我正在使用python 2.7.7 Tkinter,它正在Windows 7上运行.

I am using python 2.7.7 , Tkinter, and this is being run on Windows 7.

任何对rtf文件的帮助操作也会有所帮助(目前,我看到的是标签,而不是结束格式).

Any help manipulation an rtf file would also be helpful (currently, I see the tags and not the end format).

推荐答案

tkinter不支持此功能.您将必须选择一种支持字体(rtf,.docx,.html等)的文件格式,将小部件中的数据转换为这种格式,然后将其写入文件.

There is no support for this in tkinter. You will have to pick a file fomat that supports fonts (rtf, .docx, .html, etc), convert the data in the widget to this format, and then write it to a file.

这篇关于Python Tkinter文本编辑器不会将字体保存到文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆