Re: Trouble calling method from one class to another class

RF <[email protected]> Sun, 25 Apr 2021 11:32:54 -0700 (PDT)
Newsgroups gmane.comp.python.wxpython
Message-ID <[email protected]>
Thank you John for your comments. One at a time if I may.

> The "tab_num" has some logic issues ...

Well, I changed line 67: self.tab_num = 1
to
frame.tab_num = 1
and got: NameError: name 'frame' is not defined.

I'm thinking about this one...I must not have understood you 
correctly...the instance was created in line 95 and must not be 'global' 
since line 67 is complaining about it.

I'm attaching my current pyCore4.py file as pyCore4.2.py (zipped). Line 67 
is the only change: frame.tab_num = 1 

On Saturday, April 24, 2021 at 5:36:19 PM UTC-7 johnf wrote:

> Python code has default values. In this case you don't have to pass any 
> parameters to init the class for it to create an instance of the class.  Of 
> course at some point those values will be needed.  That said, your code is 
> needs the parameters:
>
> super().__init__(parent, id = id, pos= position, size = size, style=style)
>
> You are subclassing the wx.TreeCtrl class and you need to pass those 
> parameters.  In the case of the Geeksforgeeks code they created an instance 
> of the wx.TreeCtrl class directly (you could have also done the same).  
> They passed the parameters that were needed for their project and wxPython 
> defaulted the rest.  BTW I'm not sure but I bet the underlining c++ code 
> has multi-def of the code to allow passing all the parameters - all, some  
> or none.
>
> The "tab_num" has some logic issues.  In the instance of the CoreFrame you 
> create a var "self.tab_num" with the value of one (1).  But what is the 
> name of the CoreFrame instance?  You named it "frame".  Therefore, any time 
> you want to determine value of "tab_num" you would NOT use the following:
>
> CoreFrame.tab_num
>
> you would use 
>
> frame.tab_num.
>
> Why"  Because the class name is CoreFrame and it is not the instance of 
> the class.  The instance of the class is 'frame".
>
> BTW in general I never do as you did above.  Adding the var and allowing 
> it to increment within the instance will only apply to the one instance in 
> this case "frame".  The moment you create a second instance of CoreFrame 
> the value of "tab_num" will start over.  Of course maybe that is what you 
> wanted.
>
> Also in your "add_tab_page" method you set yourself to fail.  It only 
> works the first time.  When you call from outside the instance there is no 
> self.tab_num.
>
> I'll let you think about how to fix that - there several ways!
>
> Johnf
>
>
>
>
>
> On 4/24/21 4:32 PM, RF wrote:
>
> John, thank you for those comments. I've looked at the __init__ docs and 
> have adjusted accordingly.
>
> One comment re the TreeCtrl __init__: I see in some examples I have looked 
> at that they don't include all the parameters and their code runs without 
> giving any errors. For example, this website: 
> https://www.geeksforgeeks.org/wxpython-treectrl/
>
> I don't understand why their codding example runs without error as they 
> have omitted the style, validator and name. In any event, I followed your 
> advice and patterned my __init__ after the options shown in the docs. 
> However, I did make one exception: I omitted the TreeCtrl validator 
> argument as I don't think I need it in the __init__. The line seems to 
> execute OK without and I still am not sure it's optional as the __init__ 
> docs show it in there. So I guess it is optional? Geeksforgeeks omitted 
> three of the arguments and I didn't get any errors when I ran their example 
> code.
>
> I'm attaching pyCode4.py as the update with my changes (based on your 
> comments).
>
> > At this point I stopped because the GUI opened and I'm not sure what you 
> are trying to d.
> At this point, I am just trying to execute the file without getting the 
> error: AttributeError: type object 'CoreFrame' has no attribute 'tab_num' 
> on line 78.
> Then I want to click on any tree node just to fire the add_tab_page() 
> method and see that a new tab page is created.
>
>
> On Sat, Apr 24, 2021 at 3:43 PM john fabiani <[email protected]> wrote:
>
>> Take my advise with a smile because I am NOT the best programmer.  But, I 
>> have to say this is some very confusing code pxCore2.py.  I believe you do 
>> not have a handle on how OOP works within python. Also you copied code 
>> directly out of the wxPython demo but lacked the understanding of what was 
>> being presented.  And I bet this is causing confusion and is also not 
>> allowing you to move forward.
>>
>> When you define a class, you don't normally create an instance of the 
>> class within the definition of the class.
>>
>> self._MyTreeCtrl = MyTreeCtrl is in the __init__ method of MyTreeCtrl.  
>>
>> so comment out "self._MyTreeCtrl = MyTreeCtrl"
>>
>> I believe with wxPython 4.x the super() is used.
>>
>> so
>>
>> wx.TreeCtrl.__init__(self, parent, id, position, size, style)
>>
>> is now 
>>
>> super().__init__(parent, id = id, pos= position, size = size, style=style)
>>
>> You can review the what is needed to init wx.TreeCtrl by looking at the 
>> method in the source code.  In this case there are two signatures:
>>
>>  TreeCtrl()
>>
>> or  TreeCtrl(parent, id=ID_ANY, pos=DefaultPosition, size=DefaultSize, 
>> style=TR_DEFAULT_STYLE, validator=DefaultValidator, name=TreeCtrlNameStr)
>>
>> Therefore, depending on your needs you could just use:
>>
>> super().__init__()
>>
>> At this point I stopped because the GUI opened and I'm not sure what you 
>> are trying to do.  If it is to list a directory there is a widget called 
>> 'FileDialog'.
>>
>> Johnf
>>
>>
>>
>>
>>
>> On 4/24/21 10:01 AM, RF wrote:
>>
>>
>> I'm attaching my small demo test app. I'm having a heck of a time 
>> figuring out how to call add_tab_page() from the MyTreeCtrl class. This 
>> should happen when I just click on a tree node. While this doesn't seem to 
>> make any sense, what I plan to do with this action is open a new notebook 
>> tab and load in the filename into the new tab. But I have to get past this 
>> first: being able to call the add_tab_page() from the MyTreeCtrl class.
>>
>> I'm fairly sure I'm missing something small here.
>>
>> Thank you for any help.
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "wxPython-users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected].
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/wxpython-users/d1e36c62-125a-425c-947e-7bb5820e0184n%40googlegroups.com 
>> <https://groups.google.com/d/msgid/wxpython-users/d1e36c62-125a-425c-947e-7bb5820e0184n%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "wxPython-users" group.
>> To unsubscribe from this group and stop receiving emails from it, send an 
>> email to [email protected].
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/wxpython-users/de54d97a-1794-a4ab-45fd-78c96b356c96%40gmail.com 
>> <https://groups.google.com/d/msgid/wxpython-users/de54d97a-1794-a4ab-45fd-78c96b356c96%40gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
>
>
> -- 
> Ralph Freshour
> -- 
> You received this message because you are subscribed to the Google Groups 
> "wxPython-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
>
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/wxpython-users/CAGgoqdiLSjJTOwi0hVNho%3DkwGbFHE5kn%3D3hQXy%3DL4pkYYraGOA%40mail.gmail.com 
> <https://groups.google.com/d/msgid/wxpython-users/CAGgoqdiLSjJTOwi0hVNho%3DkwGbFHE5kn%3D3hQXy%3DL4pkYYraGOA%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
>

-- 
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion on the web visit https://groups.google.com/d/msgid/wxpython-users/648c0562-5ad6-44d5-9747-ed753ecd5ae7n%40googlegroups.com.
pxCore4.2.py.zip (application/x-zip, 1.4 KB) - not displayed