请更改按下import tkinter as tkclass DraftApp def __init__self root selfroot = root selfroottitleDraft selfrootattributes-topmost True selfcanvas = tkCanvasselfroot width=800
请更改"按下**": import tkinter as tk
class DraftApp: def init(self, root): self.root = root self.root.title("Draft") self.root.attributes("-topmost", True) self.canvas = tk.Canvas(self.root, width=800, height=600, bg="white") self.canvas.pack()
def main(self):
key = self.check_mouse_state()
x, y = self.root.winfo_pointerx(), self.root.winfo_pointery()
if key == 1:
self.draw_circle(x, y, "purple", 10)
elif key == 2:
self.draw_circle(x, y, "white", 20)
elif key == 3:
self.canvas.delete("all")
self.root.after(100, self.main)
def check_mouse_state(self):
state = self.root.state()
if "按下左键" in state:
return 1
elif "按下右键" in state:
return 2
elif "按下中键" in state:
return 3
else:
return 0
def draw_circle(self, x, y, color="white", radius=10):
self.canvas.create_oval(
x - radius,
y - radius,
x + radius,
y + radius,
outline=color,
fill=color,
)
if name == "main": root = tk.Tk() app = DraftApp(root) app.main() # Start the main loop root.mainloop(
原文地址: https://www.cveoy.top/t/topic/iLka 著作权归作者所有。请勿转载和采集!