我對 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吧。”
PS“剛學Qt”可能是一個可以接受的答案!
uj5u.com熱心網友回復:
果然,改變這個程式使用 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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/483751.html
