-2
のカメペンの色を変更することができます。私はPythonを初めて使い、tkinter GUIを使ってカメを生成するインターフェースを作るプロジェクトを行う必要があります。私たちのマークの20%はインタフェースに個人的な貢献をしていますので、オプションのメニューから選択することで、タートルペンの色を変えたいと思っています。 「バイナリツリー」を選択すると、赤を選択することができ、デフォルトの黒ではなく赤で描画されます。私は今まで8時間の骨を費やして、これを機能させようとしています。誰かが私を助けてくれるのか、それが可能かどうか教えてもらえますか?これまでのコードは次のとおりです。tkinterインターフェース
# 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()
私のコードの状態についてお詫び申し上げます。私はそれをきれいにして、このビットを完了したらもっとコメントします。前もって感謝します。
ドキュメントを読む:https://docs.python.org/2/library/turtle.html#turtle.pencolor – 0TTT0