2014-09-05 128 views
-3

我在這段代碼中遇到錯誤: 錯誤發生在我的函數settings()中的Button()命令中。但我沒有任何計劃如何解決它,對不起。我不能把3個命令在外部功能,因爲它不會得到變量...Python:按鈕命令+'&'

from turtle import * 
from tkinter import * 
reset() 
hastrail = 1 
def moveup(): 
    setheading(90) 
    forward(5) 
def movedown(): 
    setheading(270) 
    forward(5) 
def moveright(): 
    setheading(0) 
    forward(5) 
def moveleft(): 
    setheading(180) 
    forward(5) 
def turnleft(): 
    left(18) 
def turnright(): 
    right(18) 
def forw(): 
    forward(5) 
def backw(): 
    backward(5) 
def trailrem(): 
    global hastrail 
    if hastrail == 1: 
     penup() 
     hastrail = 0 
    else: 
     pendown() 
     hastrail = 1 
def settings(): 
    color(str(colorchooser.askcolor(title = "Change a line color")[1]),str(colorchooser.askcolor(title = "Change a fill color")[1])) 
    tk = Tk() 
    tk.resizable(0,0) 
    tk.title("Shape, Shapesize, Pensize") 
    tk.geometry("400x90") 
    listbox = Listbox(tk) 
    listbox.place(x=0,y=0,width=200,height=90) 
    listbox.insert(1,"arrow") 
    listbox.insert(2,"turtle") 
    listbox.insert(3,"circle") 
    listbox.insert(4,"square") 
    listbox.insert(5,"triangle") 
    shsi = Scale(tk,width = 10,orient = HORIZONTAL) 
    shsi.place(x=200,y=0,width=200,height=30) 
    trsi = Scale(tk,width = 10, orient = HORIZONTAL) 
    trsi.place(x=200,y=30,width=200,height=30) 
    Button(tk,text="Save",command = lambda:shape(str(listbox.get(ACTIVE)))&shapesize(int(shsi.get()))&pensize(int(trsi.get()))).place(x=200,y=60,width=200,height=30) 

onkeypress(moveup,"Up") 
onkeypress(movedown,"Down") 
onkeypress(moveright,"Right") 
onkeypress(moveleft,"Left") 
onkeypress(turnleft,"a") 
onkeypress(turnright,"d") 
onkeypress(forw,"w") 
onkeypress(backw,"s") 
onkeypress(trailrem,"t") 
onkeypress(settings,"c") 
listen() 
mainloop() 

請告訴我什麼,我做錯了//修復它請。

+2

有什麼具體問題?你是否收到錯誤消息或堆棧跟蹤?你期望發生什麼?發生了什麼呢? – Chris 2014-09-05 17:05:01

+2

另外,你的格式是* horrendous *。這段代碼是不可讀的。看看Python的官方風格指南[PEP 8](http://legacy.python.org/dev/peps/pep-0008/)。 – Chris 2014-09-05 17:06:26

+0

我收到了一條錯誤消息。我想在一個按鈕命令中更改海龜的Shape,Shapesize和Pensize。 pensize不會改變,並且出現錯誤消息:「TypeError:不支持的操作數類型爲&:'NoneType'和'NoneType'」 – ProgrammingDonkey 2014-09-05 17:06:54

回答

1

如果您嘗試使用&運算符將多個表達式串聯在一起,除非您的所有函數調用都返回整數,否則它不太可能正常工作,除非您的所有函數調用都返回整數,但在此情況並非如此。我不建議這樣做,但你可以把每一個命令集合的獨立元素,如列表或元組:

Button(tk,text="Save",command = lambda:[ 
    shape(str(listbox.get(ACTIVE))), 
    shapesize(int(shsi.get())), 
    pensize(int(trsi.get())) 
]).place(x=200,y=60,width=200,height=30) 

I can't put the 3 commands in an external function, cause it wouldn't get the variables

通常情況下,這是事實。但是如果你在裏定義了第二個函數,那麼它的所有變量仍然是可見的。

def settings(): 
    def save_button_clicked(): 
     shape(str(listbox.get(ACTIVE))) 
     shapesize(int(shsi.get())) 
     pensize(int(trsi.get())) 
    #rest of `settings` code goes here... 
    Button(tk,text="Save",command = save_button_clicked).place(x=200,y=60,width=200,height=30) 
+0

非常感謝你; ) – ProgrammingDonkey 2014-09-05 17:22:21