【问题标题】:Change the font proprietes of a piece of text更改一段文本的字体属性
【发布时间】:2017-12-02 20:32:32
【问题描述】:

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

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

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_0之前研究了很多
  • 您需要标记您想要看起来不同的文本片段,然后使用 tag_config 方法更改带有标记的文本属性。例如,这就是基于 tkinter 的 IDLE 语法为 python 代码着色的方式。阅读http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html 中的文本小部件文档。还可以尝试在 SO tkinter 标记的问题中搜索“文本标签”或类似的内容。
  • 如果您进行了研究,请在问题中包含您学到的一些内容。例如,您尝试过的方法无效。

标签: python text tkinter fonts notepad


【解决方案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()

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

【讨论】:

    猜你喜欢
    • 2011-07-21
    • 1970-01-01
    • 2016-07-04
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 2012-02-05
    • 2013-10-20
    • 1970-01-01
    相关资源
    最近更新 更多