我的绘图代码将不够快速接收值以绘制稳定的线路



我从我的代码中收到此错误: motion()恰好获得1个参数(0给定(。我该如何重新处理它,或需要更改我需要更改以获取稳定的线或检索鼠标的x和y值更快的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 which 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 position 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 position 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()

motion函数中的这一行发生的例外情况:

canvas.after(1,motion)

after函数对回调没有任何参数。这意味着它在没有event参数的情况下调用您的motion功能。

如果我没记错的话,似乎当您删除该行时您的应用程序也有效,但我可能错了。

最新更新