How to change the value of an existing wxPython object with a file dialog button

796 Views Asked by At

I have been working on creating a python GUI for some work. I would self-describe as a novice when it comes to by Python knowledge. I am using wxPython and wxGlade to help with the GUI development, as well.

The problem is as follows:

I have an empty TextCtrl object and a Button next to it.

The Button is meant to open a FileDialog and populate or replace the TextCtrl with the value of the file location that is selected. I have created the functionality for the button to open the FileDialog but I can't seem to figure out how to populate the TextCtrl with that resulting value.

import wx

class frmCheckSubmital(wx.Frame):
    def __init__(self, *args, **kwds):
        # begin wxGlade: frmCheckSubmitall.__init__
        kwds["style"] = kwds.get("style", 0) | wx.DEFAULT_FRAME_STYLE
        wx.Frame.__init__(self, *args, **kwds)
        self.rbxUtilitySelect = wx.RadioBox(self, wx.ID_ANY, "Utility", choices=["Stormwater", "Sewer", "Water"], majorDimension=1, style=wx.RA_SPECIFY_ROWS)
        self.txtFeaturesPath = wx.TextCtrl(self, wx.ID_ANY, "")
        self.btnSelectFeatures = wx.Button(self, wx.ID_ANY, "Select")
#        selectEvent = lambda event, pathname=txt: self.dialogFeatures(event, pathname)
        self.btnSelectFeatures.Bind(wx.EVT_BUTTON, self.dialogFeatures)
        self.txtPipesPath = wx.TextCtrl(self, wx.ID_ANY, "")
        self.btnSelectPipes = wx.Button(self, wx.ID_ANY, "Select")
        self.bxOutput = wx.Panel(self, wx.ID_ANY)
        self.cbxDraw = wx.CheckBox(self, wx.ID_ANY, "Draw")
        self.btnClear = wx.Button(self, wx.ID_ANY, "Clear")
        self.btnZoom = wx.Button(self, wx.ID_ANY, "Zoom")
        self.btnRun = wx.Button(self, wx.ID_ANY, "Run", style=wx.BU_EXACTFIT)

        self.__set_properties()
        self.__do_layout()
        # end wxGlade

    def __set_properties(self):
        # begin wxGlade: frmCheckSubmitall.__set_properties
        self.SetTitle("Check Submittal")
        self.rbxUtilitySelect.SetSelection(0)
        self.btnSelectFeatures.SetMinSize((80, 20))
        self.btnSelectPipes.SetMinSize((80, 20))
        self.cbxDraw.SetValue(1)
        self.btnClear.SetMinSize((50, 20))
        self.btnZoom.SetMinSize((50, 20))
        # end wxGlade

    def __do_layout(self):
        # begin wxGlade: frmCheckSubmitall.__do_layout
        sizer_1 = wx.BoxSizer(wx.VERTICAL)
        sizer_5 = wx.BoxSizer(wx.VERTICAL)
        sizer_8 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_7 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_6 = wx.BoxSizer(wx.HORIZONTAL)
        sizer_5.Add(self.rbxUtilitySelect, 0, wx.ALIGN_CENTER | wx.BOTTOM, 10)
        lblFeatures = wx.StaticText(self, wx.ID_ANY, "Features: ")
        sizer_6.Add(lblFeatures, 0, wx.ALIGN_CENTER | wx.LEFT, 16)
        sizer_6.Add(self.txtFeaturesPath, 1, 0, 0)
        sizer_6.Add(self.btnSelectFeatures, 0, wx.ALIGN_CENTER_VERTICAL | wx.LEFT | wx.RIGHT, 5)
        sizer_5.Add(sizer_6, 0, wx.EXPAND, 0)
        lblPipes = wx.StaticText(self, wx.ID_ANY, "Pipes: ")
        sizer_7.Add(lblPipes, 0, wx.ALIGN_CENTER | wx.LEFT | wx.RIGHT, 16)
        sizer_7.Add(self.txtPipesPath, 1, 0, 0)
        sizer_7.Add(self.btnSelectPipes, 0, wx.ALIGN_CENTER_VERTICAL | wx.LEFT | wx.RIGHT, 5)
        sizer_5.Add(sizer_7, 0, wx.ALL | wx.EXPAND, 0)
        sizer_5.Add(self.bxOutput, 1, wx.ALL | wx.EXPAND, 10)
        sizer_8.Add(self.cbxDraw, 0, wx.LEFT | wx.RIGHT, 10)
        sizer_8.Add(self.btnClear, 0, wx.RIGHT, 10)
        sizer_8.Add(self.btnZoom, 0, 0, 0)
        sizer_8.Add((20, 20), 1, 0, 0)
        sizer_8.Add(self.btnRun, 0, wx.BOTTOM | wx.RIGHT, 10)
        sizer_5.Add(sizer_8, 0, wx.EXPAND, 0)
        sizer_1.Add(sizer_5, 1, wx.EXPAND, 0)
        self.SetSizer(sizer_1)
        self.Layout()
        self.SetSize((400, 300))
        # end wxGlade
