简体   繁体   中英

Can you change the colour of the turtle pen in a tkinter interface

so I'm new to Python and have to do a project where we make an interface that generates turtle figures using a tkinter GUI. 20% of our marks go for personal contribution to the interface so I want to be able to change the color of the turtle pen by selecting from an option menu, eg when 'Binary Tree' is selected you can choose red and it will draw in red rather than the default black. I'm after spending the bones of 8 hours so far trying to get this to work. Can someone please help me or tell me if it's possible to do? Here's my code so far:

# Import the Modules
from turtle import *
from tkinter import *
from tkinter.ttk import Entry, OptionMenu
import math

library = ["Binary Tree", "Dandelion", "Fern", "Flake", "Square Gasket", "Swiss Flag", "Square Gasket", "Circle Gasket"]
colorLibrary = ["black", "red", "blue", "green", "cyan", "magenta", "white", "yellow"]


#Create an empty window and give it's width and height
 root = Tk()
 root.title("Turtle Fractals")
 root.geometry("400x200+300+300")

#Set the pen speed & width
 pen = Pen()
 pen.speed(0)
 pen.width(1)

#end def
 screen = Screen()
 screen.bgcolor("grey")

#===================================
#   Event handler functions
#===================================
def clearF() :
#Empty the entry vars
lengthStr.set("")
fractalStr.set("")
screen.reset()

#End def
pen.goto(0,0)

def drawF() :
 # get the string and make it an integer
 age = int(fractalStr.get())
 length = int(lengthStr.get())

graphics = library.index(libStr.get())    
if graphics == 0 :
    binTree (age, length);

elif graphics == 1 :
    dandelion (age, length);

elif graphics == 2 :
    fern (age, length);

elif graphics == 3 :
    flake (age,length);

elif graphics == 4 :
    sGasket (age,length);

elif graphics == 5 :
    swissFlag (age, length);

elif graphics == 6 :
    squareGasket (age, length);

elif graphics == 7 :
    circleGasket (age, length);

pen = colorLibrary.index(colorLibStr.get())    
if pen == 0 :
     black();

elif pen == 1 :
     red();

elif pen == 2 :
     blue();

elif pen == 3 :
     green();

elif pen == 4 :
     cyan();

elif pen == 5 :
     magenta();

elif pen == 6 :
     white();

elif pen == 7 :
     yellow();

#End elif 
#End def


def black():
 color = Color(000,000,000)

def red():
 color = Color(255,000,000)

def blue():
 color = Color("blue")

def green():
 color = Color("Green")

def cyan():
 color = Color("Cyan")

def magenta():
 color = Color("Magenta")

def white():
 color = Color("White")

def yellow():
 color = Color("Yellow")

#Draw the Binary Tree
def binTree(n,l) :
 if n==0 or l<2 : 
    return
#End if
pen.forward(l)
pen.left(45); binTree(n-1, l/2)
pen.right(90); binTree(n-1, l/2); pen.left(45)
pen.backward(l)
color = colorLibrary
#End def

#Draw the Dandelion
def dandelion(n,l) :
 if n==0 or l<2 :
    return
#End if
pen.forward(l)
pen.left(90); dandelion(n-1, l/3)
pen.right(60); dandelion(n-1, l/3)
pen.right(60); dandelion(n-1, l/3)
pen.right(60); dandelion(n-1, l/3)
pen.left(90)
pen.backward(l)
#End def

#Draw the Fern
def fern (n,l) :
 if n==0 or l<2 :
    return
#End if
pen.forward(2*l/3)
pen.right(50); fern(n-1, l/2); pen.left(50)
pen.forward(2*l/3)
pen.left(30); fern(n-1, l/2); pen.right(30)
pen.forward(2*l/3)
pen.right(15); fern(n-1, 0.8*l); pen.left(15)
pen.backward(2*l)
#End def

#Draw the Koch curve
def koch(n,l) :
 if n==0 or l<2 :
    pen.forward(l)
    return
#End if
koch(n-1, l/3); pen.left(60)
koch(n-1, l/3); pen.right(120)
koch(n-1, l/3); pen.left(60)
koch(n-1, l/3)
#End def

#Draw the Snowflake
def flake(n,l) :
for i in range(3) :
    koch(n,l)
    pen.right(120)
#End for
#End def

#Draw the Sierpinski Gasket
def sGasket(n,l) :
if n==0 or l<2 :
    for i in range(3) :
        pen.forward(l)
        pen.left(120)
        return
    #End for
#End if

for i in range(3) :
    sGasket(n-1, l/3)
    pen.forward(l)
    pen.left(120)
#End for
#End def

# Swiss Flag
def swissFlag(n,l):
 if n == 0 or l < 2:
      for i in range(4):
           pen.forward(l)
           pen.left(90)
      #endfor
      return
 #endif

 for i in range(4):
      swissFlag(n - 1, l / 3)
      pen.forward(l)
      pen.left(90)
      #endfor
 #end def

 # Square gasket
 def squareGasket(n,l):
 if n == 0 or l < 2:
      for i in range(4):
           pen.forward(l)
           pen.left(90)
      #endfor
      return
 #endif

 for i in range(4):
     squareGasket(n - 1, l / 3)
     pen.forward(l)
     pen.left(90)
     pen.forward(l / 3);pen.left(90);pen.forward(l / 3);pen.right(90);
     squareGasket(n - 1, l / 3)
     pen.right(90);pen.forward(l / 3);pen.left(90);pen.backward(l/3)
 #endfor
 #end

 # Circle gasket
 def circleGasket(n,l):
 if n == 0 or l<2:
    return
 #endif
 for i in range(2):
    circleGasket(n - 1, l / 2)
    pen.circle(l, 90)
    circleGasket(n - 1, l / 3)
    pen.circle(l, 90)
  #end


 #===================================
 #   Make the interface components
 #===================================

 label = Label(root, text = "Turtle Fractals")
 label.grid(row = 0, column = 1, columnspan = 2)

 fractalLabel = Label(root, text = "Fractal")
 fractalLabel.grid(row = 1, column = 0)

 fractalStr = StringVar()
 fractalEntry = Entry(root, textvariable = fractalStr)
 fractalEntry.grid(row = 1, column = 1)

 libStr = StringVar()
 libOptionMenu = OptionMenu(root, libStr, library[0], *library)
 libOptionMenu.grid(row = 1, column = 3, columnspan = 2)

 colorLibStr = StringVar()
 colorLibOptionMenu = OptionMenu(root, colorLibStr, colorLibrary[0], *colorLibrary)
 colorLibOptionMenu.grid(row = 2, column = 3, columnspan = 2)

 #================

 lengthLabel = Label(root, text = "Length")
 lengthLabel.grid(row = 2, column = 0)

 lengthStr = StringVar()
 lengthEntry = Entry(root, textvariable = lengthStr)
 lengthEntry.grid(row = 2, column = 1)

 clearButton = Button(root, text = "Clear", command = clearF)
 clearButton.grid(row = 3, column = 1, columnspan = 2)

 drawButton = Button(root, text = "Draw", command = drawF)
 drawButton.grid(row = 3, column = 3)

 #=====================Catch Events===================
 root.mainloop()

Apologies for the state of my code. I'm going to clean it up and comment more once I get this bit done. Thanks in advance.

是的,你可以turtle.pencolor()

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