I am having problems with wxpython. When I run the following code, it will show the listbox in a small form (it will only show the listbox in about 10×10). I do not know why this is happening and there is not much resources online to help me with this. The listbox will show correctly if i do not add any buttons. Pardon me if it is an easy fix however i am truly stumped. If you do not understand what my problem is, please run the following code and you will see.
import wx
class MyFrame(wx.Frame):
"""make a frame, inherits wx.Frame"""
def __init__(self,parent,id):
# create a frame, no parent, default to wxID_ANY
wx.Frame.__init__(self, parent, id, 'Testing',
pos=(300, 150), size=(600, 500))
panel = wx.Panel(self)
sampleList = ['dsakdsko0', '1', '2', '3', '4']
listBox = wx.ListBox(panel, -1, (4,3), (100, 60), sampleList, wx.LB_SINGLE)
listBox.SetSelection(3)
self.SetBackgroundColour("white")
wx.Button(self, 1, 'Close', (50, 130))
wx.Button(self, 2, 'Random Move', (150, 130), (110, -1))
self.Bind(wx.EVT_BUTTON, self.OnClose, id=1)
self.Bind(wx.EVT_BUTTON, self.OnRandomMove, id=2)
# show the frame
self.Show(True)
#menu bar
status=self.CreateStatusBar()
menubar=wx.MenuBar()
first=wx.Menu()
second=wx.Menu()
first.Append(wx.NewId(),"New","Creates A new file")
first.Append(wx.NewId(),"ADID","Yo")
menubar.Append(first,"File")
menubar.Append(second,"Edit")
self.SetMenuBar(menubar)
def OnClose(self, event):
self.Close(True)
def OnRandomMove(self, event):
screensize = wx.GetDisplaySize()
randx = random.randrange(0, screensize.x - APP_SIZE_X)
randy = random.randrange(0, screensize.y - APP_SIZE_Y)
self.Move((randx, randy))
if __name__=='__main__':
application = wx.PySimpleApp()
frame=MyFrame(parent=None,id=-1)
frame.Show()
# start the event loop
application.MainLoop()
Thank you very very much.
The problem involves the parents. Your buttons’ parent is the frame while the listbox’s parent is the panel. Give the panel to the buttons as their parent too and the issue will go away. By the way, it’s usually not a good idea to assign your own ID numbers, especially such low ones since those might be used. But if you really want to, then follow these instructions from Robin Dunn’s wxPython book:
“You can ensure that wxPython does not use your explicit ID elsewhere in the application by calling the global function wx.RegisterId(). To prevent your program from duplicating wxPython
IDs, you should avoid using ID numbers between the global constants wx.ID_LOWEST and wx.ID_HIGHEST.”
EDIT: Here’s an example of sorts:
Change this:
to this:
Then to Register your ids:
Personally, I would just create my buttons like this instead: