简体   繁体   English

您可以在 tkinter 界面中更改乌龟笔的颜色吗

[英]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.所以我是 Python 新手,必须做一个项目,在该项目中我们制作一个界面,使用 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.我们的20%的标记为个人贡献提供了对界面的个人贡献,因此我希望通过从选项菜单中选择来改变海龟笔的颜色,例如选择“二进制树”时,您可以选择红色,它将绘制红色而不是默认的黑色。 I'm after spending the bones of 8 hours so far trying to get this to work.到目前为止,我花了 8 个小时的时间试图让它发挥作用。 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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