如何在 Tkinter 滚动文本中插入标签,以便用户可以输入他们选择的样式

How to insert a tag in a Tkinter ScrolledText so that the user can type in their chosen style

我是 Python 的新手,我 运行 遇到了一个似乎无法找到答案的障碍。我有一个程序,其中多个 tkinter.Entry 和 tkinter.ScrolledText 元素分布在 tkinter.Notebook 中的多个选项卡中。我已经为基本格式(粗体、斜体和下划线)配置了标签。

标签在插入文本时工作正常,但我想让用户能够将光标放在 ScrolledText 框中的任意位置(在现有文本的末尾或中间),选择他们想要的格式输入,然后将他们输入的内容作为该格式输出。到目前为止,这是我得到的:

import tkinter as tk
from tkinter.scrolledtext import ScrolledText
...
#an example of one of the Scrolled Text widgets
sermonText = ScrolledText(
    self.sermonFrame,
    fg = 'black',
    font = self.standardFont,
    spacing2 = 3,
    spacing3 = 10,
    wrap = tk.WORD,
    padx = 10,
    pady = 10)
sermonText.grid(
    column = 0,
    row = 6,
    columnspan = 6,
    sticky = 'nsew')
self.configureTags(sermonText)

#the function to configure tags for the ScrolledText widgets
def configureTags(self, component):
    component.tag_configure('plainText', font = ('Helvetica', '12'))
    component.tag_configure('boldText', font = ("Helvetica", "12", "bold"))
    component.tag_configure('italicText', font = ("Helvetica", "12", "italic"))
    component.tag_configure('boldItalicText', font = ('Helvetica', '12', 'bold italic'))
    component.tag_configure('underlineText', font = ('Helvetica', '12', 'underline'))
    component.tag_configure('boldUnderlineText', font = ('Helvetica', '12', 'bold underline'))
    component.tag_configure('underlineItalicText', font = ('Helvetica', '12', 'underline italic'))
    component.tag_configure('boldUnderlineItalicText', font = ('Helvetica', '12', 'bold underline italic'))

还有用户可以单击的格式化按钮。粗体按钮调用此函数:

def setBold(self):
    currentComponent = self.root.focus_get()
    if currentComponent.tag_ranges(tk.SEL):
        cursorStart = currentComponent.index(tk.SEL_FIRST)
        cursorEnd = currentComponent.index(tk.SEL_LAST)
        print('first:', cursorStart, 'last', cursorEnd)
        currentComponent.tag_add('boldText', cursorStart, cursorEnd)
    else:
        cursor = currentComponent.index(tk.INSERT)
        currentComponent.tag_add('boldText', cursor)
        currentComponent.configure(font = ('Helvetica', '12', 'bold'))

当存在通过拖动鼠标选中的文本时,此方法有效。否则,如果单击粗体按钮,它只会更改框中已有文本末尾的格式。

tl:dr - 如何更改 setBold 函数,以便用户可以单击 tkinter ScrolledText 框中的任意位置,单击调用它的按钮,然后开始以粗体输入。

即 现有文本: “我们都去酒吧吧。” 将光标放在 'bar' 之前,单击粗体按钮,然后键入 'foo ';导致: “我们都去 foo 吧。”

P.S。 “只学Qt”大概是一个可以接受的答案!

果然,将此程序更改为使用 Qt 而不是 Tkinter 使我所有的文本格式化梦想成真。总而言之,对于每个 Tkinter ScrolledText 小部件,我都使用了 Qt 的 QTextEdit 小部件。这使我能够将这些功能连接到每个粗体、斜体和下划线按钮:

def setBold(self):
    component = self.win.focusWidget()
    if isinstance(component, QTextEdit):
        font = QFont(component.textCursor().charFormat().font())
        if font.weight() == QFont.Normal:
            font.setWeight(QFont.Bold)
            component.setCurrentFont(font)
        else:
            font.setWeight(QFont.Normal)
            component.setCurrentFont(font)

def setItalic(self):
    component = self.win.focusWidget()
    if isinstance(component, QTextEdit):
        font = QFont(component.textCursor().charFormat().font())
        if font.italic() == False:
            font.setItalic(True)
            component.setCurrentFont(font)
        else:
            font.setItalic(False)
            component.setCurrentFont(font)

def setUnderline(self):
    component = self.win.focusWidget()
    if isinstance(component, QTextEdit):
        font = QFont(component.textCursor().charFormat().font())
        if font.underline() == False:
            font.setUnderline(True)
            component.setCurrentFont(font)
        else:
            font.setUnderline(False)
            component.setCurrentFont(font)