我正在编写一些代码,用户需要能够选择程序将在其上运行的文件。我创建了一个浏览按钮,允许用户选择一个文件,但是当你点击“好的”时,程序的其余部分并没有意识到有输入。选择文件后,还应在浏览栏中自动输入文件名。有什么建议么?
from Tkinter import *
class Window:
def __init__(self, master):
#Browse Bar
csvfile=Label(root, text="File").grid(row=1, column=0)
bar=Entry(master).grid(row=1, column=1)
#Buttons
y=7
self.cbutton= Button(root, text="OK", command=master.destroy) #closes window
y+=1
self.cbutton.grid(row=10, column=3, sticky = W + E)
self.bbutton= Button(root, text="Browse", command=self.browsecsv)
self.bbutton.grid(row=1, column=3)
#-------------------------------------------------------------------------------------#
def browsecsv(self):
from tkFileDialog import askopenfilename
Tk().withdraw()
filename = askopenfilename()
#-------------------------------------------------------------------------------------#
import csv
with open('filename', 'rb') as csvfile:
logreader = csv.reader(csvfile, delimiter=',', quotechar='|')
rownum=0
for row in logreader:
NumColumns = len(row)
rownum += 1
Matrix = [[0 for x in xrange(NumColumns)] for x in xrange(rownum)]
csvfile.close()
root = Tk()
window=Window(root)
root.mainloop()