我正在try 在turtle和tkinter中制作类似油漆的应用程序.当我只使用turtle时,我制作了一个程序,可以让你使用点击和拖动自由绘制.然后我try 制作一个界面,然后我切换到生 turtle .现在,当我点击 turtle 时,它会移动,当我拖动 turtle 时,它会画,但当我点击并拖动时,它只会移动.我不知道如何修复它
仅限 turtle 中的第一个代码:
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()
使用turtle和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 done函数应该在第一个版本中保持turtlealert ,但在第二个版本中,主循环会保持整个应用程序alert ,我不能再使用done了.我认为问题可能发生在这些功能的某个地方. 我try 在不同的地方使用收听、更新和完成,但似乎没有什么变化(甚至刹车它只是像以前一样工作)