如何简化wxpython代码?

时间:2013-10-22 08:07:52

标签: wxpython

嗨我的代码有问题,在onbutton方法中,n取数字100或更少作为参数,它工作正常。但是当数字超过100时,我的计算机会冻结,最后它会崩溃。我需要简化我的代码并希望以任何方式使其工作。有什么建议?我知道在我的代码中有一些sql选择,但是没有注意到只是把它们想象为数字和文本在第一次选择它给出了我想要创建的静态文本的数量,在第二次选择它需要内容与我的内容想填补。

import wx
import pyodbc
cnxn = pyodbc.connect("DRIVER={SQL Server Native Client 11.0};SERVER=10.75.79.215;DATABASE=HUB_DATA;UID=sa;PWD=password")
cursor = cnxn.cursor()

########################################################################
class RandomPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent, color):
        """Constructor"""
        wx.Panel.__init__(self, parent)
        self.SetBackgroundColour(color)

class scroll(wx.ScrolledWindow):
    def __init__(self, parent, color):
        wx.ScrolledWindow.__init__(self, parent)
        self.SetBackgroundColour(color)
        self.SetScrollbars(4,4,5000,5000)



########################################################################
class MainPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)

        topSplitter = wx.SplitterWindow(self)
        hSplitter = wx.SplitterWindow(topSplitter)

        panelOne = RandomPanel(hSplitter, "white")
        panelTwo = RandomPanel(hSplitter, "white")
        hSplitter.SplitVertically(panelOne, panelTwo)
        hSplitter.SetSashGravity(0.5)

        self.panelThree = scroll(topSplitter, "white")
        topSplitter.SplitHorizontally(hSplitter, self.panelThree)
        topSplitter.SetSashGravity(0.5)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(topSplitter, 1, wx.EXPAND)
        self.SetSizer(sizer)

        txtOne = wx.StaticText(panelOne, -1, label = "piradoba", pos = (20,10))
        self.txtTwo = wx.StaticText(panelOne, -1, label = "", pos = (40,80))
        self.txtplace = wx.TextCtrl(panelOne, pos = (20,30))
        button = wx.Button(panelOne, label = "search", pos = (40,100))
        button.Bind(wx.EVT_BUTTON, self.Onbutton)
        self.ctrls1 = []
        self.ctrls2 = []
        self.ctrls3 = []
        self.ctrls4 = []
        self.ctrls5 = []
        self.ctrls6 = []
        self.ctrls7 = []
        self.ctrls8 = []
        self.ctrls9 = []
        self.ctrls10 = []
        self.ctrls11 = []
        self.ctrls12 = []
        self.ctrls13 = []
        self.ctrls14 = []


    def Onbutton(self, event):
        var=self.txtplace.GetValue()
        if len(var) == 9 or len(var) == 11:
            self.txtTwo.SetLabel("True")
            cursor.execute("""SELECT COUNT(DPVLDT) as raodenoba
                            FROM [HBGE_Reports].[dbo].[hub_DDJPFile] s
                            where exists (SELECT [ZGDCS]
                            FROM [HUB_DATA].[dbo].[SSCUSTP] d
                            where [ZGIDNO]=?
                            and
                            s.DPACS=d.ZGDCS)""",str(var))
            raw = cursor.fetchone()
            n = raw.raodenoba

            for i in range(n):
                self.ctrls1.append(wx.StaticText(self.panelThree, label = '', pos = (20, 30 * i)))
                self.ctrls2.append(wx.StaticText(self.panelThree, label = '', pos = (120, 30 * i)))
                self.ctrls3.append(wx.StaticText(self.panelThree, label = '', pos = (220, 30 * i)))
                self.ctrls4.append(wx.StaticText(self.panelThree, label = '', pos = (320, 30 * i)))
                self.ctrls5.append(wx.StaticText(self.panelThree, label = '', pos = (420, 30 * i)))
                self.ctrls6.append(wx.StaticText(self.panelThree, label = '', pos = (520, 30 * i)))
                self.ctrls7.append(wx.StaticText(self.panelThree, label = '', pos = (620, 30 * i)))
                self.ctrls8.append(wx.StaticText(self.panelThree, label = '', pos = (720, 30 * i)))
                self.ctrls9.append(wx.StaticText(self.panelThree, label = '', pos = (820, 30 * i)))
                self.ctrls10.append(wx.StaticText(self.panelThree, label = '', pos = (920, 30 * i)))
                self.ctrls11.append(wx.StaticText(self.panelThree, label = '', pos = (1020, 30 * i),size=(10,10)))
                self.ctrls12.append(wx.StaticText(self.panelThree, label = '', pos = (1220, 30 * i),size=(50,100)))
                self.ctrls13.append(wx.StaticText(self.panelThree, label = '', pos = (1420, 30 * i),size=(50,100)))
                self.ctrls14.append(wx.StaticText(self.panelThree, label = '', pos = (1620, 30 * i),size=(50,100)))



            cursor.execute("""declare @a varchar(20)
                    set @a=?
                    SELECT [DPVLDT]
                          ,[DPCPDT]
                          ,[DPACB]
                          ,[DPACS]
                          ,[DPACX]
                          ,[DPCYCD]     
                          ,[DPDLCD]
                          ,[RCY_AMOUNT]
                          ,[LCY_AMOUNT]
                          ,[DPBLBL]
                          ,[DPNAR1]
                          ,[DPNAR2]
                          ,[DPNAR3]
                          ,[DPNAR4]     
                          FROM [HBGE_Reports].[dbo].[hub_DDJPFile] s
                          where exists (SELECT [ZGDCS]
                          FROM [HUB_DATA].[dbo].[SSCUSTP] d
                          where [ZGIDNO]=@a
                          and
                          s.DPACS=d.ZGDCS)
                          order by [DPVLDT] desc""", str(var))

            rows = cursor.fetchall()
            for i in range(n):
                self.ctrls1[i].SetLabel(str(rows[i].DPVLDT))
                self.ctrls2[i].SetLabel(str(rows[i].DPCPDT))
                self.ctrls3[i].SetLabel(str(rows[i].DPACB))
                self.ctrls4[i].SetLabel(str(rows[i].DPACS))
                self.ctrls5[i].SetLabel(str(rows[i].DPACX))
                self.ctrls6[i].SetLabel(str(rows[i].DPCYCD))
                self.ctrls7[i].SetLabel(str(rows[i].DPDLCD))
                self.ctrls8[i].SetLabel(str(rows[i].RCY_AMOUNT))
                self.ctrls9[i].SetLabel(str(rows[i].LCY_AMOUNT))
                self.ctrls10[i].SetLabel(str(rows[i].DPBLBL))
                self.ctrls11[i].SetLabel(str(rows[i].DPNAR1))
                self.ctrls12[i].SetLabel(str(rows[i].DPNAR2))
                self.ctrls13[i].SetLabel(str(rows[i].DPNAR3))
                self.ctrls14[i].SetLabel(str(rows[i].DPNAR4))









########################################################################
class MainFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="test",
                          size=(800,600))
        panel = MainPanel(self)
        self.Show()

#----------------------------------------------------------------------
if __name__ == "__main__":
    app = wx.App(False)
    frame = MainFrame()
    app.MainLoop()

1 个答案:

答案 0 :(得分:1)

看起来您正在循环结果集并设置各种标签。但是,您似乎还在每次迭代时覆盖标签。我不确定你为什么要那样做。

我对你的问题的猜测是查询变得太大了。尝试在没有wx的普通Python中进行查询,看看执行需要多长时间。如果你可以测量它花了多长时间,那么在那段时间里,它会阻止wxPython的主循环,这就是它冻结的原因。要解决这个问题,您需要将查询放入一个单独的线程中,并将结果发送回wxPython以更新显示。

以下是关于此主题的几篇文章: