GUI从python2到python3

本文探讨了在升级到wxPython 4.1.0后遇到的编译问题,通过创建可哈希的lc类来使wx.Colour成为字典键。同时,展示了如何在view.py和gui.py中进行相应调整以确保代码在Python 3兼容性。
摘要由CSDN通过智能技术生成

won’t compile on wxPython 4.1.0 use wxPython 4.0.7 instead
这里使用是这个版本的wxPython 4.0.7.post2,最新版本会存在更多的不兼容。

Python 为什么list不能作为字典的key?

对于有些unhashable types不能作为dict key,我创建一个类class,他可以作为dict.key

#################view.py下的修改
class lc:
    #for (lengend_color) to be hashable
    #so can be dict.key using wx.Colour
    def __init__(self,name):
        self.name=name
  
 ################lyf
        self.legend_color = dict([
                            (lc(wx.Colour(64,128,128,trans)),1),(lc(wx.Colour(255,0,255,trans)),0),
                            (lc(wx.Colour(64,0,0,trans)),0), (lc(wx.Colour(255,255,0,trans)),0),
                            (lc(wx.Colour(0,128,255,trans)),0),(lc(wx.Colour(128,0,255,trans)),0),
                            (lc(wx.Colour(128,128,64,trans)),0),])
################################
    def get_color(self):
        for color, used in self.legend_color.items():
            if not used:
                self.legend_color[color] = 1
                #lpj
                return color.name
###########原始版本python2
        self.legend_color = dict([
                            (wx.Colour(64,128,128,trans),1),(wx.Colour(255,0,255,trans),0),
                            (wx.Colour(64,0,0,trans),0), (wx.Colour(255,255,0,trans),0),
                            (wx.Colour(0,128,255,trans),0),(wx.Colour(128,0,255,trans),0),
                            (wx.Colour(128,128,64,trans),0),])
                            # Format: [color, 0 or 1] 0 stands for not being used
                            #  1 for being used


    def get_color(self):
        for color, used in self.legend_color.iteritems():
            if not used:
                self.legend_color[color] = 1
                return color
#######gui.py下的修改
        grid_panel.legend_color =dict([
                            (lc(wx.Colour(64,128,128,trans)),1),(lc(wx.Colour(255,0,255,trans)),0),
                            (lc(wx.Colour(64,0,0,trans)),0), (lc(wx.Colour(255,255,0,trans)),0),
                            (lc(wx.Colour(0,128,255,trans)),0),(lc(wx.Colour(128,0,255,trans)),0),
                            (lc(wx.Colour(128,128,64,trans)),0),])
        
                                # Format: [color, 0 or 1] 0 stands for not being used
                                #  1 for being used


#################################
                color = view_material.zone_color.pop()
                #lyf
                for color_index, used in grid_panel.legend_color.items():
                    if color_index.name==color:
                        grid_panel.legend_color[color_index]=0
                #lpj comment one line
                #grid_panel.legend_color[color] = 0
                # Delete the last zone
########################原始版本
        grid_panel.legend_color =dict([
                            (wx.Colour(64,128,128,trans),1),(wx.Colour(255,0,255,trans),0),
                            (wx.Colour(64,0,0,trans),0), (wx.Colour(255,255,0,trans),0),
                            (wx.Colour(0,128,255,trans),0),(wx.Colour(128,0,255,trans),0),
                            (wx.Colour(128,128,64,trans),0),])
 ####################################
                 color = view_material.zone_color.pop()
                grid_panel.legend_color[color] = 0

此外,对于画图包wxpython,在view.py中有一些语句在python3中已经被弃用,因此将其comment

        #lyf comment
       #dc.BeginDrawing()
##################################
        #lyf comment
       #dc.EndDrawing()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

balabalahoo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值