我正在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 在不同的地方使用收听、更新和完成,但似乎没有什么变化(甚至刹车它只是像以前一样工作)

推荐答案

由于 turtle 只有在你点击它时才会移动,所以我必须让 turtle 和整个屏幕一样大.然后我try 用

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()

现在它工作得很完美,耶:)

Python相关问答推荐

socket.gaierror:[Errno -2]名称或服务未知|Firebase x Raspberry Pi

"Discord机器人中缺少所需的位置参数ctx

过载功能是否包含Support Int而不是Support Int?

使用matplotlib pcolormesh,如何停止从一行绘制的磁贴连接到上下行?

如何使用没有Selenium的Python在百思买着陆页面上处理国家/地区 Select ?

如何使用scipy从频谱图中回归多个高斯峰?

运行总计基于多列pandas的分组和总和

将输入管道传输到正在运行的Python脚本中

如何更改分组条形图中条形图的 colored颜色 ?

对所有子图应用相同的轴格式

Odoo 16使用NTFS使字段只读

Pandas Loc Select 到NaN和值列表

如何在turtle中不使用write()来绘制填充字母(例如OEG)

如何指定列数据类型

如何使用SentenceTransformers创建矢量嵌入?

考虑到同一天和前2天的前2个数值,如何估算电力时间序列数据中的缺失值?

通过ManyToMany字段与Through在Django Admin中过滤

Gekko中基于时间的间隔约束

如何在海上配对图中使某些标记周围的黑色边框

使用Python异步地持久跟踪用户输入