Python使用lamda函数时出错,缺少1个必需的位置参数:';事件';
我有以下程序Python使用lamda函数时出错,缺少1个必需的位置参数:';事件';,python,tkinter,tkinter-canvas,Python,Tkinter,Tkinter Canvas,我有以下程序 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): &
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()
从tkinter导入*
将tkinter作为tk导入
A类:
colorCelda=“黑色”
colorDefault=“白色”
colorBorde=“黑色”
BordedDefault=“黑色”
定义初始化(自、根、主、x、y、大小):
“”“由单元格(…)调用的对象的构造函数”“”
self.master=master
self.abs=x
self.ord=y
self.size=大小
self.fill=False
def开关(自):
“”“如果单元格已满或未满,请切换。”“”
self.fill=非self.fill
def牵引(自):
#迪布贾尔帆布
如果self.master不是无:
轮廓=格里拉·科洛博德
填充=Grilla.colorCelda
如果不是自行填写:
轮廓=Grilla.BordedDefault
fill=Grilla.colorDefault
xmin=self.abs*self.size
xmax=xmin+self.size
ymin=self.ord*self.size
ymax=ymin+self.size
self.master.create_矩形(xmin,ymin,xmax,ymax,fill=fill,outline=outline)
CellGrilla类(画布):
定义初始值(self、master、numFil、numCol、tamGrid、*args、**kwargs):
画布._uu初始化(self,master,width=tamGrid*numCol,height=tamGrid*numFil,*args,**kwargs)
self.button1=tk.Button(self,command=lambda:self.button1\u单击)
self.cellSize=tamGrid
self._grid=[]
对于范围内的行(numFil):
行=[]
对于范围内的列(numCol):
行追加(Grilla(主、自、列、行、tamGrid))
self.\u grid.append(行)
#记住经过修改的细胞,以避免在鼠标移动过程中出现多次状态切换。
self.switched=[]
#绑定单击操作
#self.bind(“,self.button1\u单击)
self.draw()
def牵引(自):
对于自网格中的行:
对于行中的单元格:
cell.draw()
def_coordenadas(自身、事件):
行=int(event.y/self.cellSize)
column=int(event.x/self.cellSize)
返回行、列
def按钮1_单击(自身、事件):
self.bind(“,self.button1\u单击)
行、列=自身。\u坐标(事件)
单元格=自身网格[行][列]
cell.switch()
cell.draw()
通过
def按钮2_单击(自身):
通过
def按钮3_已单击(自行):
通过
def按钮4_单击(自行):
通过
如果名称=“\uuuuu main\uuuuuuuu”:
app=Tk()
#Tamaño de canvas x Tamaño de pixeles
网格=CellGrilla(应用程序,75,75,10)
grid.grid(行=1,列=1,行span=4,sticky=“news”)
boton1=按钮(应用程序,text=“Dibujar”,命令=grid.button1\u单击,高度=1,宽度=30)
boton2=按钮(应用程序,text=“DDA”,命令=grid.button2\u单击,高度=1,宽度=30)
boton3=按钮(应用程序,text=“放大”,命令=grid.button3\u单击,高度=1,宽度=30)
boton4=按钮(应用程序,text=“缩小”,命令=网格。按钮3\u单击,高度=1,宽度=30)
boton5=按钮(应用程序,text=“Bresenham”,命令=grid.button3\u单击,高度=1,宽度=30)
boton6=按钮(应用程序,text=“Clear”,命令=网格。按钮4\u单击,高度=1,宽度=30)
boton1.grid(行=1,列=2,sticky=“news”)
boton2.grid(行=2,列=2,sticky=“news”)
boton3.grid(行=3,列=2,sticky=“news”)
boton4.grid(行=4,列=2,sticky=“news”)
boton5.grid(行=5,列=2,sticky=“news”)
boton6.grid(行=6,列=2,sticky=“news”)
app.mainloop()
现在我正试图弄清楚如何让第一个按钮工作,当什么都没有按下,我按下网格时,什么都没有发生。当我按下按钮“Dibujar”时,它应该能够在网格上绘制,但是我尝试实现de“if else”函数,但我不知道如何正确地执行它。我一直在尝试用“lambda”来解决这个问题,但当按下第一个按钮时,我得到了以下错误
Tkinter回调中的异常
回溯(最近一次呼叫最后一次):
文件“E:\Python\lib\tkinter_init_uuz.py”,第1884行,在调用中
返回self.func(*args)
TypeError:button1\u clicked()缺少1个必需的位置参数:
“事件”
我不确定我做错了什么,这是我第一次用python编程,所以如果有人能帮我解决这个问题,我将不胜感激。根据您对
按钮1的定义,它有强制的事件
参数。但是在方法调用期间没有传递任何内容。您可以删除它,或者通过指定默认值使其成为可选的
def button1_clicked(self, event=None):
...
请看以下代码:
从tkinter导入*
将tkinter作为tk导入
A类:
colorCelda=“黑色”
colorDefault=“白色”
colorBorde=“黑色”
BordedDefault=“黑色”
定义初始化(自、根、主、x、y、大小):
“”“由单元格(…)调用的对象的构造函数”“”
self.master=master
self.abs=x
self.ord=y
self.size=大小
self.fill=False
def开关(自):
“”“如果单元格已满或未满,请切换。”“”
self.fill=非self.fill
def重置(自):
“”“清除单元格”“”
self.fill=False
def牵引(自):
#迪布贾尔帆布
如果self.master不是无:
轮廓=格里拉·科洛博德
填充=Grilla.colorCelda
如果不是自行填写:
轮廓=Grilla.BordedDefault
fill=Grilla.colorDefault
xmin=self.abs*self.size
xmax=xmin+self.size
ymin=self.ord*self.size
ymax=ymin+self.size
self.master.create_矩形(xmin,ymin,xmax,ymax,fill=fill,outline=outline)
CellGrilla类(画布):
定义初始值(self、master、numFil、numCol、tamGrid、*args、**kwargs):
画布._uu初始化(self,master,width=tamGrid*numCol,height=tamGrid*numFil,*args,**kwargs)
self.bind(“,self.square_单击)
self.cellSize=tamGrid
self.pen\u type=“绘图”
self._grid=[]
对于范围内的行(numFil):
行=[]
对于范围内的列(numCol):
行。附加(栅格)(主、自、列、,