wxpython:关闭应用程序不会关闭进度条

时间:2016-07-23 16:03:32

标签: python wxpython progress-bar

我有一个从SQL服务器检索数据的应用程序。我包含一个进度条来显示进度。

然而,问题是当我尝试通过点击" x"来关闭应用程序时在应用程序窗口的右上角,应用程序的主窗口将关闭,但进度条将继续运行,直到完成SQL服务器的所有工作。

我想知道是否有办法在点击" x" 时终止所有内容。示例代码(减去从SQL服务器执行数据检索的部分)如下:

import wx, pyodbc

class App(wx.Frame):
    def __init__(self, parent, title):
        super(App, self).__init__(parent, title=title, size=(600, 400))
        #-----------------------------------------------------------
        self.Bind(wx.EVT_CLOSE, self.OnExit)  # ADDED 
        #-----------------------------------------------------------
        p = wx.Panel(self)
        nb = wx.Notebook(p)
        self.Panel1 = PanelMaker(nb, 'Foo')               
        nb.AddPage(self.Panel1, "Foo")
        sizer = wx.BoxSizer()
        sizer.Add(nb, 1, wx.EXPAND)
        p.SetSizer(sizer)
        self.Centre()

    #-----------------------------------------------------------
    def OnExit(self, evt):    #ADDED
        self.Destroy()
        #self.Close() #I tried self.Close(), but I could not even 
                      #close the application window when using it.
    #-----------------------------------------------------------
class ProgressBar(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, title="In progress...", size=(300, 90),  style = wx.FRAME_FLOAT_ON_PARENT)        
        GridBagSizer = wx.GridBagSizer()

        self.gauge = wx.Gauge(self, range = 100, size = (-1, 30), style =  wx.GA_HORIZONTAL, name = 'In Progress') 
        self.gauge.SetValue(0)
        txt = wx.StaticText(self, label = 'Hamsters are working very hard to move data', style = wx.ALIGN_CENTER)

        GridBagSizer.Add(self.gauge, pos = (0, 0), span = (1, 1), flag = wx.EXPAND|wx.ALL, border = 15)
        GridBagSizer.Add(txt, pos = (1, 0), span = (1, 1), flag = wx.ALL, border = 15)
        self.SetSizer(GridBagSizer)
        self.Layout()

    def Update(self, step):        
        self.gauge.SetValue(step)
        if step == 100:
            self.Close()

class PanelMaker(wx.Panel):    
    def __init__(self, parent, tool):
        wx.Panel.__init__(self, parent = parent)
        Panel1Sizer = wx.GridBagSizer(0, 10)

        ProceedButton = wx.Button(self, label = tool, size = (-1, -1))   

        ProceedButton.Bind(wx.EVT_BUTTON, self.OnProceedButton)
        Panel1Sizer.Add(ProceedButton, pos = (7, 0), span = (1, 1), flag = wx.EXPAND|wx.LEFT, border = 12)
        Panel1Sizer.Layout()
        self.SetSizer(Panel1Sizer)    

    def OnProceedButton(self, evt):
        Progbar = ProgressBar(self.GetParent())
        Progbar.Show()

        connection = pyodbc.connect(DRIVER = '{SQL Server}',
                                    SERVER = ServerName,
                                    DATABASE = DatabaseName,
                                    Trusted_Connection = True)

        cursor = connection.cursor()

        for i in range(100):
            #retrieve data from the SQL server.......
            wx.Yield()
            Progbar.Update(i+1)

    #Closing SQL connection    
    cursor.close()
    del cursor
    connection.close()

1 个答案:

答案 0 :(得分:0)

是的,绑定关闭事件。

self.Bind(wx.EVT_CLOSE, self.OnExit)

此外,我没有看到您关闭数据库连接或self.Destroy()