如何管理全局变量的使用以获取智能代码


 tableButon = table()
 tableParameter = table()
 entry41 = None
 entry42 = None
 entry43 = None
 entry44 = None
 entry51 = None
 entry52 = None
 entry53 = None
 entry54 = None
 entry61 = None
 entry62 = None
 entry63 = None
 entry64 = None
 entry71 = None
 entry72 = None
 entry73 = None
 entry74 = None
 entry81 = None
 entry82 = None
 entry83 = None
 entry84 = None
 entry91 = None
 entry92 = None
 entry93 = None
 entry94 = None
 entry101 = None
 entry102 = None
 entry104 = None
 entry111 = None
 entry112 = None
 entry113 = None
 entry121 = None
 entry122 = None
 entry123 = None
 entry131 = None
 entry132 = None
 entry141 = None
 entry142 = None
 entry151 = None
 entry152 = None
 entry161 = None
 entry162 = None
 entry171 = None
 entry172 = None
def showParameters(notebook):
     global tablaParametros
     global entry41
     global entry42
     global entry43
     global entry44
     global entry51
     global entry52
     global entry53
     global entry54
     global entry61
     global entry62
     global entry63
     global entry64
     global entry71
     global entry72
     global entry73
     global entry74
     global entry81
     global entry82
     global entry83
     global entry84
     global entry91
     global entry92
     global entry93
     global entry94
     global entry101
     global entry102
     global entry103
     global entry104
     global entry111
     global entry112
     global entry113
     global entry121
     global entry122
     global entry123
     global entry131
     global entry132
     global entry141
     global entry142
     global entry151
     global entry152
     global entry161
     global entry162
     global entry171
     global entry172
     ...........
     entry41 = gnc_plcEntry(plcSymbol="WL_Axial_Cons1", modulo='gncgnc_dilataciones.src', plcFactor=1, plcFormat=None, editable=True, sensitive=True, textColor=None, baseColor=None)
     tablaCalculoTemperatura.attachToCell(entry41, col=2, row=2, xpadding=SeparacionH, ypadding=SeparacionV)
     entry42 = gnc_plcEntry(plcSymbol="WL_Axial_Cons2", modulo='gncgnc_dilataciones.src', plcFactor=1, plcFormat=None, editable=True, sensitive=True, textColor=None, baseColor=None)
     tablaCalculoTemperatura.attachToCell(entry42, col=3, row=2, xpadding=SeparacionH, ypadding=SeparacionV)

....

  def updateParameters(notebook):
     global tablaParameter
     global entry41
     global entry42
     ....
     value = jh.Get("\PLC\program\symbol\module\'gnc\gnc_dilataciones.src'\WL_Axial_Cons1").values()[0]
     entry41.set_text(str(value))
     value = jh.Get("\PLC\program\symbol\module\'gnc\gnc_dilataciones.src'\WL_Axial_Cons2").values()[0]
     entry42.set_text(str(value))
     .......

有太多的全球性,这种方式使用它们有点乏味。我认为这不是处理全球变量的好方法。与所有全球列表一起使用列表会更好吗?还是上课并与自我一起使用?有什么想法吗?

最好的解决方案将不使用这么多不同的列表。
您应该使用一个列表(顺序订单)或使用字典:

my_dict = {}
my_dict['entry1'] = None
my_dict['entry2'] = None
...
def my_func():
    global my_dict
    print(my_dict)

,但我也建议您将dict作为参数传递而不是试图全球化。

def my_func(d):
    print(d)
my_func(my_dict)

您也可以创建一个循环以创建众多变量,如果它们都将包含None

for i in range(0,1000):
    my_dict['entry'+str(i)] = None

相关内容

  • 没有找到相关文章

最新更新