Hello, Python newbie here. I have been using various templates to play around with wxPython and see what I can do and am stuck at the following bit -

I have a MyPage panel. This panel has wxTextCtrl boxes and when the button the panel has is clicked I want to be able to access the information in the wxTextCtrl boxes and be able to manipulate it etc.

I do not know how I can call these wxTextCtrls from the definition-
def check_buttonClick(self,event):

Can anyone advise me?

import wx
from wx.lib.wordwrap import wordwrap

class MyFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY,
            'Introduction to Information Systems', size=(500, 700))

        # create a status bar at the bottom
        self.CreateStatusBar()
        self.SetStatusText("This is the statusbar")

        self.createMenu()
        
                # style=wx.NB_TOP is default
        # could use style=wx.NB_BOTTOM
        nb = wx.Notebook(self, wx.ID_ANY)
        # MyPage(parent, conversion dictionary, preselected choice)
        self.page1 = MyPage(nb, 3)
        self.page2 = MyPage(nb, 2)
        self.page3 = MyPage(nb, 0)
        nb.AddPage(self.page1, "Attributes Table")
        nb.AddPage(self.page2, "ER Diagram")
        nb.AddPage(self.page3, "Relational Data Model")
        # start with page1 active
        self.page1.SetFocus()

    def createMenu(self):
        """creates all the menu items"""
        filemenu = wx.Menu()
       
        filemenu_exit = filemenu.Append(wx.ID_ANY, "E&xit",
            "Quit the program")

        optionmenu = wx.Menu()
        self.optionmenu = optionmenu
        optionmenu.AppendRadioItem(1001, "University Library", "")
        optionmenu.AppendRadioItem(1002, "Online Auction System", "")
        optionmenu.AppendRadioItem(1003, "Example 3", "")
        optionmenu.AppendRadioItem(1004, "Example 4", "")
        optionmenu.AppendRadioItem(1005, "Example 5", "")

        helpmenu = wx.Menu()
        # the optional & allows you to use alt/a
        # the last string argument shows in the status bar
        # on mouse_over
        helpmenu_about = helpmenu.Append(wx.ID_ANY, "&About",
            "About message")

        # create a menu bar at the top for the menu titles
        menuBar = wx.MenuBar()
        menuBar.Append(filemenu, "&File")
        menuBar.Append(optionmenu, "&Option")
        menuBar.Append(helpmenu, "&Help")
        self.SetMenuBar(menuBar)   
        

        # bind the menu events to an action method
        self.Bind(wx.EVT_MENU, self.onMenuAbout, helpmenu_about)
       
        self.Bind(wx.EVT_MENU, self.onMenuExit, filemenu_exit)


    def onMenuAbout(self, event):
        """use the much fancier wx.AboutBox()"""
        # first fill the info object
        info = wx.AboutDialogInfo()
        info.Name = "TeachInfoSys"
        info.Version = "v.1.0"
        info.Copyright = "(C) copyright 2009"
        info.Description = wordwrap(
            "blah nah arre yaar dekho yeh lambi baat hai kissi cheez ke baare mein",
            300, wx.ClientDC(self))
        info.Developers = ["DeveloperPerson"]
        info.License = "Wish upon a star!"
        # now call wx.AboutBox with this info object
        wx.AboutBox(info)

    def onMenuSave(self, event):
        self.SetStatusText("Not implemented yet!")

    def onMenuExit(self, event):
        # exit via wx.EVT_CLOSE event
        self.Close(True)
        

        
