[Python] I have some problem with making program with Python turtle graphics

Discussão em 'Python' iniciado por Stack, Outubro 24, 2024.

  1. Stack

    Stack Membro Participativo

    I have some questions about my code.

    I'm a beginner at Python, and I'm starting a drawing program with Python turtle graphics and uploading it to Github.

    I want to make it so when I click the left mouse button, it draws the line. And when I just move my mouse without clicking the left button, the turtle just follows my cursor.

    So I googled it and found some weird but maybe helpful code.

    Like, when I click the left mouse button, the turtle doesn't move, and I can move the turtle by clicking the left mouse button, but the line doesn't draw...

    Anyways, here is all of my code. (I think showing all the code will be better for you.)


    from turtle import Screen

    screen = Screen()

    while True:

    print('===========================')
    print('*** Welcome to Pyint!!! ***')
    print('___________________________')
    print('*** Drawing With Turtle ***')
    print('___________________________')
    print('******* ver. 2.1.0 ********')
    print('___________________________')
    print('* Press Enter For Start!! *')
    print('___________________________')
    print('===========================')

    import turtle as t

    t.color('white')
    t.bgcolor('black')
    t.speed(0)
    t.shape('circle')
    t.pensize(5)
    t.shapesize(2,2,2)

    break

    def colorsettings():

    while True:

    print('------------------------')
    print('**** Color Settings ****')
    print('------------------------')

    chgpencolor = t.color(str(input('Color?:')))
    savecolorsettings = str(input('Save the color settings? [y/n]:'))

    if savecolorsettings == 'n':
    continue
    elif savecolorsettings == 'y':
    print('-----------------------')
    print('******** Saved ********')
    print('-----------------------')
    break

    def thicknesssettings():

    while True:

    print('------------------------')
    print('** Thickness Settings **')
    print('------------------------')

    chgpenthickness = t.pensize(int(input('Thickness?:')))
    savethicknesssettings = str(input('Save the thickness settings? [y/n]:'))

    if savethicknesssettings == 'n':
    continue
    elif savethicknesssettings == 'y':
    print('-----------------------')
    print('******** Saved ********')
    print('-----------------------')
    break

    Moving, Dragging = range(2) # Codes about control the turtle start here.

    def move_pen(x, y):
    if state != Moving:
    return

    onmove(screen, None)
    t.penup()
    t.setheading(t.towards(x, y))
    t.goto(x, y)
    onmove(screen, move_pen)

    def click_pen(x, y):
    global state

    t.onclick(None)
    onmove(screen, None)

    t.onrelease(release_handler)
    t.ondrag(drag_handler)

    state = Dragging

    def release_handler(x, y):
    global state

    t.onrelease(None)
    t.ondrag(None)

    t.onclick(click_pen)
    onmove(screen, move_pen)

    state = Moving

    def drag_handler(x, y):
    if state != Dragging:
    return

    t.ondrag(None)
    t.pendown()
    t.setheading(t.towards(x, y))
    t.goto(x, y)
    t.ondrag(drag_handler)

    def onmove(self, fun, add=None):

    if fun is None:
    self.cv.unbind('<Motion>')
    else:
    def eventfun(event):
    fun(self.cv.canvasx(event.x) / self.xscale, -self.cv.canvasy(event.y) / self.yscale)
    self.cv.bind('<Motion>', eventfun, add)

    onmove(screen, move_pen)
    t.onclick(click_pen) # End of codes about control the turtle

    state = Moving

    while True:

    ipt = str(input())

    if ipt == 'pencolor':
    colorsettings()
    elif ipt == 'penthickness':
    thicknesssettings()
    elif ipt == 'h':
    t.goto(0,0)
    elif ipt == 'c':
    t.clear()
    elif ipt == 'z':
    t.undo()
    elif ipt =='pu':
    t.penup()
    elif ipt =='pd':
    t.pendown()
    elif ipt == 'gg':
    print('Are you really want to close the program? [y/n]')
    elif ipt == 'n':
    continue
    elif ipt == 'y':
    break



    This was my first time here, and also my first question. If my question was rude or unkind for you, I want to say sorry.

    Thanks :)

    Continue reading...

Compartilhe esta Página