wxPython的;将值传递给另一个类的wx.TextCtrl

时间:2011-12-02 16:43:56

标签: oop class wxpython argument-passing python-2.7

我想提前感谢所有人花时间回顾这个问题,我确信很多人一开始就对此感到困惑,我对OOP也有点新意,我主要是在过去完成了vbscripts,所以这对我来说是一个新的前沿。

我的问题是我需要:

将值从一个面板传递到另一个面板......     我确信这很简单,但我的头发却变得灰白了。

import wx
import win32com.client

class FinanceInfo(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        zbox = wx.BoxSizer(wx.VERTICAL)
        self.Description = wx.TextCtrl(self, -1, style=wx.TE_READONLY|wx.TE_MULTILINE, size=(200,204))
        zbox.Add(self.Description,0, wx.EXPAND,15)
        self.SetSizer(zbox)

class Frame(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(Frame, self).__init__(*args, **kwargs)
        self.InitUI()
    def InitUI(self):
        panel = wx.Panel(self, -1)
        box1 = wx.BoxSizer(wx.HORIZONTAL)
        box2 = wx.BoxSizer(wx.HORIZONTAL)
        box3 = wx.BoxSizer(wx.HORIZONTAL)
        box4 = wx.BoxSizer(wx.HORIZONTAL)
        box5 = wx.BoxSizer(wx.HORIZONTAL)
        all_box = wx.BoxSizer(wx.VERTICAL)
        overall = wx.BoxSizer(wx.HORIZONTAL)

        nb = wx.Notebook(panel)
        page2 = FinanceInfo(nb)
        nb.AddPage(page2, "Finance Information")

        first = wx.StaticText(panel, label="First Name: ")
        last = wx.StaticText(panel, label="Last Name: ")
        self.DATA = wx.ListBox(panel, style=wx.LB_SINGLE, size=(100,100))
        self.Bind(wx.EVT_LISTBOX, self.OnSelection, id=self.DATA.GetId()) 
        self.CLYa = wx.StaticText(panel, label="")
        self.P2Da = wx.StaticText(panel, label="")
        self.PLYa = wx.StaticText(panel, label="")

        self.FN = wx.TextCtrl(panel, size=(75,-1))
        self.LN = wx.TextCtrl(panel, size=(75,-1))

        Search = wx.Button(panel, label="Search Patient")
        self.Bind(wx.EVT_BUTTON, self.pulldata, id=Search.GetId())
        Close = wx.Button(panel, label="Close Viewer")
        self.Bind(wx.EVT_BUTTON, self.OnClose, id=Close.GetId())

        box1.Add(first, 0, wx.ALL, 5)
        box2.Add(last, 0, wx.ALL, 5)
        box1.Add(self.FN, 1, wx.ALL, 5)
        box2.Add(self.LN, 1, wx.ALL, 5)
        box3.Add(self.DATA, 1 , wx.ALL, 5)
        box4.Add(Search, 0, wx.ALL, 5)
        box5.Add(Close, 0, wx.ALL, 5)

        all_box.Add(box1, 0, wx.LEFT)
        all_box.Add(box2, 0, wx.LEFT)
        all_box.Add(wx.StaticLine(panel), 0, wx.ALL|wx.EXPAND, 5)
        all_box.Add(box3, 0, wx.CENTER)
        all_box.Add(box4, 0, wx.CENTER)
        all_box.Add(box5, 0, wx.CENTER)
        overall.Add(all_box,0,wx.EXPAND)
        overall.Add(nb, 1, wx.EXPAND)

        panel.SetSizer(overall)

        self.SetSize((500, 275))
        self.SetTitle("Maxident Historical Data Viewer")
        self.Centre()
        self.Show(True)

    def OnClose(self, event):
        quit()

    def pulldata(self, event):
        firstname = str(self.FN.GetValue())
        lastname = str(self.LN.GetValue())
        access = pullID(lastname.strip(), firstname.strip())
        access.MoveFirst
        dat = ""
        while not access.EOF:
            a = str(access.Fields("First Name").value)
            b = str(access.Fields("Last Name").value)
            PID = str(access.Fields("Patient Number").value)
            name = str(a + " " + b + " :" + PID)
            self.DATA.Insert(name, 0)
            access.MoveNext()
    def OnSelection(self, event):
        x = str(self.DATA.GetStringSelection())
        y = x.split(":")
        PID = y[1]
        pullfinancedata(PID)

def pullID(lastname, firstname):
    DB = r"C:\Converted DBases\DATA.mdb"
    engine = win32com.client.Dispatch("DAO.DBEngine.36")
    db = engine.OpenDatabase(DB)
    sql = "select [Patient Number], [First Name], [Last Name] from [tPatients] where [Last Name]='" + lastname.upper() + "' and [First Name]='" + firstname.upper() + "'"
    access = db.OpenRecordset(sql)
    return access    

def pullfinancedata(PID):
    DB = r"C:\Converted DBases\DATA.mdb"
    engine = win32com.client.Dispatch("DAO.DBEngine.36")
    db = engine.OpenDatabase(DB)
    sql = "select * from [tPayment History] where [Patient Number]=" + PID
    access = db.OpenRecordset(sql)
    dat = ""
    while not access.EOF:
        PD = "Payment Date:\t" + str(access.Fields("Payment Date").value) + '\n'
        PM = "Payment Method:\t" + str(access.Fields("Payment Method").value) + '\n'
        PP = "Patient Payment:\t" + str(access.Fields("Patient Payment").value) + '\n'
        IP = "Insurance Payment:\t" + str(access.Fields("Insurance Payment").value) + '\n'
        dat = dat + PD + PM + PP + IP + "\n ------------------ \n"
        access.MoveNext()
    """
    THIS IS WHERE I NEED HELP!
    """
    print dat
    """
    I need this dat variable to be passed to the FinanceInfo class and
    i've tried FinanceInfo.Description.SetValue(dat) but its not working
    """
def main():
    ex = wx.App()
    Frame(None)
    ex.MainLoop()

if __name__ == '__main__':
    main()

我也很感激任何其他提示或技巧。不确定我的BoxSizer是否有意义,我主要是在其他地方得到了这些例子。

3 个答案:

答案 0 :(得分:2)

有几种方法可以做到这一点:

  • 保留对每个面板的引用,并将它们绕过willy nilly。然后你可以做self.panelOne.MyTextCtrl.SetValue(self.otherText.GetValue())
  • 之类的东西
  • 使用wx.PostEvent传递信息
  • 使用pubsub

可能还有其他方法,但我更喜欢最后一种方式。您可以在此处阅读一个简单示例:http://www.blog.pythonlibrary.org/2010/06/27/wxpython-and-pubsub-a-simple-tutorial/

答案 1 :(得分:0)

迈克的回答是正确的。对于几个类之间的简单通信,我更喜欢只发送对另一个类的引用。这是一个干净/透明的方法,不需要任何额外的库。见here an example

答案 2 :(得分:0)

我希望你已经解决了......但如果没有,我使用pubsub和adodbapi修改了你的例子。 我使用我的mdb和adodbapi因为逻辑使用OpendDataBase而.movenext在我的Python 2.7安装中不起作用。

enter code here

import wx
from wx.lib.pubsub import Publisher
import win32com.client
import adodbapi
adodbapi.adodbapi.verbose = True 
# adds details to the sample printout

class FinanceInfo(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        Publisher().subscribe(self.showFrame, ("show.mainframe"))
        zbox = wx.BoxSizer(wx.VERTICAL)
        self.Description = wx.TextCtrl(self, -1, style=wx.TE_READONLY|wx.TE_MULTILINE, size=(200,204))
        zbox.Add(self.Description,0, wx.EXPAND,15)
        self.SetSizer(zbox)
    def showFrame(self, msg):
        """
        Shows the frame and shows the message sent in the
        text control
        """
        self.Description.SetValue(msg.data)

class Frame(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(Frame, self).__init__(*args, **kwargs)
        self.InitUI()

    def InitUI(self):
        #self.FinancePanelInfo = FinanceInfo(self)
        panel = wx.Panel(self, -1)
        box1 = wx.BoxSizer(wx.HORIZONTAL)
        box2 = wx.BoxSizer(wx.HORIZONTAL)
        box3 = wx.BoxSizer(wx.HORIZONTAL)
        box4 = wx.BoxSizer(wx.HORIZONTAL)
        box5 = wx.BoxSizer(wx.HORIZONTAL)
        all_box = wx.BoxSizer(wx.VERTICAL)
        overall = wx.BoxSizer(wx.HORIZONTAL)

        nb = wx.Notebook(panel)
        page2 = FinanceInfo(nb)
        nb.AddPage(page2, "Finance Information")

        first = wx.StaticText(panel, label="First Name: ")
        last = wx.StaticText(panel, label="Last Name: ")
        self.DATA = wx.ListBox(panel, style=wx.LB_SINGLE, size=(100,100))
        self.Bind(wx.EVT_LISTBOX, self.OnSelection, id=self.DATA.GetId()) 
        self.CLYa = wx.StaticText(panel, label="")
        self.P2Da = wx.StaticText(panel, label="")
        self.PLYa = wx.StaticText(panel, label="")

        self.FN = wx.TextCtrl(panel, size=(75,-1))
        self.LN = wx.TextCtrl(panel, size=(75,-1))

        Search = wx.Button(panel, label="Search Patient")
        self.Bind(wx.EVT_BUTTON, self.pulldata, id=Search.GetId())
        Close = wx.Button(panel, label="Close Viewer")
        self.Bind(wx.EVT_BUTTON, self.OnClose, id=Close.GetId())

        box1.Add(first, 0, wx.ALL, 5)
        box2.Add(last, 0, wx.ALL, 5)
        box1.Add(self.FN, 1, wx.ALL, 5)
        box2.Add(self.LN, 1, wx.ALL, 5)
        box3.Add(self.DATA, 1 , wx.ALL, 5)
        box4.Add(Search, 0, wx.ALL, 5)
        box5.Add(Close, 0, wx.ALL, 5)

        all_box.Add(box1, 0, wx.LEFT)
        all_box.Add(box2, 0, wx.LEFT)
        all_box.Add(wx.StaticLine(panel), 0, wx.ALL|wx.EXPAND, 5)
        all_box.Add(box3, 0, wx.CENTER)
        all_box.Add(box4, 0, wx.CENTER)
        all_box.Add(box5, 0, wx.CENTER)
        overall.Add(all_box,0,wx.EXPAND)
        overall.Add(nb, 1, wx.EXPAND)

        panel.SetSizer(overall)

        self.SetSize((500, 275))
        self.SetTitle("Maxident Historical Data Viewer")
        self.Centre()
        self.Show(True)

    def OnClose(self, event):
        quit()

    def pulldata(self, event):
        firstname = str(self.FN.GetValue())
        lastname = str(self.LN.GetValue())
        access = pullID(firstname.strip(), lastname.strip())

        dat = ""
        for rec in access:
            a = str(rec[0])
            b = str(rec[1])
            #PID = str(access.Fields("Patient Number").value)
            name = str(a + ":" + b)
            print name
            self.DATA.Insert(name, 0)
            #access.MoveNext()
        access.close

    def OnSelection(self, event):
        x = str(self.DATA.GetStringSelection())
        y = x.split(":")
        PID = y[0]
        #PID = "Rossini Gianni"
        dati = pullfinancedata(PID)
        righe =""
        for line in dati:
            print line
            riga = str(line) 
            righe = righe + riga + "\n"
            Publisher().sendMessage(("show.mainframe"), righe)

def pullID(name, firstname):
    #name = "ROSSINI GIANNI"
    #DB = r"d:\coop&mie_doc\pdci\iscritti_2007.mdb"
    # db = engine.OpenDatabase(DB)
    # data_source = "D:\coop&mie_doc\pdci\iscritti_2007.mdb"
    # mdw ="C:\Programmi\File comuni\System\System.mdw"
    # DSN = "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE=%s;Jet OLEDB:System Database=%s;" % (data_source, mdw)
    SQL_statement = "select [name], [address] from [tessere_2008] where [name] LIKE '" + name.upper() + "%'"
    #SQL_statement = "select [name], [address] from [tessere_2008] "
    #access = db.OpenRecordset(sql)
    # access = engine.Open(sql,conAccess,1,3)
    _databasename = "d:\coop&mie_doc\pdci\iscritti_2007.mdb"
    _table_name= 'tessere_2008'
    _username = ''
    _password = ''
    _mdw =  "C:\Programmi\File comuni\System\System.mdw"
    constr = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;User Id=%s;Password=%s;Jet OLEDB:System Database=%s;' % (_databasename, _username, _password, _mdw)
    #constr = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Jet OLEDB:System Database=%s;' % (_databasename, _mdw)
    conAccess = adodbapi.connect(constr)
    accessdb =  conAccess.cursor()
#    accessdb = msaccess.AccessDb()
#    connAccess = accessdb.connect("D:\coop&mie_doc\pdci\iscritti_2007.mdb", "Yram", "","C:\Programmi\File comuni\System\System.mdw")
    print SQL_statement
    accessdb.execute(SQL_statement)

    print 'result rowcount shows as= %d. (Note: -1 means "not known")' \
             % (accessdb.rowcount,)
#    fields = access.getFields()
#    print fields
#    for item in access:
#      print item
    #get the results
    access = accessdb.fetchmany(1)
    #print them
    for rec in access:
        print rec
    return accessdb    

def pullfinancedata(PID):    
    print "pullfinancedata"
    print PID
    #DB = r"d:\coop&mie_doc\pdci\iscritti_2007.mdb"
    #engine = win32com.client.Dispatch(r'ADODB.Recordset')
    #db = engine.OpenDatabase(DB)
    _databasename = "d:\coop&mie_doc\pdci\iscritti_2007.mdb"
    _table_name= 'tessere_2008'
    _username = ''
    _password = ''
    _mdw =  "C:\Programmi\File comuni\System\System.mdw"
    constr = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;User Id=%s;Password=%s;Jet OLEDB:System Database=%s;' % (_databasename, _username, _password, _mdw)
    #constr = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Jet OLEDB:System Database=%s;' % (_databasename, _mdw)
    conAccess = adodbapi.connect(constr)
    accessdb =  conAccess.cursor()
    #SQL_statement = "select [name], [address] from [tessere_2008] where [name] LIKE '" + name.upper() + "%'"
    SQL_statement = "select * from [Tesseramento] where [anagra_iscritto]='" + PID + "'"
    #access = db.OpenRecordset(sql)
    print SQL_statement
    accessdb.execute(SQL_statement)
    print 'result rowcount shows as= %d. (Note: -1 means "not known")' \
             % (accessdb.rowcount,)
    dat = ""
    #while not access.EOF:
    #    PD = "Payment Date:\t" + str(access.Fields("Payment Date").value) + '\n'
    #    PM = "Payment Method:\t" + str(access.Fields("Payment Method").value) + '\n'
    #    PP = "Patient Payment:\t" + str(access.Fields("Patient Payment").value) + '\n'
    #    IP = "Insurance Payment:\t" + str(access.Fields("Insurance Payment").value) + '\n'
    #    dat = dat + PD + PM + PP + IP + "\n ------------------ \n"
    #    access.MoveNext()
    """
    THIS IS WHERE I NEED HELP!
    """
    #get the results
    access = accessdb.fetchmany(accessdb.rowcount)
    #print them
    #for rec in access:
    #    print rec
    """
    I need this dat variable to be passed to the FinanceInfo class and
    i've tried FinanceInfo.Description.SetValue(dat) but its not working
    """
    return access

def main():
    ex = wx.App()
    Frame(None)
    ex.MainLoop()

if __name__ == '__main__':
    main()