wxPython:使用更新的表刷新帧

时间:2016-06-17 01:59:24

标签: python user-interface wxpython refresh

我使用pyodbc创建一个GUI来查看我的SQL数据库中的表信息,目前我的代码会杀死该帧并使用更新的信息重新生成它。以下是执行搜索时所执行操作的示例:

def nctDialog(self):
    global nct, frame, table
    dlg = wx.TextEntryDialog(
            self, 'Enter NCT # here:',
            'NCT Search', 'NCT #')

    if dlg.ShowModal() == wx.ID_OK:
        nct = ""
        if(str(dlg.GetValue()) != ""):
            nct =  int(dlg.GetValue())
        frame.Destroy()
        frame = TestFrame(None, sys.stdout)
        frame.Show(True)

    dlg.Destroy()

它可以工作,但是看到窗口重新出现在屏幕上的新位置,我感到有点烦恼。我真正想要实现的是更新表信息而不会删除帧。

有没有办法更新框架中的表而不会在wxPython中删除和重新构建框架?

TestFrame主要是不感兴趣的,它设置框架并调用CustTableGrid(),我将在之后显示...

class TestFrame(wx.Frame):
    def __init__(self, parent, log):
    ....
        grid = CustTableGrid(p, log)
    ....

CustTableGrid()创建一个网格并调用:

class CustTableGrid(gridlib.Grid):
    def __init__(self, parent, log):
        gridlib.Grid.__init__(self, parent, -1)
    table = CustomDataTable(log)
    ....

CustomDataTable:

class CustomDataTable(gridlib.PyGridTableBase):


    def __init__(self, log):
        global nct, toDate, fromDate, isn
        gridlib.PyGridTableBase.__init__(self)
        self.log = log

        self.colLabels = [ 'Model', 'ISN', 'NCT', 'FW', 'Date', 'WWN']

        self.dataTypes = [ gridlib.GRID_VALUE_STRING,gridlib.GRID_VALUE_STRING,gridlib.GRID_VALUE_STRING,gridlib.GRID_VALUE_STRING,gridlib.GRID_VALUE_STRING,gridlib.GRID_VALUE_STRING,gridlib.GRID_VALUE_STRING,gridlib.GRID_VALUE_STRING]



        self.data = []


        cursor.execute("select ISN, WWN, Date, FW, Model, NCT, isCurrent, Date from [General Drive Info] order by [General Drive Info].Date desc")
        rows = cursor.fetchall()

        count = 0


        if(fromDate != None and toDate != None):
            ...

2 个答案:

答案 0 :(得分:1)

取代:

frame.Destroy()
frame = TestFrame(None, sys.stdout)
frame.Show(True)

我说:

    table.data[:] = []            #clear all prior data in the table
    table.nctCall()               #puts in the new data/performs nct query
    grid.ForceRefresh()           #forces refresh of the grid with new table

nctCall()是类对象CustomDataTable的def,它将对象内的数据修改为所需的值,然后ForceRefresh()网格对象强制它显示更改。

答案 1 :(得分:0)

首先,我必须说using globalsgenerally frowned upon

话虽如此,如果没有完全重组你的程序,我将不得不对你的全局变量做出一些假设。

而不是:

frame.Destroy()
frame = TestFrame(None, sys.stdout)
frame.Show(True)

假设table类中的CustTableGridgrid中的TestFrame都是全局的(或者您将它们作为实例成员并从某处获取对它们的引用)更高),你应该能够做到这样的事情:

首先,更新CustomDataTable

中的数据

接下来,使用这些线代替Frame替换它们:

msg = wx.grid.GridTableMessage(table, wx.grid.GRIDTABLE_REQUEST_VIEW_GET_VALUES)
grid.ProcessTableMessage(msg)