0

childframe クラスで pubsub からのメッセージをサブスクライブすると問題が発生します。メインフレームからの選択を表示する正しいコードを書く方法がわかりません。また、childframe クラスで定義行を渡すこともできません。質問をどのように指定したかわかりませんが、理解するのが難しいことはわかっていますが、このコードでは、私が求めていたものを正確に見つけることができると思います. 私はpythonが初めてなので、プリズは批判しないでください

import wx
from wx.lib.pubsub import Publisher as pub
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 Mainframe(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent)

        self.panel = wx.Panel(self)
        self.quote1 = wx.StaticText(self.panel, label = "id")
        self.test=wx.StaticText(self.panel, label = "")
        self.editquote1 = wx.TextCtrl(self.panel, size = (140, -1))
        self.button = wx.Button(self.panel, label = "search")


    #
        self.windowSizer = wx.BoxSizer()
        self.windowSizer.Add(self.panel, wx.ID_ANY, wx.ALL | wx.EXPAND)

    #
        self.sizer = wx.GridBagSizer(6, 6)
        self.sizer.Add(self.quote1, (0, 0))
        self.sizer.Add(self.editquote1, (0, 1))


        self.sizer.Add(self.button, (3, 1))
        self.sizer.Add(self.test, (4, 1))


    #
        self.border = wx.BoxSizer()
        self.border.Add(self.sizer, 1, wx.ALL | wx.EXPAND, 6)

    #
        self.panel.SetSizerAndFit(self.border)
        self.SetSizerAndFit(self.windowSizer)

    #
        self.button.Bind(wx.EVT_BUTTON, self.Onbutton and self.afterOnbutton)




    def Onbutton(self,e):
        var = self.editquote1.GetValue()
        if len(var)==9:
            self.test.SetLabel('True')

        if len(var)==9:
            cursor.execute("""SELECT COUNT(DPVLDT) as amount
                            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()
            a = raw.amount
            pub.sendMessage("amount", a)


            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(a):
                pub.sendMessage(i, rows[i].DPVLDT)





        else:   
            self.test.SetLabel('False')

    def afterOnbutton(self,e):
        wx.CallAfter(self.Onbutton,e)
        var = self.editquote1.GetValue()
        if len(var)==9:
            self.new = Childframe(None)
            self.new.Show()





class Childframe(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent)

        self.panel=wx.Panel(self)




        self.windowSizer = wx.BoxSizer()
        self.windowSizer.Add(self.panel, wx.ID_ANY, 13, wx.ALL | wx.EXPAND, 13)

        pub.subscribe(self.amount, "amount")

        for n in range(3):
            pub.subscribe(self.row, n)


    def amount(self, amount):
        for n in range(amount.data):
            [].append(wx.StaticText(self.panel, id=n, label='test', pos=(20, 30 * n)))
        N = amount.data
        return N

    #I'm having problem here. I can't get N and I don't know how to SetLabel in the static text above 
    def row(self, row):
        for n in range(N):
            [].SetLabel(str(row.data))




if __name__=='__main__':
        app=wx.App(False)
        frame1=Childframe(None)
        frame=Mainframe(None)
        frame.Show()
        app.MainLoop()
4

1 に答える 1

0

問題は、Childframe の作成とメッセージの送信の間のタイミングが間違っていることです。メインフレームがメッセージを送信するとき、新しいフレームはまだ存在しないため、メッセージは誰にも受信されません。次の代わりに、ボタンにバインドされたイベントを分割してみてください。

 self.button.Bind(wx.EVT_BUTTON, self.Onbutton and self.afterOnbutton)

試す:

self.button.Bind(wx.EVT_BUTTON, self.afterOnbutton)

他の関数 Onbutton が後で呼び出されるように afterOnbutton のコードを変更します。

    def afterOnbutton(self):
        wx.CallAfter(self.Onbutton)
        var = self.editquote1.GetValue()
        if len(var)==9:
            self.new = Childframe(None)
            self.new.Show()

    def Onbutton(self):
        var = self.editquote1.GetValue()
        if len(var)==9:
            self.test.SetLabel('True')
         ... same ...
于 2013-09-19T15:13:44.330 に答える