Python 使用 lambda 函数错误缺少 1 个必需的位置参数:“事件”

Posted

技术标签:

【中文标题】Python 使用 lambda 函数错误缺少 1 个必需的位置参数:“事件”【英文标题】:Python using lamda function error missing 1 required positional argument: 'event' 【发布时间】:2021-05-18 11:44:14 【问题描述】:

我有以下程序

from tkinter import *
import tkinter as tk


class Grilla:
    colorCelda = "black"
colorDefault = "white"
colorBorde = "black"
bordeDefault = "black"

def __init__(self, root, master, x, y, size):
    """ Constructor of the object called by Cell(...) """
    self.master = master
    self.abs = x
    self.ord = y
    self.size = size
    self.fill = False

def switch(self):
    """ Switch if the cell is filled or not. """
    self.fill = not self.fill

def draw(self):
    # dibujar en el canvas
    if self.master is not None:
        outline = Grilla.colorBorde
        fill = Grilla.colorCelda

        if not self.fill:
            outline = Grilla.bordeDefault
            fill = Grilla.colorDefault

        xmin = self.abs * self.size
        xmax = xmin + self.size
        ymin = self.ord * self.size
        ymax = ymin + self.size

        self.master.create_rectangle(xmin, ymin, xmax, ymax, fill=fill, outline=outline)


class CellGrilla(Canvas):
    def __init__(self, master, numFil, numCol, tamGrid, *args, **kwargs):
        Canvas.__init__(self, master, width=tamGrid * numCol, height=tamGrid * numFil, *args, **kwargs)
    self.button1 = tk.Button(self, command=lambda: self.button1_clicked)
    self.cellSize = tamGrid

    self._grid = []
    for row in range(numFil):

        line = []
        for column in range(numCol):
            line.append(Grilla(master, self, column, row, tamGrid))

        self._grid.append(line)

    # memorize the cells that have been modified to avoid many switching of state during mouse motion.
    self.switched = []

    # bind click action
    # self.bind("<Button-1>", self.button1_clicked)

    self.draw()

def draw(self):
    for row in self._grid:
        for cell in row:
            cell.draw()

def _coordenadas(self, event):
    row = int(event.y / self.cellSize)
    column = int(event.x / self.cellSize)
    return row, column

def button1_clicked(self, event):
    self.bind("<Button-1>", self.button1_clicked)
    row, column = self._coordenadas(event)
    cell = self._grid[row][column]
    cell.switch()
    cell.draw()
    pass

def button2_clicked(self):
    pass

def button3_clicked(self):
    pass

def button4_clicked(self):
    pass


if __name__ == "__main__":
app = Tk()

# Tamaño de canvas x tamaño de pixeles
grid = CellGrilla(app, 75, 75, 10)

grid.grid(row=1, column=1, rowspan=4, sticky="news")

boton1 = Button(app, text="Dibujar", command=grid.button1_clicked, height=1, width=30)
boton2 = Button(app, text="DDA", command=grid.button2_clicked, height=1, width=30)
boton3 = Button(app, text="Zoom in", command=grid.button3_clicked, height=1, width=30)
boton4 = Button(app, text="Zoom out", command=grid.button3_clicked, height=1, width=30)
boton5 = Button(app, text="Bresenham", command=grid.button3_clicked, height=1, width=30)
boton6 = Button(app, text="Clear", command=grid.button4_clicked, height=1, width=30)

boton1.grid(row=1, column=2, sticky="news")
boton2.grid(row=2, column=2, sticky="news")
boton3.grid(row=3, column=2, sticky="news")
boton4.grid(row=4, column=2, sticky="news")
boton5.grid(row=5, column=2, sticky="news")
boton6.grid(row=6, column=2, sticky="news")

app.mainloop()

现在我正试图弄清楚如何使第一个按钮起作用,当没有按下任何东西并且我按下网格时,什么也没有发生。当我按下“Dibujar”按钮时,它应该能够在网格上绘制,但是我尝试实现 de“if else”功能,但我无法弄清楚如何正确地做到这一点。我一直在尝试用“lambda”来解决这个问题,但是在按下第一个按钮时出现以下错误

Tkinter 回调异常

Traceback(最近一次调用最后一次):

文件“E:\Python\lib\tkinter_init_.py”,第 1884 行,在 call

返回 self.func(*args)

TypeError: button1_clicked() 缺少 1 个必需的位置参数: '事件'

我不确定自己做错了什么,这是我第一次使用 python 编程,所以如果有人能帮我解决这个问题,我将不胜感激。

【问题讨论】:

