#!/usr/bin/env python #---------------------------------------------------------------------- # Name: replacer.py # Purpose: A simple program for learning the basics of some # GUI programming with wxPython. This program is # basically a self-contained search and replace tool. # Dependencies: wxPython #---------------------------------------------------------------------- import wx class ReplacerPanel(wx.Panel): def __init__(self, parent, id): wx.Panel.__init__(self, parent, id) # Create the first static text label (staticText1) # and text control (textarea1). # This is a multiline text area where the user inputs # the string that will be the subject of the search/replace. self.staticText1 = wx.StaticText(self, label="Input Text Here:", pos=wx.Point(10, 15)) self.textarea1 = wx.TextCtrl(self, 1, style=wx.TE_MULTILINE, pos=wx.Point(10, 30), size=wx.Size(300,100), name="input") self.textarea1.SetBackgroundColour((255,255,255)) # Create the second static text label (staticText2) # and text control (textarea2). # This is a multiline text area where the post-replace # output is displayed. self.staticText2 = wx.StaticText(self, label="Replaced Text:", pos=wx.Point(10, 205)) self.textarea2 = wx.TextCtrl(self, 1, style=wx.TE_MULTILINE, pos=wx.Point(10, 220), size=wx.Size(300,100), name="output") self.textarea2.SetBackgroundColour((220,220,220)) # Create the third static text label (staticText3) # and text control (textbox1). # This is a textbox where the user will input the text # to be replaced. self.staticText3 = wx.StaticText(self, label="Text To Replace:", pos=wx.Point(10, 150)) self.textbox1 = wx.TextCtrl(self, 1, pos=wx.Point(10, 165), size=wx.Size(125,-1), name="toReplace") # Create the fourth static text label (staticText4) # and text control (textbox2). # This textbox is for the string that will replace # any matches of the value in textbox1. self.staticText4 = wx.StaticText(self, label="Replace With:", pos=wx.Point(185, 150)) self.textbox2 = wx.TextCtrl(self, 1, pos=wx.Point(185, 165), size=wx.Size(125,-1), name="toReplaceWith") # Create a button with an event binding that triggers # the actual search and replace process. self.button1 = wx.Button(self, id=-1, label="Do It!", pos=wx.Point(325, 30)) self.button1.Bind(wx.EVT_BUTTON, self.button1Click, self.button1) # This is the function that is executed when the button (button1) is # pressed. def button1Click(self, event): inputText = self.textarea1.GetValue() inputToReplace = self.textbox1.GetValue() inputReplaceWith = self.textbox2.GetValue() outputText = inputText.replace(inputToReplace, inputReplaceWith) self.textarea2.SetValue(outputText) # Calls on the ReplacerPanel class to put the GUI together. def main(): app = wx.PySimpleApp() frame = wx.Frame(None, -1, "Replacer", size = (420, 375)) ReplacerPanel(frame,-1) frame.Show(True) app.MainLoop() # Tell the computer to run the main() function. if __name__ == '__main__': main()