You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

如何解决Python Tkinter富文本编辑器重复格式化选中文本的问题?

Fixing Tkinter Text Widget Formatting Issue: Independent Styling for Bold/Italic

Looks like the problem here is that you're reusing the same contboxfont dictionary for all your tags. Since dictionaries are mutable in Python, every time you modify contboxfont['weight'] or contboxfont['slant'], you're changing the underlying object that all existing tags are referencing. That's why your second formatting operation affects previously styled text—those old tags are still pointing to the same updated font dict.

Here's how to fix this so each formatting operation applies only to the selected text independently:

Step 1: Avoid modifying a shared font dictionary

Instead of altering a single global font dict, create a new font configuration each time you apply a style. You can base it on the Text widget's default font to maintain consistency, then override just the property you need for the current style.

Step 2: Update your bold/italic functions

Revise your functions to create fresh font configurations per operation, and make sure each tag uses its own unique font settings:

import tkinter as tk
from tkinter.font import Font

def boldtxt():
    bldbtn['activebackground'] = "#ffad33"
    # Get the default font of the text widget
    default_font = Font(font=contbox['font'])
    # Create a new bold font based on default
    bold_font = Font(font=default_font)
    bold_font.configure(weight="bold")
    # Add tag with the new bold font
    contbox.tag_add("bold", tk.SEL_FIRST, tk.SEL_LAST)
    contbox.tag_config("bold", font=bold_font)

def italictxt():
    itlbtn['activebackground'] = "#ffad33"
    # Get the default font of the text widget
    default_font = Font(font=contbox['font'])
    # Create a new italic font based on default
    italic_font = Font(font=default_font)
    italic_font.configure(slant="italic")
    # Add tag with the new italic font
    contbox.tag_add("italic", tk.SEL_FIRST, tk.SEL_LAST)
    contbox.tag_config("italic", font=italic_font)

# Initialize the GUI
root = tk.Tk()
contbox = tk.Text(root)
contbox.pack()

bldbtn = tk.Button(root, text="Bold", command=boldtxt)
bldbtn.pack(side=tk.LEFT)

itlbtn = tk.Button(root, text="Italic", command=italictxt)
itlbtn.pack(side=tk.LEFT)

root.mainloop()

Key Improvements:

  • We use tkinter.font.Font to create distinct font objects for each style, instead of modifying a shared dictionary. This ensures each tag has its own independent font configuration.
  • By basing each styled font on the Text widget's default font, we preserve existing font properties (like family and size) while only changing the weight/slant.
  • Each tag ("bold" and "italic") now uses its own unique font object, so modifying one won't affect the other or previously styled text.

Bonus: Toggle Functionality (Optional)

If you want to let users toggle styles on/off (e.g., click bold again to remove the bold style), you can check if the selected text already has the tag and adjust accordingly:

def boldtxt():
    bldbtn['activebackground'] = "#ffad33"
    # Check if any selected text has the bold tag
    selected_tags = contbox.tag_names(tk.SEL_FIRST)
    if "bold" in selected_tags:
        # Remove the bold tag from selection
        contbox.tag_remove("bold", tk.SEL_FIRST, tk.SEL_LAST)
    else:
        # Apply bold as before
        default_font = Font(font=contbox['font'])
        bold_font = Font(font=default_font)
        bold_font.configure(weight="bold")
        contbox.tag_add("bold", tk.SEL_FIRST, tk.SEL_LAST)
        contbox.tag_config("bold", font=bold_font)

This way, users can easily switch styles on and off without unintended side effects.

内容的提问来源于stack exchange,提问作者Srinjay Vatsa

火山引擎 最新活动