python 为什么我的字典会从这个函数更改?

iq3niunx  于 2022-11-28  发布在  Python
关注(0)|答案(1)|浏览(174)

首先,我认为重要的是要提到我是一个初学者,尽管我确信这可以在我的代码中看到。
我遇到了一个问题,字典中的一些值正在更新,而我没有专门调用它们进行更改。我使用以下方法开始:

def draw_mtext(self, scale, win_x, win_y):
    try:
        dxf_mtext = cad_dict['dxf_mtext']
    
        tk_fonts = list(font.families())
        font_match_dict = {}
        standard_font = 'Arial'
        anchor_dict = {1: 'NW', 2: 'N', 3: 'NE', 4: 'W', 5: 'CENTER',
            6: 'E', 7: 'SW', 8: 'S', 9: 'SE'}
        # Font tag dictionary created to handle scaling of font sizes with
        # zoomer function
        self.font_tag_list = []
        
        print(f"end of draw_mtext, before canvas_mtext: {cad_dict['dxf_mtext'][0]}")
        print('\n')
    
        canvas_mtext = dxf_to_canvas.dxf_mtext_to_canvas(
                dxf_mtext, scale, self.extents, win_x, win_y)
    
        print(f"end of draw_mtext, after canvas_mtext: {cad_dict['dxf_mtext'][0]}")
        print('\n')
        
        for mtext in canvas_mtext:
            text = mtext[0]
            dxf_height = mtext[1]
            dxf_width = mtext[2]
            dxf_style = mtext[3]
            coords = mtext[4]
            rotation = mtext[5]
            attachment = mtext[6]
    
            for font_type in tk_fonts:
                if dxf_style in font_match_dict.keys():
                    canvas_font = font_match_dict[dxf_style]
                elif font_type in dxf_style:
                    canvas_font = font_type
                    font_match_dict[dxf_style] = canvas_font
                else:
                    canvas_font = standard_font
    
            # Scaling text height to match canvas layout
            canvas_height = int(dxf_height * 5)
    
            anchor = anchor_dict[attachment].lower()
            # Create font dict in order to facilitate zoomer scaling
            tag = f'{canvas_font}{canvas_height}'
            if tag not in self.font_tag_list:
                self.font_tag_list.append(tag)
            
            self.canvas.create_text(
                    coords,
                    text=text, 
                    font=(canvas_font, str(canvas_height)),
                    fill='black',
                    anchor=anchor,
                    tags=tag
            )
    
    except KeyError:
        pass

在这个方法中,我从不同的模块调用这个函数:

def dxf_mtext_to_canvas(dxf_mtext, scale, extents, win_x, win_y):
     canvas_mtext = []
 
     # Retrieve extents dimensions in order to translate coords
     extents_1 = extents[0]
     extents_2 = extents[1]
     extents_width = extents_2[0] - extents_1[0]
     extents_length = extents_2[1] - extents_1[1] 
 
     # Get amount to transform coords
     extents_x_min = min([extents_1[0], extents_2[0]])
     extents_y_min = min([extents_1[1], extents_2[1]])
 
     # Retrieves coordinates from dxf_lines
     for mtext in dxf_mtext:
 
         coords = mtext[4]
         updated_mtext = mtext
 
         # Puts coords at origin of canvas
         x_coord = coords[0] - extents_x_min
         y_coord = coords[1] - extents_y_min
         
         # Multiply coords by scale and move to center of canvas
         translation_x = (win_x - extents_width * scale) / 2 
         translation_y = (win_y - extents_length * scale) / 2 
        
         x_coord_canvas = x_coord * scale + translation_x
         y_coord_canvas = y_coord * scale + translation_y
 
         updated_mtext[4] = (x_coord_canvas, -1*y_coord_canvas)
  
         # Insert updated canvas coordinates into mtext list
         canvas_mtext.append(mtext)
 
     return canvas_mtext

不知怎么的,我的字典在那个函数调用之后发生了变化,我一辈子都搞不清楚为什么,这让我抓狂。
我将非常感谢任何指点。

y1aodyip

y1aodyip1#

忘记了列表的可变性。一旦值被转换为元组就起作用。

相关问题