wxPython objects and their attributes

Hello everyone,

I’m new to wxPython and Python OOP and i dont understand two thing in wxPython.
In the link below i show two examples of code related with two questions i have.
Link: wxPython question examples - Pastebin.com
The questions I have are probably caused by attempt to reference OOP Python tutorials I’ve watched to things which happens in wxPython.

Example 1 - First question

This example shows the bare minimum code to show a window with given title and size.
As far is I know I create an object “myframe” which is an object of “wx.Frame” class.
Thats probably true, because when i call “isinstance(myframe, wx.Frame)” i get in respond “True”.
If so how can I get the values of the attributes back?
In the tutorials and examples of OOP there is usually someting like this “print(object.attribute)”
Unfortunatelly when i try to “print(myframe.title)” i get an error claiming, that my object does not have such an attribute. I tried to get my values back by writing “myframe.dict” which in tutorials returns all attributes, but in this case returns empty set “{}”…

How is it possible, that “myframe” which is an object stores many values, such as title, size and so on without having even a single attribute? How can I get them (values i passed) so that I can use them later?

Example 2 - Second question

This example shows code rewritten form using classes and inheritance. Despite of things from first question I have one additional question.
First I create a “myframe” object of “MainFrame” class, which is a child of “wx.Frame” class. Additionally I add to this object one test attribute named “dog”. Then (from MainFrame class) I call constructor of “Textpanel” class which is child of “wx.StaticText” class.
The wxPython shows window with this text just like I wanted, but I don’t get how does it work.
When i call MainFrame constructor i assign it and in result I have “myframe” object. It exists and I can print my test attrubite “dog” by using “print(myframe.dog)”.
The thing I dont get is calling “Textpanel” from “MainFrame”. This call is assignet to nothig, so it should not exist. If so how is it possible for wxPython to display it? Where is it stored? How can i print my test attribute “cat” when I dont have an objet to call?

Thank you in advance for explanation, I hope someone will be able to do in an understandable way :slight_smile:

First question

You can access the attributes of the frame with getter methods

import wx
app = wx.App()
myframe = wx.Frame(None, title="Hello World", size=(500,500))
print(myframe.GetTitle()) # Prints the frame title
print(myframe.GetSize()) # Prints the frame size

See wx.Frame — wxPython Phoenix 4.1.1 documentation

Second question

I should probably defer to someone who knows more about wxPython’s internals since it seems that is what your asking about. :slightly_smiling_face:

Attributes like myframe.Size should work.

>>> self.Size  # self is a frame
wx.Size(1751, 1251)

If you need to access a widget, you should store a reference, e.g. as attribute or in a dictionary.
It’s common not to store references for static elements like StaticText or Bitmap.
You can still access such a widget by looking at the frame’s children:

>>> self.GetChildren()
WindowList: [<ActiveControls.Notebook object at 0x0C4C8AD0>, <wx._core.ToolBar object at 0x0FA5A6C0>, <wx._core.StatusBar object at 0x10ACEDF0>]
>>> self.Children
WindowList: [<ActiveControls.Notebook object at 0x0C4C8AD0>, <wx._core.ToolBar object at 0x0FA5A6C0>, <wx._core.StatusBar object at 0x10ACEDF0>]

(You can do this recursively.)

For getting started with wxPython you may look at wxGlade and it’s tutorial. It helps you to get the basic layout done.


P.S.: wx.Frame inherits the properties from wx.Window: wx.Window — wxPython Phoenix 4.1.1 documentation

for example 1
import wx

myframe = wx.Frame(None, title=“Hello World”, size=(500,500))

You see the size printed at the console.
For example 2 you should start reviewing how sizers work. You should also look at using a panel to append your text widget too. The second example sort works - but the coding is not how to learn wxPython.

there are loads of getters & setters for the objects and, as you may well know, they are the basis for properties, or public attributes, which are listed after the methods in the brilliant docu of wxPython
and applying all this properly usually leads to a pretty smart gui :sunglasses: