Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

tkinter - 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() == 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('')

.... '''


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...