2017-03-06 2 views
2

Im Code unten heißt es: „SyntaxWarning: name‚Farbe‘zugeordnet ist, bevor globale Deklaration globale Farbe“ Allerdings erklärte ich globale Farb bevor ich es zuweisen? Ich bin sehr verwirrt. Ich lief es und es funktioniert, aber ich verstehe einfach nicht, was die Syntax Warnung zeigt auf ...SyntaxWarning: name ‚Farbe‘ zugeordnet ist, bevor globale Deklaration globalen Farb Python

from Tkinter import * 
from sys  import exit 
from random import * 

color = "black" #Sets default color to black 
w,h=640,480 #Width, height of canvas 
root=Tk() 
pixelcount = 0 #Sets the inital pixelcount to 1 
tool = 1 #Sets deafu 
ptX, ptY, ptX2, ptY2 = 0, 0, 0, 0 
cnvs=Canvas(root,width=w,height=h,bg='#D2B48C') # 210 180 140 
cnvs.pack(side = RIGHT) 
buttons = Frame(root, width = 80, height = h) #Creates region for buttons 
buttons.pack(side = LEFT) #Put button region on left 

def quit(evt): 
    exit(0)  

def menu(arg): #Accepts arguments from button clicks and binds appropriate stimulus to appropriate tool function 
    print arg 
    global tool 
    cnvs.unbind('<Button-1>') 
    if arg == 1: 
     cnvs.bind('<Button-1>', line) 
    elif arg == 2: 
     cnvs.bind('<Button-1>', poly) 
    elif arg == 3: 
     cnvs.bind('<Button-1>', rect) 
    elif arg == 4: 
     cnvs.bind('<B1-Motion>', pencil) 
    elif arg == 5: 
     cnvs.bind('<B1-Motion>', spray) 
    elif arg == 6: 
     cnvs.bind('<B1-Motion>', blotter) 
    elif arg == 7: 
     global color 
     color = "red" 
    elif arg == 8: 
     global color 
     color = "black" 
    elif arg == 9: 
     global color 
     color = "blue" 
    elif arg == 10: 
     global color 
     color = "purple" 

def line(evt): #Line function 
    global pixelcount 
    global color 
    pixelcount += 1 
    if pixelcount % 2 == 1: 
     ptX, ptY, = (evt.x, evt.y) 
     global ptX, ptY 
     print ptX, ptY 

    else: 
     ptX2, ptY2, = (evt.x, evt.y) 
     cnvs.create_line(ptX, ptY, ptX2, ptY2, fill = color) 

def lineButtonClick(): #Activated when line button clicked 
    menu(1) 

lineButton = Button(root, text = "line", command = lineButtonClick) 
lineButton.pack() 
lineButton.config(width = 10) 

def poly(evt): #Poly function 

    global pixelcount 
    pixelcount += 1 
    global color 
    print str(pixelcount) + "pixel" 
    if pixelcount == 1: 
     global ptX, ptY 
     ptX, ptY, = (evt.x, evt.y) 
     print ptX, ptY 

    else: 
     global ptX2, ptY2 
     ptX2, ptY2, = (evt.x, evt.y) 
     print str(ptX2) + " " + " " +str(ptY2) + "pt2" 
     cnvs.create_line(ptX, ptY, ptX2, ptY2, fill = color) 
     ptX, ptY = ptX2, ptY2 

def polyButtonClick(): #Activated when poly button clicked 

    menu(2) 
polyButton = Button(root, text = "poly", command = polyButtonClick) 
polyButton.pack() 
polyButton.config(width = 10) 

def rect(evt): #Rectangle function 
    global pixelcount 
    if pixelcount % 2 == 0: 
     global ptX, ptY  
     ptX, ptY, = (evt.x, evt.y) 
     print ptX, ptY 
     pixelcount += 1 

    else: 
     global ptX2, ptY2 
     ptX2, ptY2, = (evt.x, evt.y) 
     pixelcount += 1 
     cnvs.create_rectangle(ptX, ptY, ptX2, ptY2, fill = color, outline = color) 

def rectButtonClick(): #Activated when rectangle button clicked 

    menu(3) 
