I basically have a lot of different canvases that are all consequent to the other in order to create a game-like study guide. I wrote all canvases in functions given that it is schoolwork and with the click of every button the current canvas ereases and it jumps to the next canvas called by a function. My issue is that I also need to take into account correct answers to every question and doing so with a counter and I opted to go with a button because one can edit the configs --text-- that it displays.
def esp3():
canvase3=Canvas(window, width=750,height=537,bg='yellow')
canvase3.pack()
img = ImageTk.PhotoImage(Image.open("3esp.jpeg").resize((750, 537), Image.Resampling.LANCZOS))
canvase3.background = img
bg = canvase3.create_image(0, 0, anchor=tk.NW, image=img)
button1 = tk.Button(window, text="a", image=abutton, command=lambda: eligemateria() & canvase3.destroy())
button1 = canvase3.create_window(270, 280, anchor=tk.NW, window=button1)
button2 = tk.Button(window, text="b", image=bbutton, command=lambda: eligemateria() & canvase3.destroy())
button2 = canvase3.create_window(346, 280, anchor=tk.NW, window=button2)
button3 = tk.Button(window, text="c", image=cbutton, command=lambda: eligemateria() & canvase3.destroy())
button3 = canvase3.create_window(422, 280, anchor=tk.NW, window=button3)
Now, an example of something I would want to do is
button1 = tk.Button(window, text="a", image=abutton, command=lambda:[eligemateria(), canvase3.destroy(), correctabien()])
button1 = canvase3.create_window(270, 280, anchor=tk.NW, window=button1)
I've researched many methods but they only execute the first two functions in the command. I'd appreciate any input and if any context is needed I'm open to share more code or even other stuff I've tried.
Edit: The problem with the function to add a correct answer worked and thanks for the help but I have another instance where even though it executes the three tasks it shows an error in the terminal.
def creararchivoint():
canvasnew=Canvas(window, width=750,height=537,bg='yellow')
canvasnew.pack()
img = ImageTk.PhotoImage(Image.open("new.jpeg").resize((750, 537), Image.Resampling.LANCZOS))
canvasnew.background = img
bg = canvasnew.create_image(0, 0, anchor=tk.NW, image=img)
e1 = Entry(canvasnew)
canvasnew.create_window(380,360,window=e1, width=300, height=40)
button1 = tk.Button(window, text="Listo", command=lambda: [creacion(e1.get()), eligemateria() & canvasnew.destroy()])
button1 = canvasnew.create_window(359, 385, anchor=tk.NW, window=button1)
botonsal = tk.Button(canvasnew, text="Exit", image=imagbot2, command=lambda:window.quit())
botonsal = canvasnew.create_window(630, 50, anchor=tk.NW, window=botonsal)
The error it shows is:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.2288.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "c:\Users\Rodri\Compu 1\yeet\codigo final.py", line 68, in <lambda>
button1 = tk.Button(window, text="Listo", command=lambda: [creacion(e1.get()), eligemateria() & canvasnew.destroy()])
TypeError: unsupported operand type(s) for &: 'NoneType' and 'NoneType'
CodePudding user response:
Lambda functions are nice for doing quick and simple things, but when you start wanting to do more complex functions it is typically best to just define a function the normal way using def
. You can still defined your function inside of the parent function if you wish.
def make_two_functions():
lambda_summer = lambda a, b: a b
def normal_summer(a, b):
"""this is comparable to the lambda function but uses a normal function definition"""
return a b
return lambda_summer, normal_summer
With that said, your attempt at running three subfunctions from a lambda function actually should work. Can you post the traceback you get or the way you know it does not work? Try this runnable demo below.
def func_a():
print("in func a")
return 'a'
def func_b(x):
print(f"in func b, {x=}")
return 'b'
def func_c(y=3):
print(f"in func c, {y=}")
return 'c'
my_lambda = lambda x: [func_a(), func_b(x), func_c()]
my_lambda(7)
<script src="https://modularizer.github.io/pyprez/pyprez.min.js" theme="darcula"></script>