tkinter.Canvas.move函数移动有残影(已update!)

##编程环境是vscode python版本是3.10.0
故障如下:
当我试图利用右移按钮实现文本的平移时,每按4下会留下一条竖线残影,如图:

img

有意思的是,当文本移动到中央偏右的时候,残影不再生成。这个残影当鼠标移出框外时自动消失,左移同样,已经使用过tk.canvas.update()对画布进行更新,位置在处理函数的最后一行也尝试过,都消除不了,求解!
附上代码:

import tkinter as tk
import random
from tkinter.constants import LEFT, RIGHT
from turtle import bgcolor, fillcolor
def RGB():
    return ["#"+''.join([random.choice('0123456789ABCDEF') for j in range(6)])]
class ControlText:
    def __init__(self):
         window=tk.Tk()
         window.title("标签定制")

         colorframe = tk.Frame(window)
         colorframe.pack()
         self.str = tk.StringVar()
         self.str.set('R')
         rbRed = tk.Radiobutton(colorframe, text = "红色", variable = self.str, value = 'R', command = self.setColor)
         rbRed.pack(side = LEFT)
         rbYellow = tk.Radiobutton(colorframe, text = "黄色", variable = self.str, value = 'Y', command = self.setColor)
         rbYellow.pack(side = LEFT)
         rbWhite = tk.Radiobutton(colorframe, text = "白色", variable = self.str, value = 'W', command = self.setColor)
         rbWhite.pack(side = LEFT)
         rbBlue = tk.Radiobutton(colorframe, text = "绿色", variable = self.str, value = 'G', command = self.setColor)
         rbBlue.pack(side = LEFT)
         rbRand = tk.Radiobutton(colorframe, text = "随机色", variable = self.str, value = 'D', command = self.setColor)
         rbRand.pack(side = LEFT)
         
         self.width = 250
         self.canvas = tk.Canvas(window, width = self.width, height = 40)
         self.canvas.pack()
         self.x = 42
         self.label = tk.Label(self.canvas, bg = "red", bd = 0, text = "Python Tkinter")
         self.canvas.create_window(self.x, 30, window = self.label, tags = "PT")

         frame = tk.Frame(window)
         frame.pack()
         LButton = tk.Button(frame, text = "左移", command = self.leftMove)
         LButton.pack(side = LEFT)
         RButton = tk.Button(frame, text = "右移", command = self.rightMove)
         RButton.pack(side = RIGHT)
         window.mainloop()
    def setColor(self):
        if self.str.get() =='R':
            self.label["bg"] = "red"
        elif self.str.get() =='Y':
            self.label["bg"] = "yellow"
        elif self.str.get() =='W':
            self.label["bg"] = "white"
        elif self.str.get() =='G':
            self.label["bg"] = "green"
        else:
            self.label["bg"] = RGB()
    def leftMove(self):
        self.canvas.move("PT", -5, 0)
        
        self.canvas.update()
        if self.x > 42:
            self.x -= 5
        else:
            self.x = self.width - 42
            self.canvas.delete("PT")
            color = self.label["bg"]
            self.canvas.create_window(self.x, 30, window = self.label, tags = "PT")
    def rightMove(self):
        self.canvas.move("PT", 5, 0)
        self.canvas.update()
        if self.x < self.width - 42:
            self.x += 5
        else:
            self.x = 42
            self.canvas.delete("PT")
            color = self.label["bg"]
            self.canvas.create_window(self.x, 30, window = self.label, tags = "PT")
ControlText()

你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答


本次提问扣除的有问必答次数,将会以问答VIP体验卡(1次有问必答机会、商城购买实体图书享受95折优惠)的形式为您补发到账户。


因为有问必答VIP体验卡有效期仅有1天,您在需要使用的时候【私信】联系我,我会为您补发。

所以你是用这种方法的吧:创建tkinter label组件,不断删除重绘位置已达到移动效果。
出现你这种情况有两种原因:

  1. 你的电脑屏幕刷新频率不够。
  2. 你使用创建并重绘组件以达到移动效果会给显卡带来一定运算延迟。

因此,我建议你使用画布自己的绘制功能,通过绘制文本和矩形框,绑定同样的tag,通过这个tag使用画布的move函数。

具体你可以看我的专栏“tkinter绘制虚拟组件”,其中按钮和进度条的绘制可能对你有帮助。

关于画布的具体使用见https://blog.csdn.net/qq_41556318/article/details/85272026