从主模块更改常量文件中变量的值

问题描述 投票:0回答:1

需要将常量文件中的值更改为滑块的当前值。 据我了解,此实现不会更新文件中的值

from checkers import constants

class CustomScale(ttk.Scale):
    def __init__(self, master=None, **kw):
        kw.setdefault("orient", "horizontal")
        kw.setdefault("from_", 1)  
        kw.setdefault("to", 3)     
        self.variable = kw.pop('variable', DoubleVar(master))
        ttk.Scale.__init__(self, master, variable=self.variable, **kw)
        self._style_name = '{}.custom.{}.TScale'.format(self, kw['orient'].capitalize())
        self['style'] = self._style_name
        self.last_pos = self.variable.get()
        self.dragging = False  
        self.bind("<ButtonRelease-1>", self.set_to_nearest)  

    def set_to_nearest(self, event):
        positions = [1, 2, 3]  
        current_value = self.variable.get()
        closest_position = min(positions, key=lambda x: abs(x - current_value))
        self.variable.set(closest_position)
        self.dragging = False  

        print(constants.MAX_DEPTH)
        if self.variable.get() == 1:
            constants.MAX_DEPTH = 1
            print(constants.MAX_DEPTH)
        elif self.variable.get() == 2:
            constants.MAX_DEPTH = 3
            print(constants.MAX_DEPTH)
        elif self.variable.get() == 3:
            constants.MAX_DEPTH = 5
            print(constants.MAX_DEPTH)
python variables import
1个回答
0
投票

定义一个要更新的函数

def update_max_depth(new_value)
constants.MAX_DEPTH=new_value
© www.soinside.com 2019 - 2024. All rights reserved.