我从代码中收到此错误:motion()
只取1个参数(给定0)。我如何重做它或我需要更改什么才能使我的代码绘制稳定的线或更快地检索鼠标的x和y值。
import Tkinter
from Tkinter import*
root = Tkinter.Tk()
root.wm_title("PY Paint")
master = root
red_intvar = Tkinter.IntVar()
red_intvar.set(127)
blue_intvar = Tkinter.IntVar()
blue_intvar.set(127)
green_intvar = Tkinter.IntVar()
green_intvar.set(127)
r = 5
radius_intvar = Tkinter.IntVar()
radius_intvar.set(5)
mouse_released = True
mouse2_released = True
lines= IntVar()
'''initialize variables for the radius, radius slider, red, green, and blue color value, as well as the canvas'''
def new_radius(radius_intvar):
global r
r = radius_slider.get()
r.append
'''creates a function for changing the radius of the circle when the slider is changed'''
canvas = Canvas(master, width= 1400, height = 800, relief=RAISED, bg='white')
canvas.grid()
Checkbutton(master, text='lines', variable=lines).grid(row=0, stick=W)
'''creates a canvas on wich to draw'''
red_slider = Tkinter.Scale(master, from_=0, to=255, variable=red_intvar,
orient=Tkinter.VERTICAL, label = 'Red')
red_slider.grid(row=0, column=1, sticky=Tkinter.E)
blue_slider = Tkinter.Scale(master, from_=0, to=255, variable=blue_intvar,
orient=Tkinter.VERTICAL, label = 'Blue')
blue_slider.grid(row=0, column=2, sticky=Tkinter.E)
green_slider = Tkinter.Scale(master, from_=0, to=255, variable=green_intvar,
orient=Tkinter.VERTICAL, label = 'Green')
green_slider.grid(row=0, column=3, sticky=Tkinter.E)
radius_slider = Tkinter.Scale(master, from_=1, to=20, variable=radius_intvar,
orient=Tkinter.VERTICAL, label = 'Pen Size', command= new_radius)
radius_slider.grid(row=0, column=4, sticky=Tkinter.E)
'''initializes and lays out the sliders for the color values and radius calue of the circle'''
def clear():
canvas.create_rectangle(0, 0, 1400, 800, fill='white', outline='white')
clear_button = Button(master, text="clear",width=20, command=clear)
clear_button.grid(row=1, column=1)
'''defines and locates a button which is used to clear the canvas'''
def motion(event):
global x
global y
x, y = event.x, event.y
print (x, y)
canvas.after(1,motion)
root.bind('<Motion>', motion)
'''catures and sends back any change in the x and y values of the mouse on the canvas'''
print root.winfo_pointerxy()
def hexstring(slider_intvar):
slider_int = slider_intvar.get()
slider_hex = hex(slider_int)
slider_hex_digits = slider_hex[2:]
if len(slider_hex_digits)==1:
slider_hex_digits = '0' + slider_hex_digits
return slider_hex_digits
'''converts the slider values into hexidecimal digits'''
def pressed(element):
global mouse_released
mouse_released= False
draw()
def released(element):
global mouse_released
mouse_released = True
def draw():
global mouse_released
global x
global y
if (x <= 1400):
if (y <= 800):
if (mouse_released == False):
canvas.create_rectangle(x-(.5*r), y-(.5*r), x+(.5*r), y+(.5*r), fill='#' + hexstring(red_intvar) + hexstring(green_intvar) + hexstring(blue_intvar),\
outline='#' + hexstring(red_intvar) + hexstring(green_intvar) + hexstring(blue_intvar))
canvas.after(1, draw)
'''draws a square of set area at the current positon of the mouse, and loops based on if left click is held'''
def m2_pressed(element):
global mouse2_released
mouse2_released= False
erase()
def m2_released(element):
global mouse2_released
mouse2_released= True
erase()
def erase():
global mouse2_released
if (x <= 1400):
if (y <= 800):
if (mouse2_released == False):
canvas.create_rectangle(x-r, y-r, x+r, y+r, fill='white', outline='white')
canvas.after(1, erase)
'''erases a square of set area at the current positon of the mouse, and loops based on if right click is held'''
canvas.bind('<Button-1>', pressed)
canvas.bind('<ButtonRelease-1>', released)
canvas.bind('<Button-3>', m2_pressed)
canvas.bind('<ButtonRelease-3>', m2_released)
root.mainloop()
答案 0 :(得分:0)
motion
函数中的这一行发生异常:
canvas.after(1,motion)
after
函数没有为回调提供参数。这意味着它在没有motion
参数的情况下调用您的event
函数。
如果我没弄错的话,当你删除该行时,你的应用程序似乎也有效,但我可能错了。