我的代码的目的是让三角形替换游标。但是,我需要一个解决方案来删除先前由光标移动绘制的三角形。
这是我的原始代码:
from tkinter import*
tk = Tk()
tk.resizable(0,0)
tk.attributes('-topmost',True)
window = Canvas(width = 1300, height = 700, bg = 'black',\
highlightthickness=0, relief='ridge')
window.pack()
window.config(cursor='none')
class Cursor:
def __init__(self,canvas):
self.canvas = canvas
self.id = self.canvas.create_polygon(0,0,10,0,0,-10)
def Move(self):
self.Mousex = self.canvas.winfo_pointerx()
self.Mousey = self.canvas.winfo_pointery()
self.id = self.canvas.create_polygon(self.Mousex,self.Mousey,\
self.Mousex+10,self.Mousey,\
self.Mousex,self.Mousey-10,\
fill = 'red')
Cursor1 = Cursor(window)
while 1:
tk.update_idletasks()
window.update()
Cursor1.Move()
我尝试过以下代码,但根本没有三角形出现。在下面的代码中我插入了一个:
self.canvas.delete(self.id)
以便代码为:
from tkinter import*
tk = Tk()
tk.resizable(0,0)
tk.attributes('-topmost',True)
window = Canvas(width = 1300, height = 700, bg = 'black',\
highlightthickness=0, relief='ridge')
window.pack()
window.config(cursor='none')
class Cursor:
def __init__(self,canvas):
self.canvas = canvas
self.id = self.canvas.create_polygon(0,0,10,0,0,-10)
def Move(self):
self.Mousex = self.canvas.winfo_pointerx()
self.Mousey = self.canvas.winfo_pointery()
self.id = self.canvas.create_polygon(self.Mousex,self.Mousey,\
self.Mousex+10,self.Mousey,\
self.Mousex,self.Mousey-10,\
fill = 'red')
self.canvas.delete(self.id)
Cursor1 = Cursor(window)
while 1:
tk.update_idletasks()
window.update()
Cursor1.Move()
答案 0 :(得分:1)
Canvas已经定义了move
方法,您可以使用它:
from tkinter import*
tk = Tk()
tk.resizable(0,0)
tk.attributes('-topmost',True)
window = Canvas(width = 1300, height = 700, bg = 'black',\
highlightthickness=0, relief='ridge')
window.pack()
window.config(cursor='none')
class Cursor:
def __init__(self,canvas):
self.canvas = canvas
self.id = self.canvas.create_polygon(0,0,10,0,0,-10, fill='red')
self.Mousex = 0
self.Mousey = 0
def Move(self):
_new_x = self.canvas.winfo_pointerx()
_new_y = self.canvas.winfo_pointery()
self.canvas.move(self.id, _new_x - self.Mousex, _new_y - self.Mousey)
self.Mousex = _new_x
self.Mousey = _new_y
#self.canvas.delete(self.id)
Cursor1 = Cursor(window)
while 1:
tk.update_idletasks()
window.update()
Cursor1.Move()