pythonuser-interfacewxglade

Integrating my code with a wxglade GUI


I am very new to python. I took a bioinformatics class this semester and for my final project I want to create an app that we can use in my lab in the future. I have created and tested the actual program in PyCharm, but I want to integrate it with a GUI and create an executable so that it can be used by anyone without having to open an IDE.

So far I have a simple frame with a button that gets the pathway for the input file. I want to take this pathway and insert it into my already existing code. From the wxGlade code, nested under class MyFrame(wx.Frame): :

def OnOpenFileDialog(self, event):  # wxGlade: MyFrame.<event_handler>
    dlg = wx.FileDialog(self, message="Choose a file")
    if dlg.ShowModal() ==wx.ID_OK:
        f = dlg.GetPath()
        filename_input = re.sub("\\\\", "/", f)
    dlg.Destroy() 

I want to take filename_input and put it here:

columns = defaultdict(list) # each value in each column is appended to a list
with open(filename_input) as file_object: # open the file
reader = csv.DictReader(file_object, delimiter='\t') # assign the file reader
for row in reader: # read a row as {column1: value1, column2: value2,...}
    for (k,v) in row.items(): # for each column (position) and value (residue)
        if v != '': # if the value isn't blank
            columns[k].append(v)  # append the residue to a list for that position

But it seems like anything defined within the OnOpenFileDialog block only exists with that block. So how should I go about integrating my code into the GUI? I feel like pasting it all under def OnOpenFileDialog would be very ugly and bad. I really don't know where to start. Sorry if this question is confusing, I will try to clarify if need be. Thanks


Solution

  • Define a method read_file(filename) and call it from within OnOpenFileDialog