0

This is a branch from my main code of a lottery ticket generator. The purpose of these two functions: (1) drawButton(length) to create rectangular buttons. (2) createMenu() to call drawButton(length) and to fill the buttons with labels.

My issue is when the main code attempts to return to the main menu, it runs turtle.clearscreen(). Shortly after writing the main menu, it did not draw the first rectangle/button.

Let me know if you guys get a different result than I do.

import turtle
import time

t1 = turtle.Turtle()
t1.speed(0)
t1.penup()

def drawButton(length):
    length1 = length*5
    for i in range(2):
        t1.fd(length1)
        t1.lt(90)
        t1.fd(length)
        t1.lt(90)

def createMenu():
    t1.sety(-13)
    down = t1.ycor()

    for i in range(4):
        t1.goto(-150, down)
        t1.pendown()
        drawButton(60)
        t1.penup()
        down = t1.ycor()-100

createMenu()
time.sleep(2)

turtle.clearscreen()

createMenu()

turtle.done()

This is what the program should draw on the second function call: This is what the program should draw on the second function call

This is what I get after the second function call: This is what I get after the second function call

Solenad
  • 13
  • 2
  • 1
    Check out [this article](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) for tips on debugging your code. – Code-Apprentice Nov 13 '22 at 16:27
  • 1
    When I run your code, the first call to `createMenu()` does in fact draw all 4 rectangles. Then it pauses for 2 seconds and draws the rectangles again. After the second call to `createMenu()` there are only 3 rectangles visible. I suspect one of the rectangles is drawn twice. You will have to use the debugging techniques in the article I linked above in order to track down why this happens. – Code-Apprentice Nov 13 '22 at 16:36

2 Answers2

0

There is a Simple fix, just change turtle.clearscreen() to t1.clear(). They are two different commands and work differently. https://stackoverflow.com/a/42260054/18554284 This Answer has a better explanation of their working

KillerRebooted
  • 470
  • 1
  • 12
  • How does this explain why only 3 rectangles are drawn? – Code-Apprentice Nov 13 '22 at 16:28
  • I am not sure, that's why I referred to the answer. According to the Documentation, it should not produce such results as `clearscreen()` is basically the same as `clear()` for all turtles – KillerRebooted Nov 13 '22 at 16:34
  • Your answer will be more useful to future visitors if you can also explain why this fixes the issue. – Code-Apprentice Nov 13 '22 at 16:38
  • @KillerRebooted Your fix works and it's more convenitent than the one i just came up with. From the link you sent, it stated that turtle.clearscreen() also deletes turtles, so I tried putting a t1 = turtle.Turtle() after the turtle.clearscreen() and it draws four rectangles on the second call. I assume that only 3 rectangles are drawn because it runs through the createMenu() function without a turtle then recreates it on the next occurences. Reiterating what you just said, your t1.clear() works because it does not delete all the turtles and just undoes all the drawings t1 did. – Solenad Nov 13 '22 at 17:47
  • Thank for clarifying things @Solenad. I also thought something similar, but the logic of it creating a turtle in the next loop as it does not exist in the first, seemed weird, so I just said I was unsure as I didn't want to give the wrong answer – KillerRebooted Nov 13 '22 at 20:50
0

Try using turtle.resetscreen() to also reset the state of the turtle.

Eloi
  • 101
  • 10