0

我有一个读取和写入 csv 的代码块。阅读器将文件“x”与文件“y”进行比较并返回新文件“z”

现在,我使用 tkinter 编写了一个 GUI 程序,该程序将文件路径返回到 GUI 程序中的文本框。

我得到的文件路径如下:

def OnButtonClick1(self):
    self.labelVariable.set( self.entryVariable.get())
    self.entry.focus_set()
    self.entry.selection_range(0, tkinter.END)
    filename = askopenfilename()
    with open(filename,'r') as f:
        for file in f:
            data = f.read()
            self.entry.insert(0,filename)

如何在阅读器中使用上面的文件路径来表示下面代码中的“myfile”?

    #Opening my enquiry list .cvs file
    datafile = open('myfile', 'r')
    datareader = csv.reader(datafile)
    n1 = []
    for row in datareader:
        n1.append(row)

        n = list(itertools.chain(*n1))
    print()

帮助很多appriciated!!!

4

1 回答 1

0

也许是这样的

class gui:
...
    def OnButtonClick1(self):
        self.labelVariable.set( self.entryVariable.get())
        self.entry.focus_set()
        self.entry.selection_range(0, tkinter.END)
        filename = askopenfilename()
        self.filename = filename
        with open(filename,'r') as f:
        for file in f:
            data = f.read()
            self.entry.insert(0,filename)
    def GetFilename(self):
        return self.filename
...
gui_object = gui()
...
#Opening my enquiry list .cvs file
myfile = gui_object.GetFilename()
datafile = open(myfile, 'r')
datareader = csv.reader(datafile)
n1 = []
for row in datareader:
n1.append(row)

n = list(itertools.chain(*n1))
于 2013-06-25T07:04:58.390 回答