Search code examples
pythonpython-2.7wxpython

Adding executable code to my wxpython gui text boxes


I currently have a working script that lets me create a copy of a backup file, take that copy and rename it to Filename_New, and rename the original file to Filename_Bad.

I am new to creating GUI's, and python code in general, but currently have a gui and would like to tie in the 3 specific pieces of code to the different boxes in the gui so that when you enter the filename in step 1 in the gui it runs that section of my python code.

Not really sure how to integrate these two things together, so any advice would be greatly appreciated. Thanks in advance, hopefully the code below is formatted correctly.

Here is a piece of my python code to execute the copy process.

I have two other variations of the process above that adds _NEW, and _BAD to the other files.

I would like to tie this code to that GUI in a text box where you enter the file name and the code executes when you hit Okay.


### Do all your imports as needed

import wx, wx.lib.newevent

import os, sys, copy, shutil

class Example(wx.Frame):
    def __init__(self, parent, title):
        super(Example, self).__init__(parent, title=title)

        self.InitUI()
        self.Centre()

    def InitUI(self):

        panel = wx.Panel(self)
        font = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT)
        font.SetPointSize(9)
        #### As already mentioned you defined a wx.BoxSizer but later were using
        #### a wx.GridBagSizer. By the way I also changed a little bit the span
        #### and flags of the widgets when added to the wx.GridBagSizer
        sizer = wx.GridBagSizer(1, 1)

        text = wx.StaticText(panel, label="Enter the VR File That Crashed: ")
        sizer.Add(text, pos=(0, 0), span=(1, 2), flag=wx.EXPAND|wx.TOP|wx.LEFT|wx.BOTTOM, border=5)

        #### tc will be used by other methods so it is better to use self.tc
        self.tc = wx.TextCtrl(panel)
        sizer.Add(self.tc, pos=(1, 0), span=(1, 2),
            flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=5)

        #### Changed the label of the buttons
        buttonOk = wx.Button(panel, label="Search File", size=(90, 28))
        buttonClose = wx.Button(panel, label="Do Stuffs", size=(90, 28))
        sizer.Add(buttonOk, pos=(2, 0), flag=wx.ALIGN_CENTER|wx.RIGHT|wx.BOTTOM, border=10)
        sizer.Add(buttonClose, pos=(2, 1), flag=wx.ALIGN_CENTER|wx.RIGHT|wx.BOTTOM, border=10)

        panel.SetSizer(sizer)

        #### This is how you Bind the button to a method so everytime the button
        #### is clicked the method is executed
        buttonOk.Bind(wx.EVT_BUTTON, self.SearchFile)
        buttonClose.Bind(wx.EVT_BUTTON, self.DoStuffs)

    def SearchFile(self, event):
        #### This is how you use the wx.FileDialog and put the selected path in 
        #### the wx.TextCtrl
        dlg = wx.FileDialog(None, message="Select File", style=wx.FD_OPEN|wx.FD_CHANGE_DIR|wx.FD_FILE_MUST_EXIST|wx.FD_PREVIEW)
        if dlg.ShowModal() == wx.ID_OK:
            self.tc.SetValue(dlg.GetPath())
        else:
            pass 

    def DoStuffs(self, event):
        #### This is how you get the path to the selected/typed file and then
        #### do your stuffs
        def copy_vrb(oldvr):
            newvrb = os.path.splitext(oldvr)[0] + "_COPY"
            shutil.copy(oldvr, newvrb + ".vrb")

        def file_rename(oldvr):
            newvrb = os.path.splitext(oldvr)[0] + "_BAD"
            shutil.copy(oldvr, newvrb + ".vr")

        def rename_copy(oldvr):
            newvrb = os.path.splitext(oldvr)[0] + "_NEW"
            shutil.copy(oldvr, newvrb + ".vr")

        oldvrb = self.tc.GetValue()
        copy_vrb(oldvr)
        file_rename(oldvr)
        rename_copy(oldvr)

        print(oldvr)


if __name__ == '__main__':
    app = wx.App()
    ex = Example(None, title='Rename')
    ex.Show()
    app.MainLoop()
else:
    pass

Type in a filename into the gui and have the code execute on that filename.


Solution

  • Welcome to StackOverflow.

    The short answer to your question is that you need to Bind the buttons in your GUI to some method. You can see in the code below how this is done.

    I changed your code a little bit because you were defining a wx.BoxSixer but then you were addidng widgets to a wx.GridBagSizer. Also, I changed the buttons to show you how to search for a file with a wx.FileDialog, just in case you do not want to type the path to the file and because I assumed (perhaps incorrectly) that the Close button is to close the app. No need for this, just click the X and the app closes.

    Code with comments

    ### Do all your imports as needed
    
    import wx, wx.lib.newevent
    
    import os, sys, copy, shutil
    
    class Example(wx.Frame):
        def __init__(self, parent, title):
            super(Example, self).__init__(parent, title=title)
    
            self.InitUI()
            self.Centre()
    
        def InitUI(self):
    
            panel = wx.Panel(self)
            font = wx.SystemSettings.GetFont(wx.SYS_SYSTEM_FONT)
            font.SetPointSize(9)
            #### As already mentioned you defined a wx.BoxSizer but later were using
            #### a wx.GridBagSizer. By the way I also changed a little bit the span
            #### and flags of the widgets when added to the wx.GridBagSizer
            sizer = wx.GridBagSizer(1, 1)
    
            text = wx.StaticText(panel, label="Enter the VR File That Crashed: ")
            sizer.Add(text, pos=(0, 0), span=(0, 2), flag=wx.EXPAND|wx.TOP|wx.LEFT|wx.BOTTOM, border=5)
    
            #### tc will be used by other methods so it is better to use self.tc
            self.tc = wx.TextCtrl(panel)
            sizer.Add(self.tc, pos=(1, 0), span=(0, 2),
                flag=wx.EXPAND|wx.LEFT|wx.RIGHT, border=5)
    
            #### Changed the label of the buttons
            buttonOk = wx.Button(panel, label="Search File", size=(90, 28))
            buttonClose = wx.Button(panel, label="Do Stuffs", size=(90, 28))
            sizer.Add(buttonOk, pos=(2, 0), flag=wx.ALIGN_CENTER|wx.RIGHT|wx.BOTTOM, border=10)
            sizer.Add(buttonClose, pos=(2, 1), flag=wx.ALIGN_CENTER|wx.RIGHT|wx.BOTTOM, border=10)
    
            panel.SetSizer(sizer)
    
            #### This is how you Bind the button to a method so everytime the button
            #### is clicked the method is executed
            buttonOk.Bind(wx.EVT_BUTTON, self.SearchFile)
            buttonClose.Bind(wx.EVT_BUTTON, self.DoStuffs)
    
        def SearchFile(self, event):
            #### This is how you use the wx.FileDialog and put the selected path in 
            #### the wx.TextCtrl
            dlg = wx.FileDialog(None, message="Select File", style=wx.FD_OPEN|wx.FD_CHANGE_DIR|wx.FD_FILE_MUST_EXIST|wx.FD_PREVIEW)
            if dlg.ShowModal() == wx.ID_OK:
                self.tc.SetValue(dlg.GetPath())
            else:
                pass 
    
        def DoStuffs(self, event):
            #### This is how you get the path to the selected/typed file and then
            #### do your stuffs
            oldvrb = self.tc.GetValue()
            print(oldvrb)
    
    
    if __name__ == '__main__':
        app = wx.App()
        ex = Example(None, title='Rename')
        ex.Show()
        app.MainLoop()
    else:
        pass