可以在python中使用循环保存语法吗? [英] save syntax with loop in python possible?
问题描述
我想在 tkinter 中创建很多小部件.我目前为此使用了很多代码.有没有办法总结代码?但是,我仍然希望能够捕获每个小部件的价值.编号上升到 200...非常感谢!
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"))
'''
我还想总结一下 If 条件.
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('')
'''
推荐答案
代替每个 IntVar
和 CheckButton
的单独变量,定义一个 dict
> 持有每组.
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}
然后您可以遍历 access_measures
字典,以使用适当的参数对每个字典调用 grid
和 bind
.grid
的大部分参数都是常量,除了 row
值可以通过 enumerate
计算.棘手的部分是确保传递给 bind
的回调实际上具有传递给 enable_measure_specification
的 k
的当前值.
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))
这篇关于可以在python中使用循环保存语法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!