【问题标题】:wxpython: closing application does not close the progress barwxpython:关闭应用程序不会关闭进度条
【发布时间】:2016-07-23 16:03:31
【问题描述】:

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

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

我想知道是否有办法在单击“x”时终止所有内容。示例代码(减去从 SQL Server 检索数据的部分)如下:

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()

【问题讨论】:

    标签: python wxpython progress-bar


    【解决方案1】:

    是的,绑定关闭事件。

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

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

    【讨论】:

    • 我根据您的建议进行了更改,但仍然发生相同的事情:主应用程序窗口可以关闭,但进度条仍会继续,直到 SQL 进程完成。您能否为我的示例代码提供更多细节或更全面的编辑?谢谢!
    • 我也会将连接代码的关闭添加到 OnExit 函数中,在 self.Destroy() 之前
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-30
    • 2016-07-20
    • 1970-01-01
    • 2014-06-06
    • 1970-01-01
    • 2012-04-17
    相关资源
    最近更新 更多