从另一个类访问小部件?

如何解决从另一个类访问小部件?

我正在尝试从另一个类更新 wx.ListCtrl。这让我发疯,我不明白我到底做错了什么。小部件是在 wx.Frame 中创建的,如:


class DataTable(gridlib.GridTableBase):
    def __init__(self,data):

        gridlib.GridTableBase.__init__(self)
        self.data = data
       
        #Store the row and col length to see if table has changed in size
        self._rows = self.GetNumberRows()
        self._cols = self.GetNumberCols()

        print(self._rows,self._cols)

        self.odd=gridlib.GridCellAttr()
        self.odd.SetBackgroundColour((217,217,217))
        self.even=gridlib.GridCellAttr()
        self.even.SetBackgroundColour((255,255,255))

    def GetAttr(self,row,col,kind):
        attr = [self.even,self.odd][row % 2]
        attr.IncRef()
        return attr

    def GetNumberRows(self):
        return len(self.data)

    def GetNumberCols(self):
        return len(self.data.columns) + 1

    def IsEmptyCell(self,col):
        return False

    def GetValue(self,col):
        if col == 0:
            pass
        #    return None #self.data.index[row]
        else:
            return self.data.iloc[row,col-1]

    def SetValue(self,value):
        if col == 0:
            pass
        else:
            self.data.iloc[row,col - 1] = value
    
    def GetColLabelValue(self,col):
        try:
            if col == 0:
                return ""
                #return 'Index' if self.data.index.name is None else self.data.index.name
            else:
                return self.data.columns[col - 1] #[col-1]
        except:
            pass

class DataGrid(gridlib.Grid):
def __init__(self,parent,data): # data
        gridlib.Grid.__init__(self,- 1) #,colnames,-1 # data

        self.table = DataTable(data)
        self.SetTable(self.table,True)
        self.Bind(wx.grid.EVT_GRID_CELL_CHANGED,self.onCellChanged)
        
    
    def onCellChanged(self,event):
        #Establish connection
        self.connect_mysql()
        #Update database
        key_id =  str("'") + self.GetCellValue(event.GetRow(),1) +  str("'")    #FOR TESTING. CHANGE TO 0 WHEN IMPORT IS FIXED
        target_col = self.GetColLabelValue(event.GetCol())
        key_col = self.GetColLabelValue(1)                                     #FOR TESTING. CHANGE TO 0 WHEN IMPORT IS FIXED
        nVal = str("'") + self.GetCellValue(event.GetRow(),event.GetCol()) +  str("'")
        sql_update = "UPDATE " + tbl + " SET " +  target_col + " = " + nVal + " WHERE " + key_col  +  " = " + key_id + ""

        try:
            self.cursor.execute(sql_update)
            stat = "UPDATE"
            oVal = event.GetString()
            action = "UPDATE"
            msg =  "Changed " + str("'") + oVal + str("'") + " to " + nVal + " for " + key_id + " in table: " + str("'") + tbl + str("'") 
        #self.updateStatus()
        except:
            stat = "ERROR"
            msg = "ERROR: Failed to update SQL table. " + "'" + tbl + "'"
            self.db_con.rollback()

       #Update dataframe

       #Log the change
       #MainFrame.test_print(stat,msg)      
       #MainFrame.lc_change.Append(["1","2","3" + "\n"])
       
       #Close connection
        self.close_connection()

