简体   繁体   中英

Get Python turtle window to display graphics and remain open

In this code I can't see why it isn't printing a hexagon 24 times. I tell it to make a 6 sided shape with 60 degrees between lines ( a hexagon) and tell it do turn 15 degrees each time. This ends up being a even 24 for the picture I'm trying to draw.

import turtle

Hex_Count = 0

x = turtle.Turtle()
x.speed(.25)
def Hexagon():
    for i in range(24):
        for i in range(6):
            x.forward(100)
            x.left(60)
        Hex_Count = Hex_Count + 1
        x.left(15)
        print(Hex_Count)
Hexagon

But, for some reason, when I run this code the turtle screen pops up for about a half second then closes. How do I get it to perform in the way I want it to?

You have several problems with your program. One is that it will when after running through the program, closing the window it created. You can add turtle.exitonclick() to the end of your script which tells python to wait for a click in the graphics window, after which it will exit.

The second problem is that you don't call the Hexagon function because you're missing the parentheses. Even if a function takes no arguments, you still need to call it like:

Hexagon()

The final problem is that you need to define Hex_Count before you try to increment it. Hex_Count + 1 will thrown an error if Hex_Count wasn't already assigned to. You can fix this by putting

Hex_Count = 0

before your for loop in Hexagon .

You have some reference issue, you just need to put the variable hex_count where it needs to be so you don't have error accessing it.

import turtle

x = turtle.Turtle()
x.speed(.25)
def Hexagon():
    Hex_Count = 0
    for i in range(24):
        for i in range(6):
            x.forward(100)
            x.left(60)
        Hex_Count += 1
        x.left(15)
    print(Hex_Count)
Hexagon()

prints 24

You have several errors that I corrected for you; I added the explanation in the comments:

import turtle

hexagons_count = 0

my_turtle = turtle.Turtle()       # x is not a good name for a Turtle object
# my_turtle.speed(.25)            # see @cdlane comment reported in a note under.

def draw_hexagon():               # use explicit names respecting python conventions (no camel case)
    global hexagons_count         # need global to modify the variable in the function scope
    for idx in range(24):         # use different dummy variable names in your loops 
        for jdx in range(6):      # use different dummy variable names in your loops
            my_turtle.forward(100)
            my_turtle.left(60)
        hexagons_count += 1
        my_turtle.left(15)
        print(hexagons_count)

draw_hexagon()             # need parenthesis to call the function

turtle.exitonclick()       # this to exit cleanly

在此处输入图片说明

Note: I know you simply copied it from the OP but my_turtle.speed(.25) doesn't make sense as the argument should be an int from 0 to 10 or a string like 'slow', 'fastest', etc. I especially don't understand why beginners with turtle code that isn't working call turtle.speed() at all -- it seems to me a function to be tweaked after everything is working. @cdlane

An approach different in a lot of the details but primarily in its use of circle() to more rapidly draw the hexagons:

from turtle import Turtle, Screen  # force object-oriented turtle

hex_count = 0  # global to count all hexagons drawn by all routines

def hexagons(turtle):
    global hex_count  # needed as this function *changes* hex_count

    for _ in range(24):  # don't need explicit iteration variable
        turtle.circle(100, steps=6)  # use circle() to draw hexagons
        turtle.left(15)  # 24 hexagons offset by 15 degrees = 360
        hex_count += 1  # increment global hexagon count
        print(hex_count)

screen = Screen()

yertle = Turtle(visible=False)  # keep turtle out of the drawing
yertle.speed('fastest')  # ask turtle to draw as fast as it can

hexagons(yertle)

screen.exitonclick()  # allow dismiss of window by clicking on it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM