如何简化wxpython代码?

迪托·奥吉阿什维利(DitoOgiashvili)

嗨,我的代码有问题,当在onbutton方法中n取数字100或更少作为参数时,它可以完美地工作。但是当数字大于100时,我的计算机死机,最后崩溃。我需要简化我的代码,并想使其以任何方式工作。有什么建议?我知道我的代码中有一些sql select,但是没有注意,只是考虑一下它们,因为数字和文本首先选择它给出了我要创建的静态文本的数量,第二选择它接受了我所要表达的内容要填补。

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()
迈克·德里斯科尔

看来您正在遍历结果集并设置各种标签。但是,您似乎还会在每次迭代中覆盖标签。我不确定您为什么要这么做。

我对您的问题的猜测是查询变得太大。尝试在不带wx的常规Python中执行查询,并查看执行所需的时间。如果您可以测量花费了多长时间,那么在这段时间内,它阻塞了wxPython的主循环,这就是为什么它冻结了。为了解决这个问题,您需要将查询放入单独的线程中,然后将结果发送回wxPython以更新显示。

以下是有关该主题的几篇文章:

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章