First Off. Total newbie here and first thread so go easy on me. I used the tut from the Starting wxPython (GUI code) thread. I have converted it to a text editor and added some annoying features to mess with my boss. I am able to open a file, edit it in the window but when I go to save, it saves a empty file. I have searched the docs, this thread as well as others and I cannot figure it out. Also I know there must be a way to bind my annoying "are you sure" messages instead of writing it twice, so any suggestions would be great. The code is as follows:
import wx
import os
WINDOW_WIDTH = 400
WINDOW_HEIGHT = 400
class MainFrame(wx.Frame):
"""We've finally added menubars."""
def __init__(self):
wx.Frame.__init__(self, None, title = 'Bob\'s New Editor',
pos = (200,75), size = (WINDOW_WIDTH, WINDOW_HEIGHT))
self.Bind(wx.EVT_CLOSE, self.OnClose)
# Menubar.
self.menuBar = wx.MenuBar()
self.menuFile = wx.Menu() # Create a dropdown menu for 'File'
self.menuInfo = wx.Menu() # Create a dropdown menu for 'Info'
self.SetMenuBar(self.menuBar) # Tell the main frame what menubar to use.
self.menuBar.Append(self.menuFile, '&File') # Add a menu.
self.loadItem = self.menuFile.Append(-1, '&Load') # Add an item to the menu.
self.Bind(wx.EVT_MENU, self.loadEvent, self.loadItem)
# Bind an event to the menu item. Note how for menu items the Bind format is different?
# I specify the widget to be bound as the last parameter, not just before 'Bind', as I usually do.
# You can use this version for any binding you do, if you want. It's necessary for menus, though.
self.exitItem = self.menuFile.Append(-1, 'E&xit')
self.Bind(wx.EVT_MENU, self.exitEvent, self.exitItem)
self.menuBar.Append(self.menuInfo, '&Info')
# Add another menu. Note how their order on the menubar depends on the order they appear in your code?
self.aboutItem = self.menuInfo.Append(-1, '&About')
self.Bind(wx.EVT_MENU, self.aboutEvent, self.aboutItem)
self.helpItem = self.menuInfo.Append(-1, '&Help')
self.Bind(wx.EVT_MENU, self.helpEvent, self.helpItem)
# Start of sizers and widgets contained within.
self.background = wx.Panel(self)
self.transferBtn = wx.Button(self.background, label = 'Load')
self.transferBtn.Bind(wx.EVT_BUTTON, self.loadEvent)
self.saveBtn = wx.Button(self.background, label='Save')
self.saveBtn.Bind(wx.EVT_BUTTON, self.saveEvent)
self.inputArea = wx.TextCtrl(self.background, style=wx.TE_PROCESS_ENTER | wx.HSCROLL)
self.inputArea.Bind(wx.EVT_TEXT_ENTER, self.transferEvent)
self.transferArea = wx.TextCtrl(self.background, style = wx.TE_MULTILINE)
self.transferArea.SetBackgroundColour("White")
self.horizontalBox = wx.BoxSizer()
self.horizontalBox.Add(self.inputArea, proportion = 1, border = 0)
self.horizontalBox.Add(self.transferBtn, proportion = 0, border = 0)
self.horizontalBox.Add(self.saveBtn, proportion = 0, border = 0)
self.verticalBox = wx.BoxSizer(wx.VERTICAL)
self.verticalBox.Add(self.horizontalBox, proportion = 0, flag = wx.EXPAND, border = 0)
self.verticalBox.Add(self.transferArea, proportion = 1, flag = wx.EXPAND, border = 0)
self.background.SetSizer(self.verticalBox)
self.Show()
def loadEvent(self, event):
"""Create a load dialog box, load a file onto transferArea,
then destroy the load box"""
loadBox = wx.FileDialog(self, message="Open",
defaultDir="C:\\", defaultFile="", style=wx.OPEN)
if loadBox.ShowModal() == wx.ID_OK: # When the user clicks 'Open' , do this:
filename = loadBox.GetPath()
target = open(filename, 'r')
self.transferArea.SetValue(target.read())
target.close()
loadBox.Destroy()
# destroys the box so that we main return to program
def saveEvent(self,event):
saveBox = wx.FileDialog(self, message="Save",
defaultDir="C:\\", defaultFile="", style=wx.SAVE)
if saveBox.ShowModal() == wx.ID_OK: # When the user clicks 'Save' , do this:
filename = saveBox.GetPath()
target = open(filename, 'w')
self.transferArea.GetValue()
target.close()
saveBox.Destroy()
def OnClose(self, event):
dlg = wx.MessageDialog(self,
"Do you really want to close this application?",
"Confirm Exit", wx.OK|wx.CANCEL|wx.ICON_QUESTION)
result = dlg.ShowModal()
dlg.Destroy()
if result == wx.ID_OK:
self.Destroy()
exitInfo = """Are you sure you want to exit?"""
exitBox = wx.MessageDialog(self, message=exitInfo, caption='Exit',
style=wx.ICON_INFORMATION | wx.STAY_ON_TOP | wx.OK)
if exitBox.ShowModal() == wx.ID_OK: #message will stay until user clicks ok
exitBox.Destroy()
exitInfo = """Are you really sure you want to exit?"""
exitBox = wx.MessageDialog(self, message=exitInfo, caption='Exit',
style=wx.ICON_INFORMATION | wx.STAY_ON_TOP | wx.OK)
if exitBox.ShowModal() == wx.ID_OK: #message will stay until user clicks ok
exitBox.Destroy()
exitInfo = """Are you really really sure you want to exit?"""
exitBox = wx.MessageDialog(self, message=exitInfo, caption='Exit',
style=wx.ICON_INFORMATION | wx.STAY_ON_TOP | wx.OK)
if exitBox.ShowModal() == wx.ID_OK: #message will stay until user clicks ok
exitBox.Destroy()
def exitEvent(self, event):
"""Exit Program"""
exitInfo = """Are you sure you want to exit?"""
exitBox = wx.MessageDialog(self, message=exitInfo, caption='Exit',
style=wx.ICON_INFORMATION | wx.STAY_ON_TOP | wx.OK)
if exitBox.ShowModal() == wx.ID_OK: #message will stay until user clicks ok
exitBox.Destroy()
exitInfo = """Are you really sure you want to exit?"""
exitBox = wx.MessageDialog(self, message=exitInfo, caption='Exit',
style=wx.ICON_INFORMATION | wx.STAY_ON_TOP | wx.OK)
if exitBox.ShowModal() == wx.ID_OK: #message will stay until user clicks ok
exitBox.Destroy()
exitInfo = """Are you really really sure you want to exit?"""
exitBox = wx.MessageDialog(self, message=exitInfo, caption='Exit',
style=wx.ICON_INFORMATION | wx.STAY_ON_TOP | wx.OK)
if exitBox.ShowModal() == wx.ID_OK: #message will stay until user clicks ok
exitBox.Destroy()
self.Destroy()
def helpEvent(self, event):
"""Writes helpful infprmation to the transfer area"""
self.transferArea.SetValue("""Could put help file here""")
def aboutEvent(self, event):
"""Pops up a little message box to describe"""
aboutInfo = """Hello there Kevin!
I sure do like this Python stuff!!"""
aboutBox = wx.MessageDialog(self, message=aboutInfo, caption='About',
style=wx.ICON_INFORMATION | wx.STAY_ON_TOP | wx.OK)
if aboutBox.ShowModal() == wx.ID_OK: #message will stay until user clicks ok
aboutBox.Destroy()
def transferEvent(self, event):
"""Moves data in inputArea onto transferArea"""
self.transferArea.SetValue(self.inputArea.GetValue())
app = wx.App(redirect=False)
window = MainFrame()
app.MainLoop()