问题
I would like to create a lot of widgets in tkinter. I am currently using a lot of code for this. Is there a way to summarise the code? However, I would still like to be able to capture the value of each widgets. The numbering goes up to 200... Thanks a lot!
'''
self.A1_access_measure_calculation = tk.IntVar()
self.A1_access_measure = tk.Checkbutton(self.A_Frame_measure, text="A1", variable=self.A1_access_measure_calculation)
self.A1_access_measure.grid(row =2, column =0, sticky = "W",padx=25, pady=4)
self.A1_access_measure.bind('<ButtonRelease-1>', lambda A1: self.enable_measure_specification("A1"))
self.A2_access_measure_calculation = tk.IntVar()
self.A2_access_measure = tk.Checkbutton(self.A_Frame_measure, text="A2", variable=self.A2_access_measure_calculation)
self.A2_access_measure.grid(row =3, column =0, sticky = "W",padx=25, pady=4)
self.A2_access_measure.bind('<ButtonRelease-1>', lambda A2: self.enable_measure_specification("A2"))
self.A3_access_measure_calculation = tk.IntVar()
self.A3_access_measure = tk.Checkbutton(self.A_Frame_measure, text="A3", variable=self.A3_access_measure_calculation)
self.A3_access_measure.grid(row =4, column =0, sticky = "W",padx=25, pady=4)
self.A3_access_measure.bind('<ButtonRelease-1>', lambda A3: self.enable_measure_specification("A3"))
self.A4_access_measure_calculation = tk.IntVar()
self.A4_access_measure = tk.Checkbutton(self.A_Frame_measure, text="A4", variable=self.A4_access_measure_calculation)
self.A4_access_measure.grid(row =5, column =0, sticky = "W",padx=25, pady=4)
self.A4_access_measure.bind('<ButtonRelease-1>', lambda A4: self.enable_measure_specification("A4"))
self.A5_access_measure_calculation = tk.IntVar()
self.A5_access_measure = tk.Checkbutton(self.A_Frame_measure, text="A5", variable=self.A5_access_measure_calculation)
self.A5_access_measure.grid(row =6, column =0, sticky = "W",padx=25, pady=4)
self.A5_access_measure.bind('<ButtonRelease-1>', lambda A5: self.enable_measure_specification("A5"))
'''
I would also like to summarise the If conditions.
'''
if self.calculations["A1"].get() == 0 and event_measure == "A1":
self.A1_Menu_blade_exchange.grid(row =2, column = 1, padx=3, pady=1, sticky = "W")
self.A1_Menu_rubbing_marks.grid(row =2, column = 2, padx=3, pady=1, sticky = "W")
if self.calculations["A1"].get() == 1 and event_measure == "A1":
self.A1_Menu_blade_exchange.grid_forget()
self.A1_Menu_rubbing_marks.grid_forget()
self.A1_Menu_rubbing_marks_border.grid_forget()
self.A1_blade_exchange_Type.set('')
self.A1_rubbing_marks_Type.set('')
self.A1_rubbing_marks_border_Type.set('')
if self.calculations["A2"].get() == 0 and event_measure == "A2":
self.A2_Menu_blade_exchange.grid(row =3, column = 1, padx=3, pady=1, sticky = "W")
self.A2_Menu_rubbing_marks.grid(row =3, column = 2, padx=3, pady=1, sticky = "W")
if self.calculations["A2"].get() == 1 and event_measure == "A2":
self.A2_Menu_blade_exchange.grid_forget()
self.A2_Menu_rubbing_marks.grid_forget()
self.A2_Menu_rubbing_marks_border.grid_forget()
self.A2_blade_exchange_Type.set('')
self.A2_rubbing_marks_Type.set('')
self.A2_rubbing_marks_border_Type.set('')
'''
回答1:
Instead of individual variables for each IntVar
and CheckButton
, define a dict
to hold each set.
import functools # Used later; see below
keys = [f"A{i}" for i in range(1, 200)]
self.calculations = {k: tk.IntVar() for k in keys}
self.access_measures = {k: tk.Checkbutton(self.A_Frame_measure, text=k, variable=self.calculations[k]) for k in keys}
You can then iterate over the access_measures
dictionary to call grid
and bind
on each with the appropriate arguments. Most of the arguments to grid
are constant, except for the row
value which can be computed by enumerate
.
The tricky part is making sure the callback passed to bind
actually has the current value of k
to pass to enable_measure_specification
.
for r, (k, cb) in enumerate(self.access_measures.items(), start=2):
cb.grid(row=r, column=0, sticky="W", padx=25, pady=4)
cb.bind('<ButtonRelease-1>', functools.partial(self.enable_measure_specification, k))
来源:https://stackoverflow.com/questions/65672150/save-syntax-with-loop-in-python-possible