2017-10-28 122 views
-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() 

道歉爲我的代碼狀態。一旦我完成了這一步,我會清理它並發表更多評論。提前致謝。

+1

閱讀文檔:https://docs.python.org/2/library/turtle.html#turtle.pencolor – 0TTT0

回答

0

是的,你可以turtle.pencolor()