Hi
Using a template from the tutorial Fuse offered i made this program:

import wx
import os

WINDOW_WIDTH = 700
WINDOW_HEIGHT = 600

class MainFrame(wx.Frame,object):
    
    def __init__(self):
        
        wx.Frame.__init__(self,None,
                          title = "Paul's Text Editor",
                          pos = (200,75),
                          size = (WINDOW_WIDTH,WINDOW_HEIGHT))

        self.menubar = wx.MenuBar()
        self.menufile = wx.Menu()
        self.menuinfo = wx.Menu()
        self.SetMenuBar(self.menubar)

        self.menubar.Append(self.menufile,'&File')

        self.loadItem = self.menufile.Append(-1,'&Load')

        self.Bind(wx.EVT_MENU,self.loadEvent,self.loadItem)

        self.save = self.menufile.Append(-1,'&Save')
        self.Bind(wx.EVT_MENU,self.saveevent,self.save)

        self.exititem = self.menufile.Append(-1,'E&xit')
        self.Bind(wx.EVT_MENU,self.exitevent,self.exititem)

        self.menubar.Append(self.menuinfo,'&Info')

        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)
        
        


        
        self.background = wx.Panel(self)
        

        
        self.tbut = wx.Button(self.background,label = 'transfer')
        self.tbut.Bind(wx.EVT_BUTTON,self.transferEvent)
        self.first = 1
        self.inarea = wx.TextCtrl(self.background,style=wx.PROCESS_ENTER)
        self.inarea.Bind(wx.EVT_TEXT_ENTER,self.transferEvent)
        self.inarea.SetValue("Enter Data Line by Line")
        self.inarea.Bind(wx.EVT_LEFT_UP,self.clearin)
        self.transarea = wx.TextCtrl(self.background,
                                     style =  wx.TE_MULTILINE)

        self.transarea.SetValue("Or go for it in here!")
        self.transarea.Bind(wx.EVT_LEFT_UP,self.cleartransarea)
        self.horbox = wx.BoxSizer()
        self.horbox.Add(self.inarea,proportion = 1,border=0)
        self.horbox.Add(self.tbut,proportion=0,border = 0)


        self.vbox = wx.BoxSizer(wx.VERTICAL)
        self.vbox.Add(self.horbox,proportion=0,flag=wx.EXPAND,
                      border=0)
        self.vbox.Add(self.transarea,proportion=1,flag=wx.EXPAND,
                      border=0)

        
        self.background.SetSizer(self.vbox)
        self.Show()
        self.loaded=0
        self.Bind(wx.EVT_CLOSE,self.OnExit)
    def cleartransarea(self,event):
        self.transarea.SetValue('')
    def clearin(self,event):
        print event
        self.inarea.SetValue('')


        
    def loadEvent(self,event):
        
        loadbox = wx.FileDialog(self,message='open',
                                defaultDir=os.getcwd(),
                                defaultFile='',
                                style = wx.OPEN)
        if loadbox.ShowModal() == wx.ID_OK:
            self.filename=loadbox.GetPath()
            target = open(self.filename,'rb')
            self.transarea.SetValue(target.read())
            target.close
            self.loaded = 1
        loadbox.Destroy()

        
    def OnExit(self,event):
        self.exitevent(wx.EVT_CLOSE)
        self.Destroy()

        
    def exitevent(self,event):
        if self.loaded:
            checksave = wx.MessageDialog(self,message = 'do you want to save?',
                                         style = wx.YES_NO)
            if checksave.ShowModal() == wx.ID_YES:
                f = open(self.filename,'w')
                f.write(self.transarea.GetValue())
                f.close()
        else:
            if len(self.transarea.GetValue()) >0:
                checksave = wx.MessageDialog(self,message = 'do you want to save?',
                                             style = wx.YES_NO)
                if checksave.ShowModal() == wx.ID_YES:

                    self.saveevent(None)
                
        self.Destroy()
    
    def saveevent(self,event):
        if self.loaded:
            overwrite = wx.MessageDialog(self,message="Would you like to overwrite the file?"
                                         ,style = wx.YES_NO)
            if overwrite.ShowModal()==wx.ID_YES:
                g = wx.MessageDialog(self,message='Saved!',style=wx.OK)
                g.ShowModal()
                f = open(self.filename,'w')
                f.write(self.transarea.GetValue())
                f.close()
        else:
            savebox = wx.FileDialog(self,message = 'Where will you save the file?',
                                    defaultDir="C:\\",
                                    defaultFile = '',
                                    style = wx.OPEN)
            
            if savebox.ShowModal() == wx.ID_OK:
                self.filename = savebox.GetPath()
                target = open(self.filename,'w')
                target.write(self.transarea.GetValue())
                target.close()
            savebox.Destroy()
            


        
    def helpevent(self,event):
        self.transarea.SetValue('''To load a file go to FILE and
press load and navigate to the
file you want and press OK.
To exit press the cross or
FILE --> EXIT''')

    def aboutevent(self,event):
        aboutinfo = 'Made by Paul Thompson.\nMade with a tutorial by Fuse'
        aboutbox = wx.MessageDialog(self,
                                    message=aboutinfo,
                                    caption='About',
                                    style=wx.ICON_INFORMATION|wx.STAY_ON_TOP|wx.OK)
        if aboutbox.ShowModal() == wx.ID_OK:
            aboutbox.Destroy()

    
    def transferEvent(self,event):
        self.transarea.SetValue(self.transarea.GetValue()+'\n'+self.inarea.GetValue())

