我试图创建一个草图蚀刻但似乎得到相同的错误进出

时间:2014-11-19 20:39:55

标签: python tkinter

代码是:

from tkinter import *
import tkinter as tk

global colour
global colourselection 
global count 

canvas_height = 700
canvas_width = 900
canvas_colour = "black"



p1_x = canvas_width/2
p1_y = canvas_height
line_width = 5
line_length = 5



def colour(self):
    global p1_colour
    p1_colour = 'green'
    colour = ['green', 'blue', 'yellow', 'red', 'orange', 'purple', 'white', 'grey']
    colourSelector = colour[levels]
    levels = levels + 1

def p1_move_N(self):
    global p1_y
    canvas.create_line(p1_x, p1_y, p1_x, (p1_y-line_length), width=line_width, fill=colour(self))
    p1_y = p1_y - line_length

def p1_move_S(self):
    global p1_y
    canvas.create_line(p1_x, p1_y, p1_x, (p1_y+line_length), width=line_width, fill=colour(self))
    p1_y = p1_y + line_length

def p1_move_E(self):
    global p1_x
    canvas.create_line(p1_x, p1_y, p1_x - line_length, p1_y , width=line_width, fill=colour(self))
    p1_x = p1_x + line_length

def p1_move_W(self):
    global p1_x
    canvas.create_line(p1_x, p1_y, p1_x + line_length, p1_y, width = line_width, fill=colour(self))
    p1_x = p1_x - line_length



#####main:
window = Tk()
window.title("Tom's")
canvas = Canvas(bg = canvas_colour, height = canvas_height, width = canvas_width, highlightthickness=0)
canvas.pack()


window.bind("w", p1_move_N)
window.bind("s", p1_move_S)
window.bind("d", p1_move_E)
window.bind("a", p1_move_W)
window.bind("c", colour)

window.mainloop()

我每次似乎都接受的错误是局部变量'水平'在分配之前引用。每次按键时,我都会尝试改变颜色。被压了。 非常感谢所有帮助。

2 个答案:

答案 0 :(得分:0)

您永远不会在任何地方声明级别,因此代码在未分配时会尝试读取它。只需在代码顶部附近使level = 0。

答案 1 :(得分:0)

我需要这段代码,很感激您编写入门脚本,我做了一些小的编辑,并修复了上面消息中没有提到的问题,但如果您想使用它,这里涉及的是新脚本。

ps。它在python中只是不知道如何发布,因此只需将其复制并粘贴到pycharm或其他内容中就可以了

from tkinter import *
import tkinter as tk

global colour
global colourselection
global count

canvas_height = 700
canvas_width = 900
canvas_colour = "white"



p1_x = canvas_width/2
p1_y = canvas_height
line_width = 5
line_length = 5



def colour(self):
    global p1_colour, levels
    levels=0
    p1_colour = 'green'
    colour = ['green', 'blue', 'yellow', 'red', 'orange', 'purple', 'white', 'grey']
    colourSelector = colour[levels]
    levels += 1

def p1_move_N(self):
    global p1_y
    canvas.create_line(p1_x, p1_y, p1_x, (p1_y-line_length), width=line_width, fill=colour(self))
    p1_y = p1_y - line_length

def p1_move_S(self):
    global p1_y
    canvas.create_line(p1_x, p1_y, p1_x, (p1_y+line_length), width=line_width, fill=colour(self))
    p1_y = p1_y + line_length

def p1_move_E(self):
    global p1_x
    canvas.create_line(p1_x, p1_y, p1_x - line_length, p1_y , width=line_width, fill=colour(self))
    p1_x = p1_x + line_length

def p1_move_W(self):
    global p1_x
    canvas.create_line(p1_x, p1_y, p1_x + line_length, p1_y, width = line_width, fill=colour(self))
    p1_x = p1_x - line_length



#####main:
window = Tk()
window.title("etch a sketch")
canvas = Canvas(bg = canvas_colour, height = canvas_height, width = canvas_width, highlightthickness=0)
canvas.pack()


window.bind("w", p1_move_N)
window.bind("s", p1_move_S)
window.bind("d", p1_move_E)
window.bind("a", p1_move_W)
window.bind("c", colour)

window.mainloop()