所以我是Python新手,必须做一个项目,我们使用tkinter GUI创建一个生成龟图的界面。我们20%的分数用于个人对界面的贡献,因此我希望能够通过从选项菜单中选择来改变乌龟笔的颜色,例如:当'二叉树'如果选中,您可以选择红色,它将以红色而不是默认黑色绘制。到目前为止,我花了8个小时的骨头试图让它发挥作用。有人可以帮助我或告诉我它是否可以做到?到目前为止,这是我的代码:
# 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()
为我的代码状态道歉。一旦我完成这项工作,我就会清理它并发表更多评论。提前谢谢。
答案 0 :(得分:0)
是的,你可以turtle.pencolor()
。