-3

I got an error at this code: The error is in my function settings() in the Button() command. but I don't got any plan how to fix it, sorry. I can't put the 3 commands in an external function, cause it wouldn't get the variables...

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()

Pls tell me what I've done wrong // fix it pls.

Chris
  • 112,704
  • 77
  • 249
  • 231
  • 2
    What's the specific problem? Do you get an error message, or a stack trace? What do you expect to happen? What's happening instead? – Chris Sep 05 '14 at 17:05
  • 2
    Also, your formatting is *horrendous*. This code is impossible to read. Have a look at [PEP 8](http://legacy.python.org/dev/peps/pep-0008/), Python's official style guide. – Chris Sep 05 '14 at 17:06
  • I got an error message. I wanted to change the turtles Shape, Shapesize and Pensize in one Button command. The pensize doesn't change, and an error message comes up: "TypeError: unsupported operand type(s) for &: 'NoneType' and 'NoneType'" – ProgrammingDonkey Sep 05 '14 at 17:06
  • 1
    _"I can't put the 3 commands in an external function cause it wouldn't get the variables"_ [Python has closure](http://stackoverflow.com/a/4020443/2363712), you know. – Sylvain Leroux Sep 05 '14 at 17:07
  • I got the error just in this line. Pls look for it: "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)" – ProgrammingDonkey Sep 05 '14 at 17:08
  • 2
    Try looking up what the [`&` operator](https://docs.python.org/3/library/stdtypes.html#index-17) actually does. You probably want to do something else. – Chris Sep 05 '14 at 17:10
  • Just wanna get a variable of one function into another function... – ProgrammingDonkey Sep 05 '14 at 17:12
  • Ok. lets try a different symbol.. – ProgrammingDonkey Sep 05 '14 at 17:13
  • Doesn't work with '^' – ProgrammingDonkey Sep 05 '14 at 17:15
  • @ProgrammingDonkey, did you just randomly pick another operator from the bitwise section of the documentation? The reason I pointed you to the docs was so that you'd *read them*. Bitwise operators are not what you want here. – Chris Sep 05 '14 at 17:22

1 Answers1

1

If you're trying to string together multiple expressions using the & operator, it isn't likely to work well, unless all of your function calls return integers, which isn't the case here. I don't recommend it, but you can put each command as a separate element of a collection such as a list or tuple:

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

Ordinarily, this is true. But if you define the second function inside the first, all of its variables will still be visible.

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)
Kevin
  • 72,202
  • 12
  • 116
  • 152