Reputation: 24759
Why does the following code create 2 widgets, and not overwrite each other? How would someone reference the first instance vs second instance?
import wx
app = wx.App(False)
frame = wx.Frame(None, -1, "Test", (250,250), (250,250))
panel = wx.Panel(frame, -1)
textbox = wx.TextCtrl(panel, -1, "", (10,10), (135,20))
textbox = wx.TextCtrl(panel, -1, "", (10,40), (135,20))
frame.Show()
app.MainLoop()
Upvotes: 1
Views: 247
Reputation: 9451
There is another reference to your TextCtrl
objects so it is no deleted as you would expect. Your panel
holds a list of all its children. To delete wxPython widget, you have to explicitly call its Destroy()
method. So in your case it would be:
textbox = wx.TextCtrl(panel, -1, "", (10,10), (135,20))
textbox.Destroy()
textbox = wx.TextCtrl(panel, -1, "", (10,40), (135,20))
To be able to access both objects, you either have to do as @jonrsharpe suggests or you can use GetChildren()
method. However holding references to all your widgets in your application yourself is preferred method.
Upvotes: 1
Reputation: 122152
The widgets are created, then assigned to the name. The first one still exists, but it is difficult for you to access it as you have assigned a different object to the name. If you want to still access both of them, try:
textboxes = []
textboxes.append(wx.TextCtrl(panel, -1, "", (10,10), (135,20)))
textboxes.append(wx.TextCtrl(panel, -1, "", (10,40), (135,20)))
Now you can access each by index:
textboxes[0]
Or loop through all of them:
for textbox in textboxes:
Upvotes: 2