Although '\u0336'
can solve some problems, it may not work in different language situations.
Like: 我是誰 → ̶我̶是̶誰.
As you can see, the otherwise good text has turned into strange symbols that we can't read.
So I write the code below:
import tkinter as tk
root = tk.Tk()
root.state('zoomed')
class strikethrough(tk.Frame):
def __init__(self, frame, text, **options):
super().__init__(frame)
c = tk.Canvas(self, **options)
textId = c.create_text(0, 0, text = text, fill = "#FFFFFF", font = ("", 30, "bold"))
x1, y1, x2, y2 = c.bbox(textId)
linewidth = 3
lineXOffset = 3
lineId = c.create_line(x1, 0, x2, 0, width=linewidth)
c.pack(fill="both", expand=1)
c.bind("<Configure>", lambda event: TextPositionChange(c, textId, lineId, linewidth, lineXOffset))
self.canvas, self.textId = c, textId
def TextPositionChange(canvas, TextId, LineId, LineWidth, LineXOffset):
x1, y1, x2, y2 = canvas.bbox(TextId)
xOffSet, yOffSet = (x2-x1)/2, (y2-y1)/2
x, y = canvas.winfo_width()/2-xOffSet, canvas.winfo_height()/2-yOffSet #left_top_position
canvas.moveto(TextId, x, y)
canvas.moveto(LineId, x-LineXOffset, y+(y2-y1)/2-LineWidth/2)
frame = strikethrough(root, "我是誰", bg="#777777")
frame.place(relx=0.5, rely=0.5, relwidth=0.5, anchor="center")
root.mainloop()