简体   繁体   English

PyGtk中excel文件路径的返回值

[英]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. 我无法将函数返回的值存储到变量中的.connect按钮小部件,以便在我的主程序中使用它。

I am using PyGtk 3+ in Python 3.4 I need returned value from these program to load other values and perform calculations. 我在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)

This is not possible because of the way how an event loop works. 由于事件循环的工作方式,这是不可能的。 But since you're working with a class you can just use an instance variable. 但是,由于您正在使用类,因此您只需使用实例变量即可。 Simplified: 简化:

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"

At that point use self.my_var anywhere else in the class. 此时,在课堂的其他任何地方使用self.my_var

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM