2017-05-21 39 views
0

我想製作可以從tkinter控制的海龜圖形。除了一件事以外,一切正常:enter code here每當我在條目窗口小部件中鍵入數字並單擊按鈕時,它都不起作用(不繪製)。從tkinter無法控制海龜

請幫助我,如果你可以。如果你想給函數傳遞給回調(不是它的返回值),你應該使用

import turtle 
from tkinter import* 
from random import* 

def colorred(): 
    turtle.color("red") 

def colorblue(): 
    turtle.color("blue") 

def colororange(): 
    turtle.color("orange") 

def coloryellow(): 
    turtle.color("yellow") 

def turtleshape(): 
    shapes=["arrow",'turtle', 'circle', 'square', 'triangle', 'classic'] 
    i=randint(0,5) 
    turtle.shape(shapes[int(i)]) 


'''arrow', 'turtle', 'circle', 'square', 'triangle', 'classic'.''' 

root=Tk() 
root.title("palindrome checker") 
frame=Frame(root) 
frame.pack() 
ekr=turtle.Screen() 
turtle.tiltangle(90) 
inp1=IntVar() 
inp2=IntVar() 
inp3=IntVar() 
leng1=inp1.get() 
leng=inp2.get() 
rad=inp3.get() 


def square(distance): 
    print("clicked") 
    turtle.forward(distance) 
    turtle.left(90) 
    turtle.forward(distance) 
    turtle.left(90) 
    turtle.forward(distance) 
    turtle.left(90) 
    turtle.forward(distance) 
    turtle.left(90) 
    turtle.left(5) 

def triangel(length): 
    turtle.forward(length) 
    turtle.left(240) 
    turtle.forward(length) 
    turtle.left(240) 
    turtle.forward(length) 
    turtle.left(60) 
    turtle.left(5) 

def circle(radius): 
    turtle.circle(radius) 
    turtle.left(5) 

red=Button(frame,bg="red",fg="red",width=8,command=colorred) 
red.grid(row=1,column=0) 

blue=Button(frame,bg="blue",fg="blue",width=8,command=colorblue) 
blue.grid(row=1,column=1) 

DRAW=Button(frame,bg="white",fg="green",text="SHAPE",width=8,command=turtleshape) 
DRAW.grid(row=1,column=2) 

orange=Button(frame,bg="orange",fg="orange",width=8,command=colororange) 
orange.grid(row=1,column=3) 

yellow=Button(frame,bg="yellow",fg="yellow",width=8,command=coloryellow) 
yellow.grid(row=1,column=4) 


square=Button(frame,text="square",bg="white",fg="black",width=6,bd=4,command=square(leng1)) 
square.grid(row=2,column=1) 

lab1=Label(frame,text="length-->") 
lab1.grid(row=2,column=2) 

disp1=Entry(frame,textvariable=inp1,width=6,bd=4) 
disp1.grid(row=2,column=3) 

triangle=Button(frame,text="triangle",bg="white",fg="black",width=6,bd=4,command=triangel(leng)) 
triangle.grid(row=3,column=1) 

lab2=Label(frame,text="length-->") 
lab2.grid(row=3,column=2) 

disp2=Entry(frame,textvariable=inp2,width=6,bd=4) 
disp2.grid(row=3,column=3) 

circle=Button(frame,text="circle",bg="white",fg="black",width=6,bd=4,command=circle(rad)) 
circle.grid(row=4,column=1) 

lab3=Label(frame,text="radius-->") 
lab3.grid(row=4,column=2) 

disp3=Entry(frame,textvariable=inp3,width=6,bd=4) 
disp3.grid(row=4,column=3) 


turtle.listen(15,15) 
mainloop() 
+0

對於初學者... turtle.color(「red」)應該按照您用於控制結構的相同標準進行縮進。也許有兩個空格?我建議四個,但這是更多的工作。該縮進必須均勻地應用於所有程序 –