app=wx.App(redirect=False)
window=MainFrame()
app.MainLoop()
print 'ending'
app.Destroy()

What it is meant to do it be a text editing program. I have it so it saves and loads files and this works fine but i have also got a part which makes the two text input bits start with text inside them to tell the user how they are meant to be used. Then i tried to make an event that when you clicked on them the text that was there disappeared. This is where i get my problem, the text clears as soon as you click in it but then it kinda freezes up and you can't do anything. There are no errors coming up in the console so i was wondering if anyone could help me to fix this.

Thanks :)

Recommended Answers

All 7 Replies

Hahaha, wow. I tried this exact same thing. Actually, I thought about trying it (got as far as using SetValue for the textbox when programme loads), but decided instead to use wx.SearchCtrl.

wx.SearchCtrl is a subclass of wxTextCtrl, so it inherits all of the same stuff, but the benefit is SearchCtrl has a built-in feature for displaying greyed-out text that disappears when the users clicks in the textbox or types in it.

If you use SearchCtrl you'll need to turn off the image, which is a simple matter of calling one of its methods designed to do exactly that, IIRC.

If you read the documentation for EVT_LEFT_DOWN (or UP, I suppose, but you should use DOWN really), you'll notice it says this:

EVT_LEFT_DOWN
Left mouse button down event. The handler of this event should normally call event.Skip() to allow the default processing to take place as otherwise the window under mouse wouldn't get the focus.

http://www.wxpython.org/docs/api/wx.MouseEvent-class.html

Add event.Skip() to the end of the clearin and cleartransarea methods.

Also, I never experienced it freeze up as such. Try right clicking the text box once it has 'frozen' and you'll notice you can type stuff. It's definitely due to the focus.

Yeah i just found that right click stuff then too.
The event.Skip() thing worked flawlessly thanks for pointing that out.
Just wondering also where you find the documentation for wxPython because i have nothing of the sort installed on my computer when i installed all of the wxPython modules

No problem. It's not immediately clear what it means by using event.Skip(), and I only figured it out because the focus issue seemed like what was happening with right-clicking.

I find wxPython documentation and explanations by typing into Google:

topic wxpython

Where 'topic' is what I'm looking for. E.g.:

mouse events wxpython

Gives this as the first link: http://www.wxpython.org/docs/api/wx.MouseEvent-class.html

I have one last question about menus in the above program.

self.save = self.menufile.Append(-1,'&Save')

what is the -1 there for and also why is there a & sign?

IIRC, -1 is the ID of the widget. More here: http://wiki.wxpython.org/wxPython%20Style%20Guide

So you should probably replace -1 with wx.ID_ANY. I'd do that in the tutorial but I can't edit my posts after about 30 minutes after posting.

The ampersand sign is the hotkey for that menu item. Open your programme, press alt (don't hold down), then you'll see the S underlined, and you can press the S key to load that menu item.

yeah i replaced all the -1's in the program with wx.ID_ANY and thanks for the help with the ampersand sign

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.