class MyPage(wx.Panel):
    """
    each panel instance creates the notbook page content
    from the given conversion dictionary convD
    """
    def __init__(self, parent, preselect):
        wx.Panel.__init__(self, parent, wx.ID_ANY,style=wx.TE_MULTILINE)
         # create another text control for scrolled multiline output
        text_out = wx.TextCtrl(self, wx.ID_ANY, 
            value=wordwrap(
            "info to be added",
            600, wx.ClientDC(self)),
            size=(200, 100),
            style=wx.TE_MULTILINE|wx.TE_READONLY)
        
        check_button = wx.Button(self, id=-1, label='Check', size=(175, 28))        
        check_button.Bind(wx.EVT_BUTTON, self.check_buttonClick)        
        # optional tooltip        
        
        
        labelStart = wx.StaticText(self, wx.ID_ANY, "")
        labelEnd = wx.StaticText(self, wx.ID_ANY, "")
        labelDomain = wx.StaticText(self, wx.ID_ANY, "Domain Description", style=wx.ALIGN_LEFT)
        labelDomain.SetFont(wx.Font(10, wx.MODERN, wx.NORMAL, wx.BOLD))
        labelCampus = wx.StaticText(self, wx.ID_ANY, "Campus", style=wx.ALIGN_LEFT)
        labelCampus.SetFont(wx.Font(10, wx.MODERN, wx.NORMAL, wx.BOLD))
        labelBook = wx.StaticText(self, wx.ID_ANY, "Book", style=wx.ALIGN_LEFT)
        labelBook.SetFont(wx.Font(10, wx.MODERN, wx.NORMAL, wx.BOLD))
        labelCopy = wx.StaticText(self, wx.ID_ANY, "Copy", style=wx.ALIGN_LEFT)
        labelCopy.SetFont(wx.Font(10, wx.MODERN, wx.NORMAL, wx.BOLD))
        labelCustomer = wx.StaticText(self, wx.ID_ANY, "Customer", style=wx.ALIGN_LEFT)
        labelCustomer.SetFont(wx.Font(10, wx.MODERN, wx.NORMAL, wx.BOLD))
        labelFine = wx.StaticText(self, wx.ID_ANY, "Fine", style=wx.ALIGN_LEFT)
        labelFine.SetFont(wx.Font(10, wx.MODERN, wx.NORMAL, wx.BOLD))
              
           
       
        campus = wx.TextCtrl(self, -1, "replace & write attributes here in a comma and space seperated list", size=(300,25), style=wx.ALIGN_LEFT)
        campusList = ("name","location", "phone no.")
        
        
        book = wx.TextCtrl(self, -1, "replace & write attributes here in a comma and space seperated list", size=(300,25), style=wx.ALIGN_LEFT)
        copy = wx.TextCtrl(self, -1, "replace & write attributes here in a comma and space seperated list", size=(300,25), style=wx.ALIGN_LEFT)
        customer = wx.TextCtrl(self, -1, "replace & write attributes here in a comma and space seperated list", size=(300,25), style=wx.ALIGN_LEFT)
        fine = wx.TextCtrl(self, -1, "replace & write attributes here in a comma and space seperated list", size=(300,25), style=wx.ALIGN_LEFT)
       
        
        sizer_v = wx.BoxSizer(wx.VERTICAL)
        sizer_v.Add(labelStart, 0, flag=wx.ALL|wx.EXPAND, border=5)
        sizer_v.Add(labelDomain, 0, flag=wx.ALL|wx.EXPAND, border=5)
        sizer_v.Add(text_out, 0, flag=wx.ALL|wx.EXPAND, border=5)
        sizer_v.Add(labelCampus, 0, flag=wx.ALL|wx.EXPAND, border=5)
        sizer_v.Add(campus, 0, flag=wx.ALL|wx.EXPAND, border=5)
        sizer_v.Add(labelBook, 0, flag=wx.ALL|wx.EXPAND, border=5)
        sizer_v.Add(book, 0, flag=wx.ALL|wx.EXPAND, border=5)
        sizer_v.Add(labelCopy, 0, flag=wx.ALL|wx.EXPAND, border=5)
        sizer_v.Add(copy, 0, flag=wx.ALL|wx.EXPAND, border=5)
        sizer_v.Add(labelCustomer, 0, flag=wx.ALL|wx.EXPAND, border=5)
        sizer_v.Add(customer, 0, flag=wx.ALL|wx.EXPAND, border=5)
        sizer_v.Add(labelFine, 0, flag=wx.ALL|wx.EXPAND, border=5)
        sizer_v.Add(fine, 0, flag=wx.ALL|wx.EXPAND, border=5)
        sizer_v.Add(labelEnd, 0, flag=wx.ALL|wx.EXPAND, border=5)
          # wx.ALL puts the specified border on all sides
        sizer_v.Add(check_button, 0, flag=wx.ALL, border=5)
        
        self.SetSizer(sizer_v)
        
    def check_buttonClick(self,event):        
       """THIS IS WHERE MY PROBLEM IS"""
       """I WANT THE BUTTON TO"""
       """see all the words stored in campus wxTextCtrl (after performing a split)"""
       """and check them against a list like campusList"""
       """and then colour the various words according to if they are in both lists"""
       """or if they were not present in the wxTextCtrl"""
        
      
      

app = wx.App(0)
# create class instance
MyFrame().Show()
# start the event loop
app.MainLoop()

Recommended Answers

All 4 Replies

All you have to do is do something like:

self.page1.campus.GetValue()

And that would return the value of that textCtrl, all of what you need is accessed through self.pagenumber.textctrl

for some reason that gives me an error:

self.page1.campus.GetValue()        
AttributeError: 'MyPage' object has no attribute 'page1'

Does it matter that Page1 is defined in myFrame which the def for the method is in myPage?

I am not sure why but when I put self. in front of all the attributes I wanted to be able to access as well as stuff like
"sizer_v.Add(self.text_out, 0, flag=wx.ALL|wx.EXPAND, border=5)"
then it worked.


But why? I do not understand these Python concepts

Self is useful in classes as it attaches it to the class, put simply if self. is in front of a variable you can then access it from any function in the class as well as outside the class.

Here is an example

class test(object):
    def __init__(self):
        self.x = 1
    def printer(self):
        print self.x
t = test()
t.printer()
print t.x

Hope that sorts things out.


Yay 500th post!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.