簡體   English   中英

PyGtk中excel文件路徑的返回值

[英]Return value of path of excel file in PyGtk

我無法將函數返回的值存儲到變量中的.connect按鈕小部件,以便在我的主程序中使用它。

我在Python 3.4中使用PyGtk 3+我需要從這些程序返回值來加載其他值並執行計算。

   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)

由於事件循環的工作方式,這是不可能的。 但是,由於您正在使用類,因此您只需使用實例變量即可。 簡化:

class MyApp:
    def __init__(self):
        ...
        self.my_var = None
        button = Gtk.Button()
        button.connect("clicked", self.on_button_clicked)

    def on_button_clicked(self, widget):
        ...
        self.my_var = "something"

此時,在課堂的其他任何地方使用self.my_var

謝謝! 這是更好的解決方案。 這是我更新的代碼:

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM