Hi there daniweb, im looking for some help. I have two classes MainWindow(wx.Frame), and AddTask(wx.Dialog). MainWindow contains a listbox called task_list. When AddTask is created I want the user to fill out the required info and and then click a button activating an on_click event. In that event I want to update the list in MainWindow. How ever when I try to do so I get and error: 'MainWindow' object has no attribute task_list.

Here is what I have tried so far (not full code):

class MainWindow(wx.Frame):
   
    def __init__(self, *args, **kwds):
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.create_tasklist()
        self.create_toolbar()
        self.set_properties()
        self.do_layout()
        self.bind_events()

    def create_tasklist(self):
        choice_list = ["TaskOne", "TaskTwo", "TaskThree"]
        self.task_list = wx.ListBox(self, -1, choices = choice_list)
......

    def on_add(self, event):
        '''Adds new task to task_list.'''
        add_task = AddTask(None, -1, "Add Task")
        add_task.ShowModal()
        add_task.Destroy()

......

class AddTask(wx.Dialog):
    '''Custom dialog to add tasks to task list.'''

    def __init__(self, *args, **kwds):
        kwds["style"] = wx.DEFAULT_DIALOG_STYLE
        wx.Dialog.__init__(self, *args, **kwds)

        self.create_widgets()
        self.set_properties()
        self.do_layout()
        self.bind_events()
......

    def on_add_button_click(self, event):
        # Adds task_name to task_list
        MainWindow.task_list.Insert(self.task_name_text.GetValue())

I have also tried other variations though I think this one shows what I am trying to do the best.

I'm not sure, since I don't know much about wxpython.
I always code in .Net

Perhaps you should make the event handling all inside of the MainWindow class

Hopefully that helps, I am about 30% sure.

I dont think thats possible since I need to create a custom dialog (a few of them).

What if you changed

MainWindow.task_list.Insert(self.task_name_text.GetValue())

into

self.task_list.Insert(self.task_name_text.GetValue())

and then have the on_add function in the MainWindow class get the task_list and insert them then?

task_list is a wx.ListBox object held and displayed in the MainWindow. AddTask does not have a ListBox object. However it does need to access MainWindows task_list. So the result of changing that line is an error telling me that AddTask object has no attribute 'task_list'.

Also there is a cancel option on the dialog and if I try to update the list in MainWindow.on_add() it will update no matter how the dialog is closed and we don't want that.

Thank you for the effort though, it is much appreciated.

Oh, here's a better way:

class MainWindow(wx.Frame):
   
    def __init__(self, *args, **kwds):
        kwds["style"] = wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.create_tasklist()
        self.create_toolbar()
        self.set_properties()
        self.do_layout()
        self.bind_events()

    def create_tasklist(self):
        choice_list = ["TaskOne", "TaskTwo", "TaskThree"]
        self.task_list = wx.ListBox(self, -1, choices = choice_list)
......

    def on_add(self, event):
        '''Adds new task to task_list.'''
        add_task = AddTask(None, -1, "Add Task")
        add_task.ShowModal()
        add_task.Destroy()
        #Here is changed!
        if add_task.ok:
                self.task_list.Insert(add_task.taskname)
        

......

class AddTask(wx.Dialog):
    '''Custom dialog to add tasks to task list.'''

    def __init__(self, *args, **kwds):
        kwds["style"] = wx.DEFAULT_DIALOG_STYLE
        wx.Dialog.__init__(self, *args, **kwds)

        self.create_widgets()
        self.set_properties()
        self.do_layout()
        self.bind_events()

......

    def on_add_button_click(self, event):
        #Here it is changed
        self.taskname = self.task_name_text.GetValue()
        #somehow implement
        #if user canceled, then self.ok = false otherwise true

Something like that

I am not sure if this is going to work but I have to say it sure looks like a good idea. Especially since I have an instance of that object created in MainWindow. I will let you know how this turns out!

Thank you so very very much jcao219 for your unrelenting quest to find a solution! I was racking my brain for almost 3 days now.

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.