rectButton = Button(root, text = "rect", command = rectButtonClick) 
rectButton.pack() 
rectButton.config(width = 10) 

def pencil(evt):#Pencil function 
    global pixelcount 
    if cnvs.bind('<ButtonRelease-1>'): 
     pixelcount = 0 
    pixelcount += 1 
    print str(pixelcount) + "pixel" 
    if pixelcount == 1: 
     global ptX, ptY 
     ptX, ptY, = (evt.x, evt.y) 
     print ptX, ptY 

    else: 
     global ptX2, ptY2 
     ptX2, ptY2, = (evt.x, evt.y) 
     print str(ptX2) + " " + " " +str(ptY2) + "pt2" 
     cnvs.create_line(ptX, ptY, ptX2, ptY2, fill = color) 
     ptX, ptY = ptX2, ptY2 


def pencilButtonClick(): 
    menu(4) 
pencilButton = Button(root, text = "pencil", command = pencilButtonClick) 
pencilButton.pack() 
pencilButton.config(width = 10) 

def spray(evt): #Spray function 
    global pixelcount 
    if cnvs.bind('<ButtonRelease-1>'): 
     pixelcount = 0 
    pixelcount += 1 
    print str(pixelcount) + "pixel" 
    ptX, ptY, = (evt.x, evt.y) 
    randomX = evt.x + randint(-10, 10) 
    randomY = evt.y + randint(-10, 10) 
    cnvs.create_oval(randomX -1, randomY-1, randomX + 1, randomY + 1, fill = color) 

def sprayButtonClick():#Activated when spray button clicked 

    menu(5) 
sprayButton = Button(root, text = "spray", command = sprayButtonClick) 
sprayButton.pack() 
sprayButton.config(width = 10) 

def blotter(evt): #Blotter function 
    global pixelcount 
    if cnvs.bind('<ButtonRelease-1>'): 
     pixelcount = 0 
    pixelcount += 1 
    print str(pixelcount) + "pixel" 
    ptX, ptY, = (evt.x, evt.y) 
    cnvs.create_oval(ptX-5, ptY-5,ptX + 5, ptY + 5, fill = color) 

def blotterButtonClick():#Activated when blotter button clicked 

    menu(6) 
blotterButton = Button(root, text = "blotter", command = blotterButtonClick) 
blotterButton.pack() 
blotterButton.config(width = 10) 

def red(): #Red color function 
    menu(7) 
redButton = Button(root, text = "red", command = red) 
redButton.pack() 
redButton.config(width = 10) 

def black(): #Black color function 
    menu(8) 
blackButton = Button(root, text = "black", command = black) 
blackButton.pack() 
blackButton.config(width = 10) 

def blue(): #Blue color function 
    menu(9) 
blueButton = Button(root, text = "blue", command = blue) 
blueButton.pack() 
blueButton.config(width = 10) 

def purple(): #Purple color function 
    menu(10) 
purpleButton = Button(root, text = "purple", command = purple) 
purpleButton.pack() 
purpleButton.config(width = 10) 

mainloop() 

Thank you so much !!!

Antwort

5

Sie geben keine global Erklärung unmittelbar vor jeder Verwendung der Variablen; Sie es verwenden einmal, am Anfang der Funktion, in der die Variable global deklariert ist:

def menu(arg): 
    global tool 
    global color 

    cnvs.unbind('<Button-1>') 
    if arg == 1: 
     cnvs.bind('<Button-1>', line) 
    elif arg == 2: 
     cnvs.bind('<Button-1>', poly) 
    elif arg == 3: 
     cnvs.bind('<Button-1>', rect) 
    elif arg == 4: 
     cnvs.bind('<B1-Motion>', pencil) 
    elif arg == 5: 
     cnvs.bind('<B1-Motion>', spray) 
    elif arg == 6: 
     cnvs.bind('<B1-Motion>', blotter) 
    elif arg == 7: 
     color = "red" 
    elif arg == 8: 
     color = "black" 
    elif arg == 9: 
     color = "blue" 
    elif arg == 10: 
     color = "purple" 
+0

Ich sehe, vielen Dank für die Klarstellung! –

Verwandte Themen