Hey everyone again, so I am having a lot of problems with function in wxPython, this time, I need to know how, or if it is possible, to add new widgets to the main frame from a function, so here is my code:

import wx

class Example(wx.Frame):

    def __init__(self,parent,id):

        wx.Frame.__init__(self,parent,id,'Example', size=(312,170))
        panel=wx.Panel(self)

        ntext=wx.StaticText(panel, -1, "Name: ", pos=(3,6))
        self.name=wx.TextCtrl(panel, -1, "Your name", size=(220, -1), pos=(60,2))
        self.name.SetInsertionPoint(0)

        Namo=wx.Button(panel, -1, "Namo", pos=(150,50), size=(60,30))
        self.Bind(wx.EVT_BUTTON, self.GetName, Namo)

    def GetName(self, panel):

        print(self.name.GetValue())

        Quit=wx.Button(panel, -1, "Quit", pos=(50,50), size=(60,30))
        self.panel.Bind(wx.EVT_BUTTON, self.closebutton, Quit)

        ntext=wx.StaticText(panel, -1, "New Text: ", pos=(3,85))
        self.panel.name=wx.TextCtrl(panel, -1, "Your Static Text", size=(220, -1), pos=(60,85))
        self.panel.name.SetInsertionPoint(0)

    def closebutton(self, panel):

        self.Close(True)
        
if __name__ == '__main__':
    app=wx.PySimpleApp()
    frame=Example(parent=None,id=-1)
    frame.Show()
    app.MainLoop()

and when i click on the button "Namo", i get this error:

Traceback (most recent call last):
File "C:\Users\H4CkiNG\Desktop\Example\example.py", line 21, in GetName
Quit=wx.Button(panel, -1, "Quit", pos=(50,50), size=(60,30))
File "C:\Python26\lib\site-packages\wx-2.8-msw-unicode\wx\_controls.py", line 87, in __init__
_controls_.Button_swiginit(self,_controls_.new_Button(*args, **kwargs))
TypeError: in method 'new_Button', expected argument 1 of type 'wxWindow *'

And I dont really know what to do. Thanks, Dan08.

I played with your code, but did not know what exactly you want to accomplish:

import wx

class Example(wx.Frame):

    def __init__(self,parent,id):

        wx.Frame.__init__(self,parent,id,'Example', size=(312,170))
        self.panel=wx.Panel(self)


        ntext=wx.StaticText(self.panel, -1, "Name: ", pos=(3,6))
        self.name=wx.TextCtrl(self.panel, -1, "Your name", size=(220, -1), pos=(60,2))
        self.name.SetInsertionPoint(0)

        Namo=wx.Button(self.panel, -1, "Namo", pos=(150,50), size=(60,30))
        self.Bind(wx.EVT_BUTTON, self.GetName, Namo)
        Quit=wx.Button(self.panel, -1, "Quit", pos=(50,50), size=(60,30))
        self.panel.Bind(wx.EVT_BUTTON, self.closebutton, Quit)


    def GetName(self, panel):
        ntext=wx.StaticText(self.panel, -1, "New Text: ", pos=(3,85))
        self.panel.name=wx.TextCtrl(self.panel, -1, "Your Static Text", size=(220, -1), pos=(60,85))
        self.panel.name.SetInsertionPoint(0)

        print(self.name.GetValue())


    def closebutton(self, panel):

        self.Close(True)
        
if __name__ == '__main__':
    app=wx.PySimpleApp()
    frame=Example(parent=None,id=-1)
    frame.Show()
    app.MainLoop()
commented: Thank you very much. Great member +1

Thank you very much. Just one more thing, because I dont want to create another thread just to ask this. Ok so, is there a way to delete widgets from the form instead of just adding them. Thanks Dan08

Thank you very much. Just one more thing, because I dont want to create another thread just to ask this. Ok so, is there a way to delete widgets from the form instead of just adding them. Thanks Dan08

I did quick google and this is what I found:
http://www.wxpython.org/docs/api/wx.Sizer-class.html#Replace

This is for replacing something with something else, which is maybe more typical use than reducing the widgets.

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.