class MainFrame(wx.Frame):
    def __init__(self,data): # (self,data):
        wx.Frame.__init__(self,-1,"Varkey Foundation") #,size=(640,480))
        #Create a panel
        self.p = wx.Panel(self)
        self.Maximize(True)
        
        #Create blank dataframe
        data = pd.DataFrame() #pd.DataFrame(np.random.randint(0,100,size=(200,5)),columns=list('EFGHD')
        #data.reset_index(drop=True,inplace=True)
        self.data = DataTable(data)
        
        self.nb = wx.Notebook(self.p)
        self.p.SetBackgroundColour( wx.Colour( 0,0 ) ) # 38,38,38
        self.nb.SetBackgroundColour(wx.Colour(58,56,56) )
        #self.SetBackgroundColour( wx.Colour( 255,56 ) )

        #create the page windows as children of the notebook
        self.page1 = PageOne(self.nb)
        self.page2 = PageTwo(self.nb)
        self.page3 = PageThree(self.nb)

        # add the pages to the notebook with the label to show on the tab
        self.nb.AddPage(self.page1,"Data")
        self.nb.AddPage(self.page2,"Analyze")
        self.nb.AddPage(self.page3,"Change Log")

        #Create the grid and continue layout
        self.grid = DataGrid(self.page1,data)
        #grid.SetReadOnly(5,5,True)
        
        #CreateFonts
        self.b_font = wx.Font(14,wx.ROMAN,wx.NORMAL,wx.BOLD,True)
        self.lbl_font = wx.Font(14,True)
        self.cb_font = wx.Font(11,wx.SCRIPT,wx.ITALIC,True)
        self.h_font = wx.Font(18,wx.DECORATIVE,True)
        #Create Title bmp
        ico = wx.Icon('varkey_bmp.bmp',wx.BITMAP_TYPE_ICO) #'varkey_frame.bmp'
        self.SetIcon(ico)

        #Page 1 sizers and widgets
        self.title = wx.StaticText(self.page1,label="TITLE",style = wx.ALIGN_CENTER | wx.ST_NO_AUTORESIZE)
        self.title.SetForegroundColour((255,255))
        self.title.SetFont(self.h_font)
        self.p1_sizer = wx.BoxSizer(wx.VERTICAL)
        self.p1_sizer.Add(self.title,wx.EXPAND,5)
        self.p1_sizer.Add(self.grid,3,wx.EXPAND | wx.LEFT | wx.RIGHT | wx.BOTTOM,25)
        #self.p1_sizer.Add(self.btn_new,-0,wx.ALIGN_CENTER,5)
        self.page1.SetSizer(self.p1_sizer)
        
        #Page 2 sizers and widgets
        self.analyze_grid = gridlib.Grid(self.page2)
        self.analyze_grid.CreateGrid(0,10)

        self.p2_sizer = wx.BoxSizer(wx.VERTICAL)
        self.p2_sizer.Add(self.analyze_grid,1,wx.EXPAND)
        self.page2.SetSizer(self.p2_sizer)
        
        #Page 3 sizers and widgets
        self.log_grid = gridlib.Grid(self.page3)
        self.log_grid.CreateGrid(0,9)
        self.log_grid.EnableEditing(False)
       
        self.p3_sizer = wx.BoxSizer(wx.VERTICAL)
        self.p3_sizer.Add(self.log_grid,wx.EXPAND)
        self.page3.SetSizer(self.p3_sizer)

        #Create widgets for top sizer
        #Insert Image
        self.staticbitmap = wx.StaticBitmap(self.p)
        self.staticbitmap.SetBitmap(wx.Bitmap('varkey_logo2.jpg'))
        self
        self.lbl_user = wx.StaticText(self.p,label="Username:")
        self.lbl_password = wx.StaticText(self.p,label="Password:")
        self.lbl_interaction = wx.StaticText(self.p,label="Interaction:")
        self.lbl_table = wx.StaticText(self.p,label="Table:")
        #SetForground colors
        self.lbl_user.SetForegroundColour((255,255))
        self.lbl_password.SetForegroundColour((255,255))
        self.lbl_interaction.SetForegroundColour((255,255))
        self.lbl_table.SetForegroundColour((255,255))

        #Set Fonts
        self.lbl_user.SetFont(self.lbl_font)
        self.lbl_password.SetFont(self.lbl_font)
        self.lbl_interaction.SetFont(self.lbl_font)
        self.lbl_table.SetFont(self.lbl_font)

        self.tc_user =wx.TextCtrl(self.p,value='cmccall95',size = (130,25))
        self.tc_password =wx.TextCtrl(self.p,value='Achilles95',style=wx.TE_PASSWORD | wx.TE_PROCESS_ENTER,25))
        self.tc_password.Bind(wx.EVT_TEXT_ENTER,self.onLogin)
        self.tc_user.SetFont(self.cb_font)
        self.tc_password.SetFont(self.cb_font)

        self.btn_login = wx.Button(self.p,label="Login",size=(105,30))
        self.btn_login.SetBackgroundColour(wx.Colour(198,89,17))
        self.btn_login.SetFont(self.b_font)
        self.btn_login.Bind(wx.EVT_BUTTON,self.onLogin) #connect_mysql

        self.btn_logout = wx.Button(self.p,label="Logout",30))
        self.btn_logout.SetBackgroundColour(wx.Colour(192,0))
        self.btn_logout.SetFont(self.b_font)
        #self.btn_logout.Bind(wx.EVT_BUTTON,self.onLogout)


        self.combo_interaction = wx.ComboBox(self.p,size = (160,25),style = wx.CB_READONLY | wx.CB_SORT | wx.CB_SORT)
        self.combo_interaction.Bind(wx.EVT_COMBOBOX,self.onComboInteraction)
        self.combo_table = wx.ComboBox(self.p,style = wx.CB_READONLY | wx.CB_SORT | wx.CB_SORT)
        self.combo_table.Bind(wx.EVT_COMBOBOX,self.onHideCommands)
        self.combo_interaction.SetFont(self.cb_font)
        self.combo_table.SetFont(self.cb_font)

        #self.combo_table.Bind(wx.EVT_COMBOBOX,self.OnComboTable)
        self.btn_load  = wx.Button(self.p,label="Load Table",30))
        self.btn_load.SetBackgroundColour(wx.Colour(31,216,6))
        self.btn_load.SetFont(self.b_font)
        #self.btn_load.Bind(wx.EVT_BUTTON,self.onLoadData)
        self.btn_load.Bind(wx.EVT_BUTTON,self.test_reload)

        self.lc_change = wx.ListCtrl(self.p,style = wx.TE_MULTILINE | wx.LC_REPORT | wx.LC_VRULES)
        self.lc_change.InsertColumn(0,"User ID")
        self.lc_change.InsertColumn(1,"Status")
        self.lc_change.InsertColumn(2,"Description")
        self.lc_change.InsertColumn(3,"Date/Time")
        #Set column widths
        self.lc_change.SetColumnWidth(0,75)
        self.lc_change.SetColumnWidth(1,75)
        self.lc_change.SetColumnWidth(2,450)
        self.lc_change.SetColumnWidth(3,125)

       
        #Add Row Button
        #self.btn_new = wx.Button(self.page1,label="+",size = (35,25))
        #self.btn_new.SetForegroundColour(wx.Colour(112,173,71))
        #self.btn_new.SetFont(self.h_font)
        #self.btn_new.Bind(wx.EVT_BUTTON,self.onInsertRecordBelow)

        #Create Filler text
        self.lbl_filler = wx.StaticText(self.p,label="",size = (125,20))
        #Create FlexGridSizers(For top half)
        self.left_fgs = wx.FlexGridSizer(3,4,25,15)
        self.left_fgs.AddMany([(self.lbl_user,wx.ALIGN_LEFT | wx.LEFT,15),(self.tc_user,wx.EXPAND),(self.lbl_interaction,wx.ALIGN_RIGHT|wx.RIGHT,10),(self.combo_interaction,(self.lbl_password,wx.ALIGN_LEFT| wx.LEFT,(self.tc_password,(self.lbl_table,(self.combo_table),(self.btn_login,2,(self.btn_logout,(self.lbl_filler,(self.btn_load,1)])

        #Create Top Sizer 
        self.top_sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.top_sizer.Add(self.left_fgs,proportion = 1,flag = wx.ALL|wx.EXPAND,border = 30)
        self.top_sizer.Add(self.staticbitmap,wx.TOP | wx.RIGHT,border = 40) #30
        self.top_sizer.Add(self.lc_change,wx.RIGHT|wx.EXPAND,30)

        #create Bottom Sizer
        self.bottom_sizer = wx.BoxSizer(wx.VERTICAL)
        self.bottom_sizer.Add(self.nb,proportion = 5,flag = wx.LEFT |wx.RIGHT | wx.EXPAND,border = 30)
        
        self.mainsizer = wx.BoxSizer(wx.VERTICAL)
        self.mainsizer.Add(self.top_sizer,proportion = 0,border = 5)
        self.mainsizer.Add(self.bottom_sizer,border = 5)
        #self.mainsizer.Add(self.status_sizer,proportion =0,flag = wx.BOTTOM|wx.ALIGN_CENTER_HORIZONTAL,border = 15)
        self.p.SetSizerAndFit(self.mainsizer)

        self.hideWidgets()
        self.onHideCommands(self)

#### Some functions...

if __name__ == '__main__':
    import sys
    app = wx.App()
    frame = MainFrame(None,sys.stdout) # (None,sys.stdout)
    frame.Show(True)
    app.MainLoop()

    

我有另一个网格类,其中有一些绑定事件。

class DataGrid(gridlib.Grid):
    def __init__(self,data):
        gridlib.Grid.__init__(self,-1 

        self.table = DataTable(data)
        self.SetTable(self.table,True)
        self.Bind(gridlib.EVT_GRID_CELL_RIGHT_CLICK,self.OnCellRightClick)
        self.Bind(wx.grid.EVT_GRID_CELL_CHANGED,self.onCellChanged)

def onCellChanged(self,event):
       
       try:   
            
            stat = "UPDATE"
            msg =  "Changed " + str("'") + self.oVal + str("'") + " to " + self.nVal + " for " + self.key_id + " in table: " + str("'") + tbl + str("'") 
         
       except:
            stat = "ERROR"
            msg = "ERROR: Failed to update SQL table. " + "'" + tbl + "'"
            #self.updateStatus()
            

       #MainFrame.test_print(stat,msg)      
       #self.MainFrame.lc_change.Append(["1","3" + "\n"])
       

我尝试将其直接传递给函数。

MainFrame.test_print(stat,msg)     

哪个可以传递变量。问题是在使用“self”时,出现错误。

然后我尝试直接访问列表控件:

self.MainFrame.lc_change.Append(["1","3" + "\n"])

还有:

MainFrame.lc_change.Append(["1","3" + "\n"])

这两个给我“类型对象'xxx'没有属性'xxx'

谁能告诉我我做错了什么?

解决方法

您需要做的是能够允许 DataGrid 引用 wx.ListCtrl 创建的 MainFrame

class MainFrame(wx.Frame):
    def __init__(self,parent,data): # (self,data):
        wx.Frame.__init__(self,-1,"Varkey Foundation") #,size=(640,480))
        #Create a panel
        self.p = wx.Panel(self)
        #Create the ListCtrl here:
        self.lc_change = wx.ListCtrl(self.p,style = wx.TE_MULTILINE | wx.LC_REPORT | wx.LC_VRULES)

        ...
        # Later when you create the DataGrid:
        #Create the grid and continue layout
        self.grid = DataGrid(self.page1,data,self.lc_change)  # pass in the ListCtrl
        ...

class DataGrid(gridlib.Grid):
    def __init__(self,lc):
        gridlib.Grid.__init__(self,- 1) #,colnames,-1 

        self.lc = lc
        ...

    def onCellChanged(self,event):
        ...
       self.lc.Append(["1","2","3" + "\n"])  # Now update the ListCtrl

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)> insert overwrite table dwd_trade_cart_add_inc > select data.id, > data.user_id, > data.course_id, > date_format(
错误1 hive (edu)> insert into huanhuan values(1,'haoge'); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive> show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 <configuration> <property> <name>yarn.nodemanager.res