调整画笔粗细跟选择颜色的下拉菜单项事件怎么不执行了,求检查代码
# -*- 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() 研究了半天,还是没搞明白为什么在事件里 的THICKNESS 和 COLOR 都是初始值
你可以在你原代码上进行print 测试,会发现打印的一直是2,所以我干脆放弃想了,重新帮你写了下,你看看~
# -*- 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()
w = Canvas(root, width=1350, height=700)
w.pack()
THICK = 7
THIN = 2
MEDIUM = 4
THICKNESS = IntVar()
THICKNESS.set(2)
COLOR = StringVar()
COLOR.set("#ff0000")
def paint(event):
x1, y1 = (event.x - THICKNESS.get()), (event.y - THICKNESS.get())
x2, y2 = (event.x + THICKNESS.get()), (event.y + THICKNESS.get())
w.create_oval(x1, y1, x2, y2, fill=COLOR.get())
def thickness(tn):
THICKNESS.set(tn)
def choosecolor():
color = colorchooser.askcolor()
COLOR.set(color)
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()
页:
[1]