In working.py
, I get the focused frame using focus()
.
In calculations.py
, I am trying to run some calculations which depends on which frame is focused.
I am calling calculations()
on btn which is in main.py
.
What I am trying to achieve is that when I click Calculate
button,
if e1_frame
is focused I should get the addition of 2 numbers and
when e2_frame
is focused I should get the multiplication of those 2 numbers.
But instead I get the following error:
TypeError: <lambda>() missing 1 required positional argument: 'e'
main.py
from tkinter import *
from working import create_frames
from calculations import calculations
class MyApp:
def __init__(self, root):
self.root = root
self.var_e1 = StringVar(None)
self.var_e2 = StringVar(None)
self.var_lbl = StringVar(None)
self.var_status_lbl = StringVar(None)
create_frames(self)
e1 = Entry(self.e1_frame, textvariable=self.var_e1)
e2 = Entry(self.e2_frame, textvariable=self.var_e2)
e1.focus_set()
e1.pack()
e2.pack()
btn = Button(self.btn_frame, text='Calculate', command=lambda e: calculations(self, e))
btn.pack()
lbl = Label(self.root, font=20, textvariable=self.var_lbl)
self.var_lbl.set('0')
lbl.pack()
status_lbl = Label(self.root, textvariable=self.var_status_lbl)
status_lbl.pack(side=BOTTOM)
self.var_status_lbl.set('Nothing is selected.')
def main():
root = Tk()
root.geometry('400x400')
MyApp(root)
root.mainloop()
if __name__ == '__main__':
main()
working.py
from tkinter import *
def focus(self, event):
if event.widget == self.e1_frame:
self.var_status_lbl.set(f'First value: {self.var_e1.get()}')
if event.widget == self.e2_frame:
self.var_status_lbl.set(f'Second value: {self.var_e2.get()}')
def create_frames(self):
e0_frame = Frame(self.root)
self.e1_frame = Frame(e0_frame)
self.e2_frame = Frame(e0_frame)
e0_frame.pack()
self.e1_frame.pack(side=LEFT)
self.e2_frame.pack(side=LEFT)
self.btn_frame = Frame(self.root)
self.btn_frame.pack(pady=10)
self.e1_frame.bind('<Enter>', lambda e: focus(self, e))
self.e2_frame.bind('<Enter>', lambda e: focus(self, e))
calculations.py
from tkinter import *
def calculations(self, event):
value1 = int(self.var_e1.get())
value2 = int(self.var_e2.get())
if event.widget == self.e1_frame:
result = value1 + value2
if event.widget == self.e2_frame:
result = value1 * value2
result = value1 + value2
self.var_lbl.set(result)