在wxPython中,如何在wxpython类中访问静态文本(或任何)控件的值

时间:2018-03-18 18:56:03

标签: wxpython

import wx
from mailmerge import MailMerge

tfields=[]

########################################################################
class WizardPage(wx.Panel):
    """"""
    #----------------------------------------------------------------------
    def __init__(self, parent, title=None):
        """Constructor"""
        wx.Panel.__init__(self, parent)



        if title:
            if title == "Narrative Setup":
               self.sizer1 = wx.BoxSizer(wx.VERTICAL)
               self.SetSizer(self.sizer1)
               self.xtitle = wx.StaticText(self, -1, title)
               self.xtitle.SetFont(wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD))
               self.sizer1.Add(self.xtitle, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
               self.sizer1.Add(wx.StaticLine(self, -1), 0, wx.EXPAND|wx.ALL, 5)
               self.tmplButton = nextButton = wx.Button( self, wx.ID_ANY, u"Select Template", wx.DefaultPosition, wx.DefaultSize, 0 ) 
               self.tmplPath = wx.StaticText(self, wx.ID_ANY, style=wx.TE_READONLY|wx.SUNKEN_BORDER)
               self.sizer1.Add( self.tmplButton, 0, wx.ALL, 5 )
               self.sizer1.Add(self.tmplPath, 0, wx.EXPAND, 0)
               self.tmplButton.Bind(wx.EVT_BUTTON, self.GetTemplate) 

               self.SASButton = nextButton = wx.Button( self, wx.ID_ANY, u"Select SAS Datasets", wx.DefaultPosition, wx.DefaultSize, 0 ) 
               self.SASPath = wx.StaticText(self, wx.ID_ANY, style=wx.TE_READONLY|wx.SUNKEN_BORDER)
               self.sizer1.Add( self.SASButton, 0, wx.ALL, 5 )
               self.sizer1.Add(self.SASPath, 0, wx.EXPAND, 0)
               self.SASButton.Bind(wx.EVT_BUTTON, self.GetSAS) 
            elif title == "Page 2":
               arrowbmp = wx.ArtProvider.GetBitmap(wx.ART_GO_FORWARD, wx.ART_BUTTON, (40, 40))

               self.sizer1 = wx.BoxSizer(wx.VERTICAL)
               self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)
               self.sizer3 = wx.BoxSizer(wx.HORIZONTAL)
               self.xtitle = wx.StaticText(self, -1, title)
               self.xtitle.SetFont(wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD))

               self.sizer2.Add(self.xtitle, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
               self.sizer2.Add(wx.StaticLine(self, -1), 0, wx.EXPAND|wx.ALL, 5)
               self.panel1 = wx.Panel(self,-1, style=wx.SUNKEN_BORDER)
               self.panel2 = wx.Panel(self,-1, style=wx.BORDER_RAISED)
               self.panel3 = wx.Panel(self,-1, style=wx.SUNKEN_BORDER)
#               tfieldsbox = wx.ListBox(panel1,choices=[],size=wx.Size(250, 450),pos=wx.Point(0, 15),style = wx.LB_SINGLE)
               self.tfieldsbox = wx.ListBox(self.panel1,choices=[],size=wx.Size(250, 555),style = wx.LB_SINGLE)
               self.sfieldsbox = wx.ListBox(self.panel1,choices=[],size=wx.Size(250, 555),pos=wx.Point(300, 0),style = wx.LB_SINGLE)
               self.btnright = wx.BitmapButton(self.panel2, id = wx.ID_ANY, bitmap = arrowbmp,
                   size = (arrowbmp.GetWidth()+10, arrowbmp.GetHeight()+10),pos = (5,250))

               self.sizer3.Add(self.panel1,7,wx.EXPAND)
               self.sizer3.Add(self.panel2,1,wx.EXPAND)
               self.sizer3.Add(self.panel3,4,wx.EXPAND)
#               panel1.SetBackgroundColour("BLUE")
#               panel2.SetBackgroundColour("GREEN")
#               panel3.SetBackgroundColour("RED")

               self.sizer1.Add(self.sizer2,1,wx.CENTER)
               self.sizer1.Add(self.sizer3,9,wx.EXPAND)

#               box = wx.BoxSizer(wx.HORIZONTAL)
#               box.Add(panel1, 7, wx.EXPAND)
#               box.Add(panel2, 1, wx.EXPAND)
#               box.Add(panel3, 4, wx.EXPAND)

#               self.LoadTemplate()
               self.SetAutoLayout(True)
#               self.SetSizer(box)
               self.SetSizer(self.sizer1)
               self.Layout()

    def GetTemplate( self, event ):
        openFileDialog = wx.FileDialog(self, "Open", "", "", 
                                              "Templates (*.docx)|*.docx", 
                                               wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
        openFileDialog.ShowModal()
        tpath=openFileDialog.GetPath()
        pub.sendMessage("Template_Path", value = tpath)
        self.tmplPath.SetLabel(openFileDialog.GetPath())
        openFileDialog.Destroy()

    def GetSAS( self, event ):
        openDirDialog = wx.DirDialog(self, "Select SAS Dataset Directory", 
                                               style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
        openDirDialog.ShowModal()
        self.SASPath.SetLabel(openDirDialog.GetPath())
        openDirDialog.Destroy()





class PanelOne(wx.Panel):
    """"""
    #----------------------------------------------------------------------
    def __init__(self, parent, title=None):
        """Constructor"""
        wx.Panel.__init__(self, parent)



        if title:
            if title == "Narrative Setup":
               self.sizer1 = wx.BoxSizer(wx.VERTICAL)
               self.SetSizer(self.sizer1)
               self.xtitle = wx.StaticText(self, -1, title)
               self.xtitle.SetFont(wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD))
               self.sizer1.Add(self.xtitle, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
               self.sizer1.Add(wx.StaticLine(self, -1), 0, wx.EXPAND|wx.ALL, 5)
               self.tmplButton = nextButton = wx.Button( self, wx.ID_ANY, u"Select Template", wx.DefaultPosition, wx.DefaultSize, 0 ) 
               self.tmplPath = wx.StaticText(self, wx.ID_ANY, style=wx.TE_READONLY|wx.SUNKEN_BORDER)
               self.sizer1.Add( self.tmplButton, 0, wx.ALL, 5 )
               self.sizer1.Add(self.tmplPath, 0, wx.EXPAND, 0)
               self.tmplButton.Bind(wx.EVT_BUTTON, self.GetTemplate) 

               self.SASButton = nextButton = wx.Button( self, wx.ID_ANY, u"Select SAS Datasets", wx.DefaultPosition, wx.DefaultSize, 0 ) 
               self.SASPath = wx.StaticText(self, wx.ID_ANY, style=wx.TE_READONLY|wx.SUNKEN_BORDER)
               self.sizer1.Add( self.SASButton, 0, wx.ALL, 5 )
               self.sizer1.Add(self.SASPath, 0, wx.EXPAND, 0)
               self.SASButton.Bind(wx.EVT_BUTTON, self.GetSAS) 

    def GetTemplate( self, event ):
        openFileDialog = wx.FileDialog(self, "Open", "", "", 
                                              "Templates (*.docx)|*.docx", 
                                               wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
        openFileDialog.ShowModal()
        tpath=openFileDialog.GetPath()
#        pub.sendMessage("Template_Path", value = tpath)
        self.tmplPath.SetLabel(openFileDialog.GetPath())
        openFileDialog.Destroy()

    def GetSAS( self, event ):
        openDirDialog = wx.DirDialog(self, "Select SAS Dataset Directory", 
                                               style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
        openDirDialog.ShowModal()
        self.SASPath.SetLabel(openDirDialog.GetPath())
        openDirDialog.Destroy()



class PanelTwo(wx.Panel):
    """"""
    #----------------------------------------------------------------------
    def __init__(self, parent, title=None):
        """Constructor"""
        wx.Panel.__init__(self, parent)



        if title:
            if title == "Page 2":
               self.Bind(wx.EVT_SHOW, self.PanelShown)
               print("create panel 2")
               arrowbmp = wx.ArtProvider.GetBitmap(wx.ART_GO_FORWARD, wx.ART_BUTTON, (40, 40))

               self.sizer1 = wx.BoxSizer(wx.VERTICAL)
               self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)
               self.sizer3 = wx.BoxSizer(wx.HORIZONTAL)
               self.xtitle = wx.StaticText(self, -1, title)
               self.xtitle.SetFont(wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD))

               self.sizer2.Add(self.xtitle, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
               self.sizer2.Add(wx.StaticLine(self, -1), 0, wx.EXPAND|wx.ALL, 5)
               self.panel1 = wx.Panel(self,-1, style=wx.SUNKEN_BORDER)
               self.panel2 = wx.Panel(self,-1, style=wx.BORDER_RAISED)
               self.panel3 = wx.Panel(self,-1, style=wx.SUNKEN_BORDER)
#               tfieldsbox = wx.ListBox(panel1,choices=[],size=wx.Size(250, 450),pos=wx.Point(0, 15),style = wx.LB_SINGLE)
               self.tfieldsbox = wx.ListBox(self.panel1,choices=[],size=wx.Size(250, 555),style = wx.LB_SINGLE)
               self.sfieldsbox = wx.ListBox(self.panel1,choices=[],size=wx.Size(250, 555),pos=wx.Point(300, 0),style = wx.LB_SINGLE)
               self.btnright = wx.BitmapButton(self.panel2, id = wx.ID_ANY, bitmap = arrowbmp,
                   size = (arrowbmp.GetWidth()+10, arrowbmp.GetHeight()+10),pos = (5,250))

               self.sizer3.Add(self.panel1,7,wx.EXPAND)
               self.sizer3.Add(self.panel2,1,wx.EXPAND)
               self.sizer3.Add(self.panel3,4,wx.EXPAND)
#               panel1.SetBackgroundColour("BLUE")
#               panel2.SetBackgroundColour("GREEN")
#               panel3.SetBackgroundColour("RED")

               self.sizer1.Add(self.sizer2,1,wx.CENTER)
               self.sizer1.Add(self.sizer3,9,wx.EXPAND)

#               box = wx.BoxSizer(wx.HORIZONTAL)
#               box.Add(panel1, 7, wx.EXPAND)
#               box.Add(panel2, 1, wx.EXPAND)
#               box.Add(panel3, 4, wx.EXPAND)

#               self.LoadTemplate()
               self.SetAutoLayout(True)
#               self.SetSizer(box)
               self.SetSizer(self.sizer1)
               self.Layout()

    def GetTemplate( self, event ):
        openFileDialog = wx.FileDialog(self, "Open", "", "", 
                                              "Templates (*.docx)|*.docx", 
                                               wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
        openFileDialog.ShowModal()
        tpath=openFileDialog.GetPath()
        pub.sendMessage("Template_Path", value = tpath)
        self.tmplPath.SetLabel(openFileDialog.GetPath())
        openFileDialog.Destroy()

    def GetSAS( self, event ):
        openDirDialog = wx.DirDialog(self, "Select SAS Dataset Directory", 
                                               style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
        openDirDialog.ShowModal()
        self.SASPath.SetLabel(openDirDialog.GetPath())
        openDirDialog.Destroy()

    def PanelShown(self, value):
        print("In PanelShown")
#        x=pages[self.page_num]
#        print("x=",x)

#        self.text.SetValue(value)
#    def LoadTemplate( self ):
#        print("pagenumber=",self.page_num)

如何从PanelOne类中的tmpPath获取值?我读了很多东西,但我不明白这一点。

        xfile=PanelOne.tmplPath.GetValue()

如何使上述代码有效,请向我解释原因。

        print ("xfile=",xfile)
        if xfile:
            document_1 = MailMerge(xfile)
            fields=document_1.get_merge_fields()
            for f in fields:
                print("newfield=",f)
                tfields.append(f)
            print("new tfields=",tfields)
        else:
            print("nothing to report")
        print("tfields=",tfields)
        for f in tfields:
           print("field=",f)
           Panel2.sfieldsbox.Append(f)

#            print("Fields included in {}: {}".format(xfile,
#                                                 
document_1.get_merge_fields()))


class PanelThree(wx.Panel):
    """"""
    #----------------------------------------------------------------------
    def __init__(self, parent, title=None):
        """Constructor"""
        wx.Panel.__init__(self, parent)



        if title:
            if title == "Narrative Setup":
               self.sizer1 = wx.BoxSizer(wx.VERTICAL)
               self.SetSizer(self.sizer1)
               self.xtitle = wx.StaticText(self, -1, title)
               self.xtitle.SetFont(wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD))
               self.sizer1.Add(self.xtitle, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
               self.sizer1.Add(wx.StaticLine(self, -1), 0, wx.EXPAND|wx.ALL, 5)
               self.tmplButton = nextButton = wx.Button( self, wx.ID_ANY, u"Select Template", wx.DefaultPosition, wx.DefaultSize, 0 ) 
               self.tmplPath = wx.StaticText(self, wx.ID_ANY, style=wx.TE_READONLY|wx.SUNKEN_BORDER)
               self.sizer1.Add( self.tmplButton, 0, wx.ALL, 5 )
               self.sizer1.Add(self.tmplPath, 0, wx.EXPAND, 0)
               self.tmplButton.Bind(wx.EVT_BUTTON, self.GetTemplate) 

               self.SASButton = nextButton = wx.Button( self, wx.ID_ANY, u"Select SAS Datasets", wx.DefaultPosition, wx.DefaultSize, 0 ) 
               self.SASPath = wx.StaticText(self, wx.ID_ANY, style=wx.TE_READONLY|wx.SUNKEN_BORDER)
               self.sizer1.Add( self.SASButton, 0, wx.ALL, 5 )
               self.sizer1.Add(self.SASPath, 0, wx.EXPAND, 0)
               self.SASButton.Bind(wx.EVT_BUTTON, self.GetSAS) 
            elif title == "Page 2":
               arrowbmp = wx.ArtProvider.GetBitmap(wx.ART_GO_FORWARD, wx.ART_BUTTON, (40, 40))

               self.sizer1 = wx.BoxSizer(wx.VERTICAL)
               self.sizer2 = wx.BoxSizer(wx.HORIZONTAL)
               self.sizer3 = wx.BoxSizer(wx.HORIZONTAL)
               self.xtitle = wx.StaticText(self, -1, title)
               self.xtitle.SetFont(wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD))

               self.sizer2.Add(self.xtitle, 0, wx.ALIGN_CENTRE|wx.ALL, 5)
               self.sizer2.Add(wx.StaticLine(self, -1), 0, wx.EXPAND|wx.ALL, 5)
               self.panel1 = wx.Panel(self,-1, style=wx.SUNKEN_BORDER)
               self.panel2 = wx.Panel(self,-1, style=wx.BORDER_RAISED)
               self.panel3 = wx.Panel(self,-1, style=wx.SUNKEN_BORDER)
#               tfieldsbox = wx.ListBox(panel1,choices=[],size=wx.Size(250, 450),pos=wx.Point(0, 15),style = wx.LB_SINGLE)
               self.tfieldsbox = wx.ListBox(self.panel1,choices=[],size=wx.Size(250, 555),style = wx.LB_SINGLE)
               self.sfieldsbox = wx.ListBox(self.panel1,choices=[],size=wx.Size(250, 555),pos=wx.Point(300, 0),style = wx.LB_SINGLE)
               self.btnright = wx.BitmapButton(self.panel2, id = wx.ID_ANY, bitmap = arrowbmp,
                   size = (arrowbmp.GetWidth()+10, arrowbmp.GetHeight()+10),pos = (5,250))

               self.sizer3.Add(self.panel1,7,wx.EXPAND)
               self.sizer3.Add(self.panel2,1,wx.EXPAND)
               self.sizer3.Add(self.panel3,4,wx.EXPAND)
#               panel1.SetBackgroundColour("BLUE")
#               panel2.SetBackgroundColour("GREEN")
#               panel3.SetBackgroundColour("RED")

               self.sizer1.Add(self.sizer2,1,wx.CENTER)
               self.sizer1.Add(self.sizer3,9,wx.EXPAND)

#               box = wx.BoxSizer(wx.HORIZONTAL)
#               box.Add(panel1, 7, wx.EXPAND)
#               box.Add(panel2, 1, wx.EXPAND)
#               box.Add(panel3, 4, wx.EXPAND)

#               self.LoadTemplate()
               self.SetAutoLayout(True)
#               self.SetSizer(box)
               self.SetSizer(self.sizer1)
               self.Layout()

    def GetTemplate( self, event ):
        openFileDialog = wx.FileDialog(self, "Open", "", "", 
                                              "Templates (*.docx)|*.docx", 
                                               wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
        openFileDialog.ShowModal()
        tpath=openFileDialog.GetPath()
        pub.sendMessage("Template_Path", value = tpath)
        self.tmplPath.SetLabel(openFileDialog.GetPath())
        openFileDialog.Destroy()

    def GetSAS( self, event ):
        openDirDialog = wx.DirDialog(self, "Select SAS Dataset Directory", 
                                               style=wx.DD_DEFAULT_STYLE | wx.DD_NEW_DIR_BUTTON)
        openDirDialog.ShowModal()
        self.SASPath.SetLabel(openDirDialog.GetPath())
        openDirDialog.Destroy()







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

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent=parent)
        self.pages = []
        self.page_num = 0
#        pub.subscribe(self.LoadTemplate, "Template_Path")

        self.mainSizer = wx.BoxSizer(wx.VERTICAL)
        self.panelSizer = wx.BoxSizer(wx.VERTICAL)
        btnSizer = wx.BoxSizer(wx.HORIZONTAL)

        # add prev/next buttons
        self.prevBtn = wx.Button(self, label="Previous")
        self.prevBtn.Bind(wx.EVT_BUTTON, self.onPrev)
        btnSizer.Add(self.prevBtn, 0, wx.ALL|wx.ALIGN_RIGHT, 5)

        self.nextBtn = wx.Button(self, label="Next")
        self.nextBtn.Bind(wx.EVT_BUTTON, self.onNext)
        btnSizer.Add(self.nextBtn, 0, wx.ALL|wx.ALIGN_RIGHT, 5)

        # finish layout
        self.mainSizer.Add(self.panelSizer, 1, wx.EXPAND)
        self.mainSizer.Add(btnSizer, 0, wx.ALIGN_RIGHT)
        self.SetSizer(self.mainSizer)


    #----------------------------------------------------------------------
    def addPage(self, pname=None ,name=None, title=None):
        """"""

        panel = name(self, title)
        self.panelSizer.Add(panel, 2, wx.EXPAND)
        pname = panel
        self.pages.append(panel)
        print("pages=",self.pages)
        if len(self.pages) > 1:
            # hide all panels after the first one
            panel.Hide()
            self.Layout()

    #----------------------------------------------------------------------
    def onNext(self, event):
        """"""
        pageCount = len(self.pages)
#        if self.page_num == 0:
#            self.LoadTemplate()
        if pageCount-1 != self.page_num:
            self.pages[self.page_num].Hide()
            self.page_num += 1
#            if self.page_num == 1:
            self.pages[self.page_num].Update()
            self.pages[self.page_num].Show()
            self.panelSizer.Layout()
        else:
            print ("End of pages!")

        if self.nextBtn.GetLabel() == "Finish":
            # close the app
            self.GetParent().Close()

        if pageCount == self.page_num+1:
            # change label
            self.nextBtn.SetLabel("Finish")

    #----------------------------------------------------------------------
    def onPrev(self, event):
        """"""
        pageCount = len(self.pages)
        if self.page_num-1 != -1:
            self.pages[self.page_num].Hide()
            self.page_num -= 1
            self.pages[self.page_num].Show()
            self.panelSizer.Layout()
        else:
            print ("You're already on the first page!")



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

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, None, title="Narratives", size=(900,600))

        self.panel = WizardPanel(self)
        x='Panel1'
        self.panel.addPage(pname=x,name=PanelOne,title="Narrative Setup")
        print("after add Panel1")
        x='Panel2'
        self.panel.addPage(pname=x,name=PanelTwo,title="Page 2")
        print("after add Panel2")
        x='Panel3'
        self.panel.addPage(pname=x,name=PanelThree,title="Page 3")
        print("after add Panel3")
        self.Show()

if __name__ == "__main__":

    def main():
        app = wx.App(False)
        frame = MainFrame()
        app.MainLoop()

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:0)

您的“tmplPath”对象是wxStaticText。 您已使用“SetLabel”更改其显示的文本,因此要获取此文本,您必须使用“GetLabel”:

xfile=PanelOne.tmplPath.GetLabel()

此致 XAV'