In the following code, I have created a panel with a button and a textctrl
object on it. I have also created a menubar that will create a new text file
(i.e. textctrl object). My problem is that when I create a new file, it
displays the textctrl over the top left of my existing panel. How do I get the
new file to display in the textctrl on my panel? I am having the save problems
when trying to open a text file. It simply displays the new textctrl over the
top left of my panel. However, I think that if I see an example or two, I could
figure both problems out as they are probably related.
#!/usr/bin/env python
import wx
import os
class MyGUI(wx.App):
def OnInit(self):
self.frame = Frame(None, title = "MyGUI", size = (600, 480))
self.frame.CenterOnScreen()
self.frame.Show()
self.SetTopWindow(self.frame)
return True
class Frame(wx.Frame):
def __init__(self, *args, **kwargs):
wx.Frame.__init__(self, *args, **kwargs)
self.panel = CreateMainPanel(self)
filemenu = wx.Menu()
filemenu.Append(wx.ID_NEW, "&New")
menubar = wx.MenuBar()
menubar.Append(filemenu, "&File")
self.SetMenuBar(menubar)
wx.EVT_MENU(self, wx.ID_NEW, self.onNew)
def onNew(self, event):
"""
Event handler for creating a new file
"""
self.editor = wx.TextCtrl(self, wx.ID_ANY, size = (400, 250),
style = wx.TE_MULTILINE)
self.SetTitle("New file -- Unsaved")
class CreateMainPanel(wx.Panel):
"""
Creates new panel with a button and textctrl widget on it
"""
def __init__(self, *args):
wx.Panel.__init__(self, *args)
self.container1 = wx.BoxSizer(wx.VERTICAL)
self.container1.Add((0,50), 0, 0)
self.container1.Add(wx.Button(self, wx.ID_ANY, "Do Something"))
self.container1.Add((0,50), 0, 0)
self.container2 = wx.GridBagSizer(hgap = 5, vgap = 5)
self.container2.Add(self.container1, pos = (0, 0))
self.container2.Add(wx.TextCtrl(self, wx.ID_ANY, style = wx.TE_MULTILINE),
span = (3, 2), pos = (0, 1),
flag = wx.EXPAND | wx.ALL, border = 15)
self.container2.AddGrowableCol(1)
self.container2.AddGrowableRow(2)
self.SetSizer(self.container2)
def main():
app = MyGUI()
app.MainLoop()
if __name__ == "__main__":
main()