使用lamda函数错误的Python缺少1个必需的位置参数:'event'

Paola vilaseca

我有以下程序

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回调中的异常

追溯(最近一次通话):

通话中的文件“ E:\ Python \ lib \ tkinter_ init _.py”,行1884

返回self.func(* args)

TypeError:button1_clicked()缺少1个必需的位置参数:“ event”

我不确定自己在做什么错,这是我第一次使用python编程,因此如果有人可以帮助我解决问题,我将非常感激。

蜥蜴

看下面的代码:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

类中的函数错误:TypeError:function()缺少1个必需的位置参数:

函数缺少 1 个必需的位置参数

Python 3.6 类型错误:缺少 1 个必需的位置参数

Python错误:缺少1个必需的位置参数:'self'

Python错误“ <方法>缺少1个必需的位置参数:'self'“

调用python函数时发生错误,TypeError:returnbook()缺少1个必需的位置参数:“ self”

类型错误:函数缺少 1 个必需的位置参数:'path' Flask Python

Python 递归函数。类型错误:table() 缺少 1 个必需的位置参数:'m'

python缺少1个必需的位置参数

insert() 缺少 1 个必需的位置参数:使用 python mysql 的“字符串”相同错误

TypeError:get()缺少1个必需的位置参数:函数SOLVED中的“ url”错误

函数model()缺少1个必需的位置参数

Spark - 缺少 1 个必需的位置参数(lambda 函数)

TypeError:函数缺少1个必需的位置参数

函数缺少 1 个必需的位置参数:“lst”

类型错误:post() 缺少 1 个必需的位置参数

类型错误:readData() 缺少 1 个必需的位置参数:“数据”

类型错误:decorator() 缺少 1 个必需的位置参数:'func'

类型错误:gassens() 缺少 1 个必需的位置参数:'self'

类型错误:insert() 缺少 1 个必需的位置参数:'chars'

类型错误:fit() 缺少 1 个必需的位置参数:'y',

类型错误:GetSize() 缺少 1 个必需的位置参数:'self'

Selenium错误:缺少1个必需的位置参数:'url'

类型错误:fit() 缺少 1 个必需的位置参数:'X'

类型错误:button() 缺少 1 个必需的位置参数:“数字”

错误“ TypeError:FirstForm()缺少1个必需的位置参数:'request'”

类型错误:removeDuplicates() 缺少 1 个必需的位置参数:“randList”

类型错误:main() 缺少 1 个必需的位置参数:'self'

类型错误:check() 缺少 1 个必需的位置参数:'self'