+1

單擊「按鈕」?什麼不畫?請創建一個展示問題的MCVE。請參閱[如何創建最小,完整和可驗證的示例_](https://stackoverflow.com/help/mcve)。 – martineau

回答

0

mysquare=Button(frame,text="square",bg="white",fg="black",width=6,bd=4,command=square) 

也沒有必要將參數傳遞給

這裏的代碼回調函數,你可以使用:

distance=int(disp1.get()) 

因此,代碼應該是這樣的:

import turtle 
from tkinter import * 
from random import * 


def colorred(): 
    turtle.color("red") 


def colorblue(): 
    turtle.color("blue") 


def colororange(): 
    turtle.color("orange") 


def coloryellow(): 
    turtle.color("yellow") 


def turtleshape(): 
    shapes = ["arrow", 'turtle', 'circle', 'square', 'triangle', 'classic'] 
    i = randint(0, 5) 
    turtle.shape(shapes[int(i)]) 


'''arrow', 'turtle', 'circle', 'square', 'triangle', 'classic'.''' 

root = Tk() 
root.title("palindrome checker") 
frame = Frame(root) 
frame.pack() 
ekr = turtle.Screen() 
turtle.tiltangle(90) 
inp1 = IntVar() 
inp2 = IntVar() 
inp3 = IntVar() 
leng1 = inp1.get() 
leng = inp2.get() 
rad = inp3.get() 


def square(): 
    print("clicked") 
    distance = int(disp1.get()) 
    turtle.forward(distance) 
    turtle.left(90) 
    turtle.forward(distance) 
    turtle.left(90) 
    turtle.forward(distance) 
    turtle.left(90) 
    turtle.forward(distance) 
    turtle.left(90) 
    turtle.left(5) 


def triangel(): 
    length = int(disp2.get()) 
    turtle.forward(length) 
    turtle.left(240) 
    turtle.forward(length) 
    turtle.left(240) 
    turtle.forward(length) 
    turtle.left(60) 
    turtle.left(5) 


def circle(): 
    radius = int(disp3.get()) 
    turtle.circle(radius) 
    turtle.left(5) 


red = Button(frame, bg="red", fg="red", width=8, command=colorred) 
red.grid(row=1, column=0) 
blue = Button(frame, bg="blue", fg="blue", width=8, command=colorblue) 
blue.grid(row=1, column=1) 
DRAW = Button(frame, bg="white", fg="green", text="SHAPE", width=8, command=turtleshape) 
DRAW.grid(row=1, column=2) 
orange = Button(frame, bg="orange", fg="orange", width=8, command=colororange) 
orange.grid(row=1, column=3) 
yellow = Button(frame, bg="yellow", fg="yellow", width=8, command=coloryellow) 
yellow.grid(row=1, column=4) 

mysquare = Button(frame, text="square", bg="white", fg="black", width=6, bd=4, command=square) 
mysquare.grid(row=2, column=1) 
lab1 = Label(frame, text="length-->") 
lab1.grid(row=2, column=2) 
disp1 = Entry(frame, textvariable=inp1, width=6, bd=4) 
disp1.grid(row=2, column=3) 
triangle = Button(frame, text="triangle", bg="white", fg="black", width=6, bd=4, command=triangel) 
triangle.grid(row=3, column=1) 
lab2 = Label(frame, text="length-->") 
lab2.grid(row=3, column=2) 
disp2 = Entry(frame, textvariable=inp2, width=6, bd=4) 
disp2.grid(row=3, column=3) 
circle = Button(frame, text="circle", bg="white", fg="black", width=6, bd=4, command=circle) 
circle.grid(row=4, column=1) 
lab3 = Label(frame, text="radius-->") 
lab3.grid(row=4, column=2) 
disp3 = Entry(frame, textvariable=inp3, width=6, bd=4) 
disp3.grid(row=4, column=3) 

turtle.listen(15, 15) 
mainloop()