Replacer - My First Python GUI
I finally got around to digging into some GUI programming with Python. I had been working on a text-based turn-based command line game and decided to take a break from that in order to pick up some GUI related knowledge. I opted for something simple and wound up with this search and replace program. There’s not much to it. Enter the text to change, the string to look for, the string to replace it with, and press the button to view the altered text.
I have to admit that it took me a while to write this bit of code. I’m not sure how much time I spent looking at all sorts of web sites for examples, different GUI editors, and other information. Finally I decided that I’d had enough of looking and needed to actually start making something. I already had wxPython installed in order to run SPE, so I opted to use the wxWidgets for my GUI program.
Now maybe this is just my lack of Python knowledge speaking, but I had one heck of a time finding examples and tutorials that really made it clear to me what went into even a simple graphical interface. Maybe I was looking in the wrong places for help. I don’t know. I was pretty happy when I found a good snippet of code on a forum, so that is what I used as the basis for what I wrote.
#!/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()
Tags: gui, Learning, Python, replacer, wxpython 