如何在wxpython中刷新Grid类?

时间:2019-05-21 01:30:13

标签: python python-3.x wxpython wxpython-phoenix

当我偶然发现有关刷新Grid中数据的问题时,我目前正在创建一个配置IP连接的程序,因为我已经制作了一个Grid类,其中包含SQL查询,该查询在该类加载后执行。所述刷新功能必须在“删除”或“修改”按钮内完成。

我尝试在网格类上使用ForceRefresh,先清除数据,然后刷新,然后完全打开和关闭窗口。其中2个无效,而最后一个则不会加载新数据。

class MainTable(wx.grid.Grid):
    def __init__(self, parent):
        wx.grid.Grid.__init__(self, parent, -1)
#Insert functions and SQL queries...

class dspdtb(wx.Frame):
    def __init__(self, title, parent=None, style = wx.MINIMIZE_BOX | wx.MAXIMIZE | wx.SYSTEM_MENU | wx.RESIZE_BORDER | wx.CLOSE_BOX | wx.CAPTION | wx.TRANSPARENT_WINDOW):
        wx.Frame.__init__(self, parent=parent, title=title)
#More stuff below...


        btn2 = BP.ButtonInfo(titleBar, wx.ID_ANY, wx.Bitmap("button5 a.png", wx.BITMAP_TYPE_PNG))
        titleBar.AddButton(btn2)
        btn2.SetBitmap(wx.Bitmap("button5 b.png", wx.BITMAP_TYPE_PNG), status="Pressed")
        self.Bind(wx.EVT_BUTTON, self.Removal, btn2)

#Still more stuff below...

        grid = MainTable(panel)
        vSizer.Add(titleBar, 0, wx.EXPAND)
        vSizer.Add((20, 20))
        vSizer.Add(grid, 0, wx.ALL | wx.CENTRE, 50)
        titleBar.DoLayout()
        vSizer.Layout()
        #toolbar end
        self.Show()
        self.Maximize(True)

#Insert other functions here...

    def Removal(self, event):
        global l_a
        t = len(l_a)
        n = 0
        chk = wx.MessageBox('Do you wanna delete the selected items?', 'Confirm Deletion', wx.YES_NO)
        if chk==2:
            while(n<t):
                sasa = l_a[n]
                print (sasa)
                try: 
                    mycursor.execute("DELETE FROM ip_config WHERE id=%s;", (sasa,))
                except mysql.connector.Error as err:
                    print("Something went wrong: {}".format(err))
                mydb.commit()
                n+=1
            wx.MessageBox('Rows now deleted, the table will now reload.', 'Deletion Completed', wx.OK)
        l_a=[]
        MainTable.ForceRefresh()

我需要程序来刷新dspdtb类或Grid类来刷新数据。但是当我执行了程序后,就会发生这种情况:

Traceback (most recent call last):
  File "C:\Users\Internal_IT\Desktop\IP automation\wxt.py", line 332, in Removal
    MainTable.ForceRefresh()
TypeError: Grid.ForceRefresh(): first argument of unbound method must have type 'Grid'

任何帮助都将受到赞赏。预先感谢!

1 个答案:

答案 0 :(得分:0)

据我所知,问题应该在于您正在类中创建MainTable对象。

grid = MainTable(panel)

然后在Removal方法中,要像MainTable类一样调用ForceRefresh方法。

MainTable.ForceRefresh()

可能您应该像这样在MainTable对象本身上调用此方法。

self.grid.ForceRefresh()

在执行此操作之前,不要忘了像这样将网格创建为具有self的实例变量。

self.grid = MainTable(panel)

让我知道是否有帮助。