Search code examples
pythonwidgetpygtk

Return value of path of excel file in PyGtk


I cannot store value returned from the function to .connect button widget in a variable to used it in my main program.

I am using PyGtk 3+ in Python 3.4 I need returned value from these program to load other values and perform calculations.

   button = Gtk.Button("Brwose File")
   button.connect("clicked",self.test2)  
   def test2(self,widget,mylist1,clicked):
        dialog = Gtk.FileChooserDialog("Please choose a file", None,
                                   Gtk.FileChooserAction.OPEN,
                                   (Gtk.STOCK_CANCEL, 
                                    Gtk.ResponseType.CANCEL,
                                    Gtk.STOCK_OPEN, Gtk.ResponseType.OK)) 
       response = dialog.run()
       if response == Gtk.ResponseType.OK:
           print("Open clicked")
           a = dialog.get_filename()  

       wb = xlrd.open_workbook(a)
       sheet = wb.sheet_by_index(0)
       ncols = sheet.ncols
       print(ncols)
       nrows = sheet.nrows
       print(nrows)
       clicked.append(1)
       print(clicked)
       mylist = []
       for i in range(sheet.nrows):
           data = sheet.row_values(i)
           mylist1.append(data)
       return (mylist1)

Solution

  • Thanks! These is better solution. Here is my updated code:

    def on_browse_clicked(self, widget):
        """Creates dialogue box to choose file for loading data when browse button is clicked"""
    
        dialog = Gtk.FileChooserDialog("Please choose a file", None,
                                       Gtk.FileChooserAction.OPEN,
                                       (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
                                        Gtk.STOCK_OPEN, Gtk.ResponseType.OK))
        response = dialog.run()
        if response == Gtk.ResponseType.OK:
            print("Open clicked")
            global file
            file = dialog.get_filename()
            dialog.destroy()
            self.browse_entry.set_text(file)
            wb = xlrd.open_workbook(file)
            sheet = wb.sheet_by_index(0)
            for i in range(1, sheet.nrows):
                data = sheet.row_values(i)
                print(data)
                self.production_data_list_store.append(data)
    
        else:
            dialog.destroy()