hi everyone.........

I have a task, I have fragmented the task into subtask. I have planned to create a class to each subclass and one parent class to handle the sub tasks. Each subclass are saved in seperate file and all my subclasses and parent class are placed in the same folder.

The parent class is subclass of wx.Frame, and subclasses are subclass of wx.Panel. Each subclass will create a GUI for each task..

ok.. here is problem

When a menu is selected in the parent class, im instantiating the child class(i.e, Panel), which has textbox and two buttons. first button is used to select a file, and second one I have planned to, read the file and pass the values to parent class OR pass the filepath to parent to read and do rest of the work.

How do I do that?

Please tell me whether I am following a correct method, by fragmenting task into small task and assigning it to child class and saving it in a separate file?.

ok, here is the Parent class

import wx
import client
"""My Parent class"""
class Parent(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)
        
        menu=wx.Menu()
        menu.Append(5000, "File")
        menu.AppendSeparator()
        menu.Append(5001, "Exit")
        menubar=wx.MenuBar()
        menubar.Append(menu, "File")
        self.SetMenuBar(menubar)
        self.Bind(wx.EVT_MENU, self.OnFile,id=5000)
        self.Bind(wx.EVT_MENU, self.OnExit,id=5001)
        
    def OnFile(self, event):
        self.cl=client.Child(self, -1)
        
    def OnBrowse(self, event):
        dir=""
        d=x.FileDialog(self, "Choose an Excel file", self.dirname, "", "*.xls", wx.OPEN)
        if d.ShowModal() == wx.ID_OK:
            self.filename=d.GetFilename()
            self.dirname=d.GetDirectory()
            self.filepath.SetValue(os.path.join(self.dirname, self.filename))
            
    def OnUpload(self, event):
        pass
        
    def OnExit(self, event):
        self.Close()

class MyApp(wx.App):
    def OnInit(self):
        frame=Parent(None, -1, "Parent window")
        frame.Show(True)
        self.SetTopWindow(frame)
        return True

if __name__ == '__main__':
    app=MyApp(redirect=False)
    app.MainLoop()

and Child class

import wx, os

"""My Child class"""

class Child(wx.Panel):
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id, size=(300,300))
        box=wx.BoxSizer(wx.HORIZONTAL)
        label=wx.StaticText(self, -1, "File Path:  ")

        self.text=wx.TextCtrl(self, -1)
        browse=wx.Button(self, -1,"Browse")
        upload=wx.Button(self, -1, "Upload")
        self.Bind(wx.EVT_BUTTON, self.OnBrowse, browse)
        self.Bind(wx.EVT_BUTTON, self.OnUpload, upload)
        box.Add(label)
        box.Add(self.text)
        box.Add(browse)
        
        mainbox=wx.BoxSizer(wx.VERTICAL)
        mainbox.Add(box)
        mainbox.Add(upload)
        
        self.SetSizer(mainbox)
        mainbox.Layout()
    def OnBrowse(self, event):
        self.dir=""
        d=wx.FileDialog(self, "Choose an Excel file", self.dir, "", "*.xls", wx.OPEN)
        if d.ShowModal() == wx.ID_OK:
            self.filename=d.GetFilename()
            self.dir=d.GetDirectory()
            self.text.SetValue(os.path.join(self.dir, self.filename))
            
    def OnUpload(self, event):
        pass
    
class MyApp(wx.App):
    def OnInit(self):
        frame=wx.Frame(None, -1, "Child window")
        panel=Child(frame, -1)
        frame.Show(True)
        self.SetTopWindow(frame)
        return True

if __name__ == '__main__':
    app=MyApp()
    app.MainLoop()

thank you,
regards,
kath.

I took a cursory look at your code and made some suggestion, at least managed to give you a way to connect between classes. Normally you would use inheritance to share attributes, but in this case it is difficult because you also inherit wxClasses and they will give you collisions as Python looks through the class attribute search tree.

Your parent ...

# module Class_Child1.py contains the file dialog

import wx
import Class_Child1 as client

"""My Parent class"""
class Parent(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title)
        menu = wx.Menu()
        menu.Append(5000, "File")
        menu.Append(5002, "Upload")
        menu.AppendSeparator()
        menu.Append(5001, "Exit")
        menubar = wx.MenuBar()
        menubar.Append(menu, "File")
        self.SetMenuBar(menubar)
        self.Bind(wx.EVT_MENU, self.OnFile, id=5000)
        self.Bind(wx.EVT_MENU, self.OnExit, id=5001)
        self.Bind(wx.EVT_MENU, self.OnUpload, id=5002)
        
    def OnFile(self, event):
        self.cl = client.Child(self, -1)
        # note that self.cl.text is empty at that point!
        self.SetTitle("--> get then upload filename")
        
    def OnUpload(self, event):
        # this is your connection to the child
        # but you have to call OnFile() first to create self.cl
        try:
            self.filename = self.cl.text.GetValue()
            self.SetFocus()
            self.SetTitle("-->" + self.filename)  # test
        except AttributeError:
            self.cl = client.Child(self, -1)
            self.SetTitle("--> get filename first")  # test
        
    def OnExit(self, event):
        self.Close()

class MyApp(wx.App):
    def OnInit(self):
        frame = Parent(None, -1, "Parent window")
        frame.Show(True)
        self.SetTopWindow(frame)
        return True

if __name__ == '__main__':
    app = MyApp(redirect=False)
    app.MainLoop()

Your first child ...

# save this file as Class_Child1.py

import wx
import os

"""My Child class"""
class Child(wx.Panel):
    def __init__(self, parent, id):
        wx.Panel.__init__(self, parent, id, size=(300,300))
        #box = wx.BoxSizer(wx.HORIZONTAL)
        box = wx.BoxSizer(wx.VERTICAL)
        label = wx.StaticText(self, -1, "File Path:  ")

        self.text = wx.TextCtrl(self, -1, size=(200,20))
        browse = wx.Button(self, -1, "Browse")
        #upload = wx.Button(self, -1, "Upload")
        self.Bind(wx.EVT_BUTTON, self.OnBrowse, browse)
        #self.Bind(wx.EVT_BUTTON, self.OnUpload, upload)
        box.Add(label)
        box.Add(self.text)
        box.Add(browse)
        
        mainbox = wx.BoxSizer(wx.VERTICAL)
        mainbox.Add(box)
        #mainbox.Add(upload)
        
        self.SetSizer(mainbox)
        mainbox.Layout()

    def OnBrowse(self, event):
        self.dir = ""
        d = wx.FileDialog(self, "Choose an Excel file", self.dir, "", "*.xls", wx.OPEN)
        if d.ShowModal() == wx.ID_OK:
            self.filename = d.GetFilename()
            self.dir = d.GetDirectory()
            # self.text will the connection to the parent!
            self.text.SetValue(os.path.join(self.dir, self.filename))
            

if __name__ == '__main__':
    # put this in here to keep from clashing with the parent's MyApp!
    class MyApp(wx.App):
        def OnInit(self):
            frame = wx.Frame(None, -1, "Child window")
            panel = Child(frame, -1)
            frame.Show(True)
            self.SetTopWindow(frame)
            return True
    
    app = MyApp(redirect=False)
    app.MainLoop()
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.