调用画笔粗细跟选择颜色的下拉菜单选项点击后不用运行了,求检查代码
# -*- coding:utf-8 -*-from tkinter import *
from tkinter import colorchooser
import tkinter
win=tkinter.Tk()
win.title("简单小部件!")
win.geometry('250x120+50+10')
label=tkinter.Label(win,\
text='欢迎来到我的世界!',\
font='宋体',\
fg='#0000ff'
)
label.pack()
def eBtnClickme():
root = Tk()
root.title("画图")
var = IntVar()
var.set(1)
w = Canvas(root, width=1350, height=700)
w.pack()
THICK = 7
THIN = 2
MEDIUM = 4
THICKNESS = THIN
COLOR = "#ff0000"
def paint(event):
x1, y1 = (event.x - THICKNESS), (event.y - THICKNESS)
x2, y2 = (event.x + THICKNESS), (event.y + THICKNESS)
w.create_oval(x1, y1, x2, y2, fill=COLOR)
def thickness(tn):
global THICKNESS
THICKNESS = tn
def choosecolor():
global COLOR
COLOR = colorchooser.askcolor()
mb = Menu(root)
thmenu = Menu(mb, tearoff=False)
thmenu.add_radiobutton(variable=var, value=1, label="细",
command=lambda: thickness(THIN))
thmenu.add_radiobutton(variable=var, value=2, label="中",
command=lambda: thickness(MEDIUM))
thmenu.add_radiobutton(variable=var, value=3, label="粗",
command=lambda: thickness(THICK))
mb.add_cascade(label="调整画笔粗细", menu=thmenu)
colormenu = Menu(mb, tearoff=False)
colormenu.add_command(label="调整画笔颜色", command=choosecolor)
mb.add_cascade(label="选择颜色", menu=colormenu)
root.config(menu=mb)
w.bind("<B1-Motion>", paint)
w.bind("<B3-Motion>", paint)
btn1=tkinter. Button (win,text ='画布!',bg='green' , command=eBtnClickme)
btn1.pack()
mainloop() 调节画笔粗细跟选择颜色不能运行,点击后粗细一样,颜色也一样是怎么回事 小诺爷 发表于 2020-6-9 18:48
调节画笔粗细跟选择颜色不能运行,点击后粗细一样,颜色也一样是怎么回事
看你的另一个帖子,以后注意别重复发帖哦! global THICKNESS global COLOR
你变量的作用域弄错了eBtnClickme函数变量的作用域不是global
改法 1, eBtnClickme下声明THICKNESS COLOR 为global
2, thickness choosecolor 使用nonlocal暴露变量给外部函数
页:
[1]