类型错误:缺少一个必需的位置参数"事件"



我正在制作一个绘画程序,但我一直遇到这个错误但是,如果我更改event.x/event.y的定义位置,则会出现相同的错误,但会使用不同的子例程

这是代码:

class paint:
def __init__(self,root,canvas):
self.root = root
self.mouse_press = False #mouse isnt being pressed
self.canvas = canvas
self.canvas.bind('<ButtonPress-1>',self.MouseDown) # when left click is pressed / down the subroutine MouseDown is opened 
self.canvas.bind('<ButtonRelease-1>',self.MouseUp)#when left click is relased open the MouseUp subroutine         

def MouseDown(self,event):
self.x = event.x
self.y = event.y
self.mouse_press = True # mouse is pressed 
self.poll() #calls the coodinates subroutine 
def MouseUp(self,event):

self.poll()
self.mouse_press = False
self.root.after_cancel(self.after_id)
def poll(self):
if self.mouse_press:

canvas.create_oval(self.x,self.y, self.x+10, self.y+10 , fill = '#f6f65a',outline = '#f6f65a')
self.after_id = self.root.after(10,self.MouseDown)

def colour_pick():
colour_choose = colorchooser.askcolor()[1] #opens a colour picker and picks the colour
print(colour_choose)
return(colour_choose)

我怎么称呼

p = paint(root,canvas)
root.mainloop()

我不断得到的错误:

Traceback (most recent call last):
File "C:UsersuserAppDataLocalProgramsPythonPython310libtkinter__init__.py", line 1921, in __call__
return self.func(*args)
File "C:UsersuserAppDataLocalProgramsPythonPython310libtkinter__init__.py", line 839, in callit
func(*args)
TypeError: paint.MouseDown missing 1 required positional argument: 'event'

您的函数MouseDown需要一个event参数,因为您这样定义它:

def MouseDown(self,event):

但是,当您从after调用函数时,您不会传递以下参数:

self.after_id = self.root.after(10,self.MouseDown)

由于MouseDown使用event对象中的数据,因此需要将对象与函数使用的属性(即:.x.y(合成,或者需要重写MouseDown,使其不需要event参数。

Bryan的回答解释了错误的原因。

对于您的情况,绑定事件<Button-1><B1-Motion>就足够了,不需要使用.after():

class paint:
def __init__(self, root, canvas):
self.root = root
self.canvas = canvas
self.canvas.bind('<Button-1>', self.mouse_down)
self.canvas.bind('<B1-Motion>', self.poll)
def mouse_down(self, event):
# save initial drag point
self.x, self.y = event.x, event.y
def poll(self, event):
# draw line from last saved point to current point instead of drawing circle
self.canvas.create_line(self.x, self.y, event.x, event.y, fill='#f6f65a', width=10, capstyle='round')
# save current point
self.x, self.y = event.x, event.y

你在伪造<Motion>方面太努力了。下面说明了我相信你实际上在努力做什么

import tkinter as tk

class Canvas(tk.Canvas):
def __init__(self, master, **kwargs):
tk.Canvas.__init__(self, master, **{'background':'#000000', 'highlightthickness':0, **kwargs})
self.bind('<Motion>', self.motion)

#instead of attempting to programmatically toggle up and down states
def motion(self, e):
if (e.state & 256) == 256: #if mouse 1 is down
self.create_oval(e.x, e.y, e.x+10, e.y+10, fill='#f6f65a', outline='#f6f65a') #fill and outline should actually come from your ColorChooser

class Root(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.paint = Canvas(self)
self.paint.pack(expand=True, fill=tk.BOTH)

if __name__ == '__main__':
Root().mainloop()

最新更新