猿问

为什么它在python中分配之前显示引用?

谁能告诉我,在这个Python程序中我做错了什么?


我正在尝试创建一个类似“画图”的应用程序,但它显示错误。


文件“E:\项目\新建文件夹\paint_new.py”,第 33 行,use_eraser


activate_button(eraser_button, eraser_mode=True)

文件“E:\项目\新建文件夹\paint_new.py”,第 36 行,activate_button


active_button.config(relief=RAISED)

未绑定本地错误:赋值前引用的局部变量“active_button”


未定义名称“eraser_on”


from tkinter import *

from tkinter.colorchooser import askcolor

import tkinter as tk




DEFAULT_PEN_SIZE = 5.0

DEFAULT_COLOR = 'black'




def setup():

    old_x = None

    old_y = None

    line_width = choose_size_button.get()

    color = DEFAULT_COLOR

    eraser_on = False

    active_button = pen_button

    c.bind('<B1-Motion>', paint)

    c.bind('<ButtonRelease-1>', reset)


def use_pen():

    activate_button(pen_button)


def use_brush():

    activate_button(brush_button)


def choose_color():

    eraser_on = False

    color = askcolor(color=color)[1]


def use_eraser():

    activate_button(eraser_button, eraser_mode=True)


def activate_button(some_button, eraser_mode=False):

    active_button.config(relief=RAISED)

    some_button.config(relief=SUNKEN)

    active_button = some_button

    eraser_on = eraser_mode


def paint(event):

    line_width = choose_size_button.get()

    paint_color = 'white' if eraser_on else color

    if old_x and old_y:

        c.create_line(old_x, old_y, event.x, event.y,

                           width=line_width, fill=paint_color,

                           capstyle=ROUND, smooth=TRUE, splinesteps=36)

    old_x = event.x

    old_y = event.y


def reset(event):

    old_x, old_y = None, None


root = tk.Tk()


pen_button = Button(root, text='pen', command=use_pen)

pen_button.grid(row=0, column=0)


brush_button = Button(root, text='brush', command=use_brush)

brush_button.grid(row=0, column=1)


color_button = Button(root, text='color', command=choose_color)

color_button.grid(row=0, column=2)


eraser_button = Button(root, text='eraser', command=use_eraser)

eraser_button.grid(row=0, column=3)



慕容708150
浏览 123回答 1
1回答

慕村225694

明智的解决方案是使用sth。喜欢这个:from tkinter import *from tkinter.colorchooser import askcolorimport tkinter as tkDEFAULT_PEN_SIZE = 5.0DEFAULT_COLOR = 'black'class ABC:&nbsp; &nbsp; def setup(self):&nbsp; &nbsp; &nbsp; &nbsp; self.old_x = None&nbsp; &nbsp; &nbsp; &nbsp; self.old_y = None&nbsp; &nbsp; &nbsp; &nbsp; self.line_width = self.choose_size_button.get()&nbsp; &nbsp; &nbsp; &nbsp; self.color = DEFAULT_COLOR&nbsp; &nbsp; &nbsp; &nbsp; self.eraser_on = False&nbsp; &nbsp; &nbsp; &nbsp; self.active_button = self.pen_button&nbsp; &nbsp; &nbsp; &nbsp; self.c.bind('<B1-Motion>', self.paint)&nbsp; &nbsp; &nbsp; &nbsp; self.c.bind('<ButtonRelease-1>', self.reset)&nbsp; &nbsp; def use_pen(self):&nbsp; &nbsp; &nbsp; &nbsp; self.activate_button(self.pen_button)&nbsp; &nbsp; def use_brush(self):&nbsp; &nbsp; &nbsp; &nbsp; self.activate_button(self.brush_button)&nbsp; &nbsp; def choose_color(self):&nbsp; &nbsp; &nbsp; &nbsp; self.eraser_on = False&nbsp; &nbsp; &nbsp; &nbsp; self.color = askcolor(color=self.color)[1]&nbsp; &nbsp; def use_eraser(self):&nbsp; &nbsp; &nbsp; &nbsp; self.activate_button(self.eraser_button, eraser_mode=True)&nbsp; &nbsp; def activate_button(self, some_button, eraser_mode=False):&nbsp; &nbsp; &nbsp; &nbsp; self.active_button.config(relief=RAISED)&nbsp; &nbsp; &nbsp; &nbsp; some_button.config(relief=SUNKEN)&nbsp; &nbsp; &nbsp; &nbsp; self.active_button = some_button&nbsp; &nbsp; &nbsp; &nbsp; self.eraser_on = eraser_mode&nbsp; &nbsp; def paint(self, event):&nbsp; &nbsp; &nbsp; &nbsp; self.line_width = self.choose_size_button.get()&nbsp; &nbsp; &nbsp; &nbsp; self.paint_color = 'white' if self.eraser_on else self.color&nbsp; &nbsp; &nbsp; &nbsp; if self.old_x and self.old_y:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.c.create_line(self.old_x, self.old_y, event.x, event.y,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;width=self.line_width, fill=self.paint_color,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;capstyle=ROUND, smooth=TRUE, splinesteps=36)&nbsp; &nbsp; &nbsp; &nbsp; self.old_x = event.x&nbsp; &nbsp; &nbsp; &nbsp; self.old_y = event.y&nbsp; &nbsp; def reset(self, event):&nbsp; &nbsp; &nbsp; &nbsp; self.old_x, self.old_y = None, None&nbsp; &nbsp; def __init__(self):&nbsp; &nbsp; &nbsp; &nbsp; self.root = tk.Tk()&nbsp; &nbsp; &nbsp; &nbsp; self.pen_button = Button(self.root, text='pen', command=self.use_pen)&nbsp; &nbsp; &nbsp; &nbsp; self.pen_button.grid(row=0, column=0)&nbsp; &nbsp; &nbsp; &nbsp; self.brush_button = Button(self.root, text='brush', command=self.use_brush)&nbsp; &nbsp; &nbsp; &nbsp; self.brush_button.grid(row=0, column=1)&nbsp; &nbsp; &nbsp; &nbsp; self.color_button = Button(self.root, text='color', command=self.choose_color)&nbsp; &nbsp; &nbsp; &nbsp; self.color_button.grid(row=0, column=2)&nbsp; &nbsp; &nbsp; &nbsp; self.eraser_button = Button(self.root, text='eraser', command=self.use_eraser)&nbsp; &nbsp; &nbsp; &nbsp; self.eraser_button.grid(row=0, column=3)&nbsp; &nbsp; &nbsp; &nbsp; self.choose_size_button = Scale(self.root, from_=1, to=10, orient=HORIZONTAL)&nbsp; &nbsp; &nbsp; &nbsp; self.choose_size_button.grid(row=0, column=4)&nbsp; &nbsp; &nbsp; &nbsp; self.c = Canvas(self.root, bg='white', width=600, height=600)&nbsp; &nbsp; &nbsp; &nbsp; self.c.grid(row=1, columnspan=5)&nbsp; &nbsp; &nbsp; &nbsp; self.setup()&nbsp; &nbsp; &nbsp; &nbsp; self.root.mainloop()ABC()学习简单的 OOP 原则!希望对您有所帮助!
随时随地看视频慕课网APP

相关分类

Python
我要回答