summarise code in python with many if-clauses

做~自己de王妃 提交于 2021-01-29 15:11:21

问题


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() == 0 and event_measure == "A1":
    self.Menu_blade_exchange["A1"].grid(row =2, column = 1, padx=3, pady=1, sticky = "W")
    self.Menu_rubbing_marks["A1"].grid(row =2, column = 2, padx=3, pady=1, sticky = "W")

if self.calculations["A1"].get() == 1 and event_measure == "A1":
    self.Menu_blade_exchange["A1"].grid_forget()
    self.Menu_rubbing_marks["A1"].grid_forget()
    self.Menu_rubbing_marks_border["A1"].grid_forget()
    self.blade_exchange_Type["A1"].set('')
    self.rubbing_marks_Type["A1"].set('')
    self.rubbing_marks_border_Type["A1"].set('')

if self.calculations["A2"].get() == 0 and event_measure == "A2":
    self.Menu_blade_exchange["A2"].grid(row =3, column = 1, padx=3, pady=1, sticky = "W")
    self.Menu_rubbing_marks["A2"].grid(row =3, column = 2, padx=3, pady=1, sticky = "W")

if self.calculations["A2"].get() == 1 and event_measure == "A2":
    self.Menu_blade_exchange["A2"].grid_forget()
    self.Menu_rubbing_marks["A2"].grid_forget()
    self.Menu_rubbing_marks_border["A2"].grid_forget()
    self.blade_exchange_Type["A2"].set('')
    self.rubbing_marks_Type["A2"].set('')
    self.rubbing_marks_border_Type["A2"].set('')

.... '''


回答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.



来源:https://stackoverflow.com/questions/65686535/summarise-code-in-python-with-many-if-clauses

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!