close
假設你需要做類似的事情
self.ini_file_model.set_value("camera_chkbox_1",self.view.ui.camerea_chkbox_1.isChecked())
self.ini_file_model.set_value("camera_chkbox_2",self.view.ui.camerea_chkbox_2.isChecked())
self.ini_file_model.set_value("camera_chkbox_3",self.view.ui.camerea_chkbox_3.isChecked())
self.ini_file_model.set_value("camera_chkbox_4",self.view.ui.camerea_chkbox_4.isChecked())
self.ini_file_model.set_value("camera_chkbox_5",self.view.ui.camerea_chkbox_5.isChecked())
self.ini_file_model.set_value("camera_chkbox_6",self.view.ui.camerea_chkbox_6.isChecked())
這事情可以改成下列
camera_chkbox_list= ["camera_chkbox_1", "camera_chkbox_2", "camera_chkbox_3", "camera_chkbox_4", "camera_chkbox_5", "camera_chkbox_6"]
# Iterate over the list and set the values
for chkbox_name in camera_chkbox_list:
chkbox_value = getattr(self.view.ui, chkbox_name)
self.ini_file_model.set_value(chkbox_name, chkbox_value)
其中camera_chkbox_list還可以簡化成
camera_chkbox_list = [f"camera_chkbox_{i}" for i in range(1,7)]
全站熱搜
留言列表