I have a class: MyClassName for example, with two functions: one to calculate a number and one to validate if a group of comboboxes (tkinter) all have selections. I’m also using tkinter for a button to call the calculate function using a lambda because I want the user to make the combobox selections and then press the calculate button, hence the need for a lambda.
Here is the button code:
# Calculate Button
self.calculate_button = ttk.Button(button_frame,
command = lambda: calculate(self), # Use a lambda to send params to the function for command.
width = 12,
text = "CALCULATE",
bootstyle = 'primary',
state = 'enabled')
# In the calculate function, I call another function:
def calculate(self):
# Call the Class function from within another Class function.
self.validate_Comboboxes() ...
And in the validate_Comboboxes function I use something like this:
# Function to validate if all the Comboboxes were selected.
def validate_Comboboxes(self):
for widget in self.base_frame.winfo_children():
if isinstance(widget, ttk.Combobox) and widget.get() == "":
messagebox.showerror("error", "All Comboboxes for the base frame must be selected for calculations!")
break
...
I tested the validate_Comboboxes function standalone outside this app, so I know it works. However, I get this error: AttributeError: ‘MyClassName’ object has no attribute ‘validate_Comboboxes’. Which seems like I don’t have it declared properly? I understand if it was simply a class attribute, you would use: myAttribute(self) to declare it, but validate_Comboboxes(self) doesn’t make sense to me as I declare it using my def statement, unless I”m missing something?
What is the correct way to code this for my use case, mainly to validate the user has all the comboboxes selected before they hit the calculate button?
I thought if you had two functions inside a class, like functionA(self) and functionB(self), you could call functionA from functionB using self.function(). So is the mechanism of using a button with a lamdda the issue here?