谁能告诉我,在这个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)
慕村225694
相关分类