2017-12-02 323 views
-1

我正在制作一个记事本,并且已经创建了字体工具,但是这会更改所有文字的字体,而不仅仅是选定的部分或您所在的部分更改一段文字的字体专有

所有这些工具的代码如下所示:

def ng(): 
    global fn #the font 
    global b #Bold variable 

    b=not b 
    if b==True: 
     fn.configure(weight="bold") 
    if b==False: 
     fn.configure(weight="normal") 

    scroll.config(font=fn) 

我如何做到这一点?

+0

你在askng之前做过任何研究吗?在这个网站上有很多问题和例子。 –

+0

是的,我研究了很多之前0_0 – KaiXtr

+0

您需要标记您想要看起来不同的文本切片,然后使用tag_config方法更改带有标记的文本属性。例如,这是基于tkinter的IDLE语法如何为python代码添加颜色。阅读'http:// infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html'中的文本部件doc。也尝试搜索SO'tkinter'标记的问题'文本标签'或类似的东西。 –

回答

1

这是一个最小的例子,它使用前景色而不是字体,因为它更容易,创建新字体是一个单独的问题。

import tkinter as tk 
root = tk.Tk() 
text = tk.Text(root) 
text.pack() 
text.tag_config('RED', foreground='red') 
text.tag_config('BLUE', foreground='blue') 
text.insert('insert', 'Some tk colors are ') 
text.insert('insert', 'red', 'RED') 
text.insert('insert', ' and ') 
text.insert('insert', 'blue', 'BLUE') 
text.insert('insert', '.') 
root.update() 

可以在插入文本后面添加标签,并在使用后更改标签配置。