471,311 Members | 1,820 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,311 software developers and data experts.

wxPython - TextCtrl widget problem

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. I know that the reason is because I have specified the parent as self (i.e. Frame). How do I get the new file to display in the textctrl widget 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 and I can't figure out a way around it.

Here's the code. Again, I'm trying to get the textctrl widget that is created in the "onNew" method of the "Frame" class to appear in the panel that I created. I know that I didn't have to make a separate class for my panel in the following code, but I want to write an application with multiple panels, so being able to change the text in one or more panels from the menu bar is a must.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/env python
  2.  
  3. import wx
  4.  
  5. class MyGUI(wx.App):
  6.     def OnInit(self):
  7.         self.frame = Frame(None, title = "MyGUI", size = (640, 480))
  8.         self.frame.CenterOnScreen()
  9.         self.frame.Show()
  10.         self.SetTopWindow(self.frame)
  11.         return True
  12.  
  13.  
  14. class Frame(wx.Frame):
  15.     def __init__(self, *args, **kwargs):
  16.         wx.Frame.__init__(self, *args, **kwargs)
  17.         self.panel = CreateMainPanel(self)
  18.         filemenu = wx.Menu()
  19.     filemenu.Append(wx.ID_NEW, "&New")    
  20.     menubar = wx.MenuBar()
  21.     menubar.Append(filemenu, "&File")
  22.     self.SetMenuBar(menubar)    
  23.  
  24.     wx.EVT_MENU(self, wx.ID_NEW, self.onNew)
  25.  
  26.  
  27.     def onNew(self, event):
  28.         """
  29.     Event handler for creating a new file
  30.     """
  31.         self.editor = wx.TextCtrl(self, wx.ID_ANY, style = wx.TE_MULTILINE)
  32.         self.SetTitle("New file -- Unsaved")
  33.  
  34.  
  35. class CreateMainPanel(wx.Panel):
  36.     """
  37.     Creates new panel with a button and textctrl widget on it
  38.     """
  39.     def __init__(self, *args):
  40.         wx.Panel.__init__(self, *args)
  41.  
  42.     self.container1 = wx.BoxSizer(wx.VERTICAL)
  43.     self.container1.Add((0,50), 0, 0)
  44.     self.container1.Add(wx.Button(self, wx.ID_ANY, "Do Something"))
  45.     self.container1.Add((0,50), 0, 0)
  46.  
  47.     self.container2 = wx.GridBagSizer(hgap = 5, vgap = 5)
  48.     self.container2.Add(self.container1, pos = (0, 0))
  49.     self.container2.Add(wx.TextCtrl(self, wx.ID_ANY, style = wx.TE_MULTILINE),
  50.                                     span = (3, 2), pos = (0, 1), 
  51.                     flag = wx.EXPAND | wx.ALL, border = 15)
  52.     self.container2.AddGrowableCol(1)
  53.     self.container2.AddGrowableRow(2)
  54.     self.SetSizer(self.container2)
  55.  
  56.  
  57. def main():
  58.     app = MyGUI()
  59.     app.MainLoop()
  60.  
  61.  
  62. if __name__ == "__main__":
  63.  
  64.     main()
  65.  
Sep 24 '07 #1
1 4475
first of all, i beleive that u have not provided the complete code here. Also the requirements are NOT clear. Hoever, i've listed few things here which may help you out.

In. OnNew method, you are creating a TextCtrl. DO NOT do that. Rather allow the user to type text in the existing TextCtrl. For this, you should change the code at line 49: before adding the textCtrl to Container2, create it and assign it to self.TextCtrl which you can refer to later.
Nov 15 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Logan | last post: by
5 posts views Thread by Jared Russell | last post: by
1 post views Thread by sillyemperor | last post: by
1 post views Thread by PeterG | last post: by
4 posts views Thread by MrQ | last post: by
4 posts views Thread by Jimmy | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.