我正在尝试在 Turtle 和 tkinter 中制作类似绘画的应用程序。当我只使用海龟时,我制作了一个程序,可以让您通过单击和拖动自由绘图。然后我尝试制作一个界面并切换到原始海龟。现在,当我单击乌龟时,它会移动,当我拖动乌龟时,它会绘制,但当我单击并拖动时,它只会移动。我不知道如何解决它
仅限海龟中的第一个代码:
import turtle
turtle.tracer(False)
turtle.hideturtle()
def draw(x, y):
t.ondrag(None)
t.down()
t.goto(x, y)
t.up()
turtle.update()
t.ondrag(draw)
def move(x, y):
turtle.onscreenclick(None)
t.goto(x, y)
turtle.onscreenclick(move)
turtle.update()
t = turtle.Turtle()
t.shape("circle")
t.pencolor('black')
t.fillcolor("")
t.up()
t.pensize(6)
t.turtlesize(1000000, 1000000)
ts = turtle.getscreen()
t.ondrag(draw)
turtle.onscreenclick(move)
turtle.update()
turtle.listen()
turtle.done()
使用海龟和 tkinter 编写代码:
import tkinter as tk
from functools import partial
from turtle import TurtleScreen, RawTurtle
def draw(x, y):
turtle.ondrag(None)
turtle.pendown()
turtle.goto(x, y)
turtle.penup()
screen.update()
turtle.ondrag(draw)
def move(x, y):
screen.onclick(None)
turtle.goto(x, y)
screen.onclick(move)
screen.update()
def set_color(color):
global pen_color
pen_color = color
turtle.pencolor(color)
screen.update()
# --- add widgets ---
root = tk.Tk()
canvas = tk.Canvas(root, width=500, height=500)
canvas.pack(side='right', expand=True, fill='both')
frame = tk.Frame(root)
frame.pack(side='left', fill='y')
tk.Label(frame, text='COLORS').grid(column=0, row=0)
tk.Button(frame, bg='red', width=10, command=partial(set_color, 'red')).grid(column=0, row=1)
tk.Button(frame, bg='yellow', width=10, command=partial(set_color, 'yellow')).grid(column=0, row=2)
tk.Button(frame, bg='green', width=10, command=partial(set_color, 'green')).grid(column=0, row=3)
tk.Button(frame, bg='blue', width=10, command=partial(set_color, 'blue')).grid(column=0, row=4)
tk.Button(frame, bg='black', width=10, command=partial(set_color, 'black')).grid(column=0, row=5)
screen = TurtleScreen(canvas)
screen.tracer(False)
pen_color = 'black'
turtle = RawTurtle(screen)
# turtle.hideturtle()
turtle.shape("circle")
turtle.penup()
turtle.pensize(5)
turtle.color(pen_color, screen.bgcolor())
turtle.ondrag(draw)
screen.onclick(move)
screen.update()
screen.listen()
screen.mainloop()
根据我的理解(我可能是错的,我刚刚开始学习Python),listen 函数应该是收集事件的函数。海龟完成功能应该在第一个版本中保持海龟警报,但在第二个版本中,主循环使整个应用程序保持警报,我不能再使用完成了。我认为问题可能发生在这些函数的某个地方。 我尝试在不同的地方使用监听、更新和完成,但似乎没有任何改变(甚至制动它就像以前一样工作)
由于乌龟只有在单击时才会移动,因此我必须使乌龟与整个屏幕一样大。然后我尝试使用
使其不可见turtle.hideturtle()
这不起作用,所以我在不改变画笔颜色或填充颜色的情况下让乌龟隐形了
新代码:
import tkinter as tk
from functools import partial
from turtle import TurtleScreen, RawTurtle, Shape
def draw(x, y):
turtle.ondrag(None)
turtle.pendown()
turtle.goto(x, y)
turtle.penup()
screen.update()
turtle.ondrag(draw)
def move(x, y):
screen.onclick(None)
turtle.goto(x, y)
screen.onclick(move)
screen.update()
def set_color(color):
global pen_color
pen_color = color
turtle.pencolor(color)
screen.update()
# --- add widgets ---
root = tk.Tk()
canvas = tk.Canvas(root, width=500, height=500)
canvas.pack(side='right', expand=True, fill='both')
frame = tk.Frame(root)
frame.pack(side='left', fill='y')
tk.Label(frame, text='COLORS').grid(column=0, row=0)
tk.Button(frame, bg='red', width=10, command=partial(set_color, 'red')).grid(column=0, row=1)
tk.Button(frame, bg='yellow', width=10, command=partial(set_color, 'yellow')).grid(column=0, row=2)
tk.Button(frame, bg='green', width=10, command=partial(set_color, 'green')).grid(column=0, row=3)
tk.Button(frame, bg='blue', width=10, command=partial(set_color, 'blue')).grid(column=0, row=4)
tk.Button(frame, bg='black', width=10, command=partial(set_color, 'black')).grid(column=0, row=5)
screen = TurtleScreen(canvas)
screen.tracer(False)
turtle = RawTurtle(screen)
#turtle.hideturtle()
turtle.shape("circle")
polygon = turtle.get_shapepoly()
fixed_color_turtle = Shape("compound")
fixed_color_turtle.addcomponent(polygon, "", "")
screen.register_shape('fixed', fixed_color_turtle)
turtle.shape("fixed")
turtle.penup()
turtle.pensize(5)
turtle.turtlesize(2000,2000)
turtle.ondrag(draw)
screen.onscreenclick(move)
screen.update()
screen.mainloop()
现在一切正常了,耶:)