Summarise Code In Python With Many If-clauses
I am currently using a lot of code for this. Is there a way to summarise the code? Thanks a lot! I would like to summarise the If conditions. ''' if self.calculations['A1'].get() =
Solution 1:
It's a bit hard to understand what you are trying to accomplish since you didn't provide a complete example. If we can assume that each block of code is identical and the only thing that is changing is the index into the array, you might be able to replace all of the if
statements with a single block of code:
if self.calculations[event_measure].get() == 0:
self.Menu_blade_exchange[event_measure].grid(row =2, column = 1, padx=3, pady=1, sticky = "W")
self.Menu_rubbing_marks[event_measure].grid(row =2, column = 2, padx=3, pady=1, sticky = "W")
else:
self.Menu_blade_exchange[event_measure].grid_forget()
self.Menu_rubbing_marks[event_measure].grid_forget()
self.Menu_rubbing_marks_border[event_measure].grid_forget()
self.blade_exchange_Type[event_measure].set('')
self.rubbing_marks_Type[event_measure].set('')
self.rubbing_marks_border_Type[event_measure].set('')
In the comments someone pointed out that the row numbers change, which could be solved by saving the row numbers in an array as well
rownum = {"A1": 2, "A2": 3, ...}
...
if self.calculations[event_measure].get() == 0:
self.Menu_blade_exchange[event_measure].grid(row =rownum[event_measure], column = 1, padx=3, pady=1, sticky = "W")
self.Menu_rubbing_marks[event_measure].grid(row =rownum[event_measure], column = 2, padx=3, pady=1, sticky = "W")
Another solution might be to use grid_remove
rather than grid_forget
, so that all of the configuration options are remembered.
This would likely be much, much easier if each "event measure" was an instance of a class.
Post a Comment for "Summarise Code In Python With Many If-clauses"