# Begin Dialog Method
    def dialogFeatures(self, event):


    # otherwise ask the user what new file to open
        #with wx.FileDialog(self, "Select the Features File", wildcard="Text files (*.txt)|*.txt",
         #              style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as fileDialog:
        fileDialog = wx.FileDialog(self, "Select the Features File", wildcard="Text files (*.txt)|*.txt",
                                   style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
        if fileDialog.ShowModal() == wx.ID_CANCEL:
            return     # the user changed their mind

        # Proceed loading the file chosen by the user
        pathname = fileDialog.GetPath()
        self.txtFeaturesPath.SetValue = pathname
        self.txtFeaturesPath.SetValue(pathname)
        try:
            with open(pathname, 'r') as file:
                self.txtFeaturesPath = file
        except IOError:
            wx.LogError("Cannot open file '%s'." % newfile)
# End Dialog Method
# end of class frmCheckSubmitall


if __name__ == '__main__':
    app=wx.PySimpleApp()
    frame = frmCheckSubmital(parent=None, id=-1)
    frame.Show()
    app.MainLoop()

I've tried to do several things and I am just burnt out and in need of some help.

Some things I've tried to do: - Add a third argument in the dialog method to return that (just not sure where to assign) - Use a lambda event to try and assign the value with the constructors?

Any help or insight would be greatly appreciated. Thank you!

3

There are 3 best solutions below

2
Rolf of Saxony On

Try:

self.txtFeaturesPath.SetValue(pathname)

You have a few other buggy "features" in your example code, so watch out.

0
MTorres On

I realized that what I was assigning was not the value the fileDialog opens, but was instead the location in ram of the fileDialog object.

the solution was the following

value = fileDialog.Directory + "\\" + fileDialog.filename            
self.txtFeaturesPath.SetValue(value)

thank you!

0
Mike Driscoll On

As others have already pointed out, the way to go is by using the text control's SetValue. But here's a small runnable example:

import wx

class MyPanel(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

        open_file_dlg_btn = wx.Button(self, label="Open FileDialog")
        open_file_dlg_btn.Bind(wx.EVT_BUTTON, self.on_open_file)

        self.file_path = wx.TextCtrl(self)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(open_file_dlg_btn, 0, wx.ALL, 5)
        sizer.Add(self.file_path, 1, wx.ALL|wx.EXPAND, 5)
        self.SetSizer(sizer)

    def on_open_file(self, event):
        wildcard = "Python source (*.py)|*.py|" \
            "All files (*.*)|*.*"        
        dlg = wx.FileDialog(
            self, message="Choose a file",
                    defaultDir='', 
                    defaultFile="",
                    wildcard=wildcard,
                    style=wx.FD_OPEN | wx.FD_MULTIPLE | wx.FD_CHANGE_DIR
        )
        if dlg.ShowModal() == wx.ID_OK:
            paths = dlg.GetPath()
            print("You chose the following file(s):")
            for path in paths:
                print(path)
            self.file_path.SetValue(str(paths))
        dlg.Destroy()


class MyFrame(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None,
                          title="File Dialogs Tutorial")
        panel = MyPanel(self)
        self.Show()

if __name__ == '__main__':
    app = wx.App(False)
    frame = MyFrame()
    app.MainLoop()