gpt4 book ai didi

python - turtle 手绘图

转载 作者:行者123 更新时间:2023-11-30 22:50:43 24 4
gpt4 key购买 nike

我正在开发一个结合了 Tkinter 和 Turtle 模块的简单绘图程序。

我想添加一个选项,用户只需使用鼠标即可绘制任何内容,类似于画图上的笔小部件。我尝试了很多东西,我不知道如何做到这一点。如何让 turtle 使用鼠标在 Canvas 上绘制任何内容(例如 Paint 上的钢笔小部件)

from tkinter import *
import turtle

sc=Tk()
sc.geometry("1000x1000+100+100")

fr4=Frame(sc,height=500,width=600,bd=4,bg="light green",takefocus="",relief=SUNKEN)

fr4.grid(row=2,column=2,sticky=(N,E,W,S))

#Canvas
canvas = Canvas(fr4,width=750, height=750)
canvas.pack()

#Turtle
turtle1=turtle.RawTurtle(canvas)
turtle1.color("blue")
turtle1.shape("turtle")

points=[]

spline=0

tag1="theline"

def point(event):
canvas.create_oval(event.x, event.y, event.x+1, event.y+1, fill="red")
points.append(event.x)
points.append(event.y)
return points

def canxy(event):
print (event.x, event.y)

def graph(event):
global theline
canvas.create_line(points, tags="theline")

def toggle(event):
global spline
if spline == 0:
canvas.itemconfigure(tag1, smooth=1)
spline = 1
elif spline == 1:
canvas.itemconfigure(tag1, smooth=0)
spline = 0
return spline

canvas.bind("<Button-1>", point)

canvas.bind("<Button-3>", graph)

canvas.bind("<Button-2>", toggle)

sc.mainloop()

最佳答案

下面的代码将让你徒手画 turtle 。您需要与其余代码集成:

import tkinter
import turtle

sc = tkinter.Tk()
sc.geometry("1000x1000+100+100")

fr4 = tkinter.Frame(sc, height=500, width=600, bd=4, bg="light green", takefocus="", relief=tkinter.SUNKEN)

fr4.grid(row=2, column=2, sticky=(tkinter.N, tkinter.E, tkinter.W, tkinter.S))

# Canvas
canvas = tkinter.Canvas(fr4, width=750, height=750)
canvas.pack()

# Turtle
turtle1 = turtle.RawTurtle(canvas)
turtle1.color("blue")
turtle1.shape("turtle")

def drag_handler(x, y):
turtle1.ondrag(None) # disable event inside event handler
turtle1.goto(x, y)
turtle1.ondrag(drag_handler) # reenable event on event handler exit

turtle1.ondrag(drag_handler)

sc.mainloop()

关于python - turtle 手绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39249934/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com