这是一个棘手的问题。在您对button1_clicked(self, event) 的定义中,您指定它将事件作为参数。问题是按钮不会创建这样的事件。 按钮的回调在被调用时没有传递任何参数。获取事件的回调类型是当您通过调用bind 将其绑定到小部件时,类似于您在回调本身中的回调。通常,人们不会在回调中调用bind,而是在小部件的初始化中调用。如果您在触发回调时尝试更改小部件的行为,您只会在回调中添加 bind 调用。 【参考方案1】:

看看这段代码:

from tkinter import *
import tkinter as tk


class Grilla:
    colorCelda = "black"
    colorDefault = "white"
    colorBorde = "black"
    bordeDefault = "black"

    def __init__(self, root, master, x, y, size):
        """ Constructor of the object called by Cell(...) """
        self.master = master
        self.abs = x
        self.ord = y
        self.size = size
        self.fill = False

    def switch(self):
        """ Switch if the cell is filled or not. """
        self.fill = not self.fill

    def reset(self):
        """ Clears the cell """
        self.fill = False

    def draw(self):
        # dibujar en el canvas
        if self.master is not None:
            outline = Grilla.colorBorde
            fill = Grilla.colorCelda

            if not self.fill:
                outline = Grilla.bordeDefault
                fill = Grilla.colorDefault

            xmin = self.abs * self.size
            xmax = xmin + self.size
            ymin = self.ord * self.size
            ymax = ymin + self.size

            self.master.create_rectangle(xmin, ymin, xmax, ymax, fill=fill, outline=outline)


class CellGrilla(Canvas):
    def __init__(self, master, numFil, numCol, tamGrid, *args, **kwargs):
        Canvas.__init__(self, master, width=tamGrid * numCol, height=tamGrid * numFil, *args, **kwargs)
        self.bind("<Button-1>", self.square_clicked)
        self.cellSize = tamGrid

        self.pen_type = "draw"

        self._grid = []
        for row in range(numFil):

            line = []
            for column in range(numCol):
                line.append(Grilla(master, self, column, row, tamGrid))

            self._grid.append(line)

        # memorize the cells that have been modified to avoid many switching of state during mouse motion.
        self.switched = []

        self.draw()

    def square_clicked(self, event):
        row, column = self._coordenadas(event)
        cell = self._grid[row][column]
        if self.pen_type == "draw":
            cell.switch()
        elif self.pen_type == "rubber":
            cell.reset()
        cell.draw()

    def draw(self):
        for row in self._grid:
            for cell in row:
                cell.draw()

    def _coordenadas(self, event):
        # `int(a / b)` is the same as `a // b`
        row = event.y // self.cellSize
        column = event.x // self.cellSize
        return row, column

    def switch_to_draw(self):
        self.pen_type = "draw"

    def switch_to_rubber(self):
        self.pen_type = "rubber"

    def clear(self):
        for row in self._grid:
            for cell in row:
                cell.reset()
                cell.draw()


if __name__ == "__main__":
    app = Tk()

    # Tamaño de canvas x tamaño de pixeles
    grid = CellGrilla(app, 75, 75, 10)

    grid.grid(row=1, column=1, rowspan=4, sticky="news")

    draw_btn = Button(app, text="Draw", command=grid.switch_to_draw, height=1, width=30)
    rubber_btn = Button(app, text="Rubber/Eraser", command=grid.switch_to_rubber, height=1, width=30)
    reset_btn = Button(app, text="Clear everything", command=grid.clear, height=1, width=30)

    draw_btn.grid(row=1, column=2, sticky="news")
    rubber_btn.grid(row=2, column=2, sticky="news")
    reset_btn.grid(row=3, column=2, sticky="news")

    app.mainloop()

我添加了一个变量来存储用户正在做的事情pen_type。稍后您可以通过更改pen_type 的值来添加颜色。

【讨论】:

【参考方案2】:

根据您对button1_clicked 的定义,它具有强制event 参数。但是在方法调用期间没有传递任何内容。要么删除它,要么通过分配默认值使其成为可选。

def button1_clicked(self, event=None):
    ...

UPD:正如TheLizzard 提到的,event 参数在方法内部使用。所以,它应该总是通过。

【讨论】:

event 在方法内部使用,因此您不能将其设为可选。

以上是关于Python 使用 lambda 函数错误缺少 1 个必需的位置参数:“事件”的主要内容,如果未能解决你的问题,请参考以下文章

类型错误:<lambda>() 缺少 1 个必需的位置参数:'w'

python pdb lambda函数全局名称错误

Python中使用列表生成式生成多个lambda函数逻辑错误

Python中使用列表生成式生成多个lambda函数逻辑错误

Python中使用列表生成式生成多个lambda函数逻辑错误

用于检查特定标签是不是不存在的 Lambda 函数-python