Tkinter Gui to read in csv file and generate buttons based on the entries in the first row Tkinter Gui to read in csv file and generate buttons based on the entries in the first row tkinter tkinter

Tkinter Gui to read in csv file and generate buttons based on the entries in the first row


Here's one way to do it. I'm not sure how you'd bind to different event handlers, but you should be able to use a lambda method to pass along the button name (assuming it's unique) and handle different actions that way.

from Tkinter import *import tkFileDialogimport csvclass Application(Frame):    def __init__(self, master = None):        Frame.__init__(self,master)        self.grid()        self.createWidgets()    def createWidgets(self):        top = self.winfo_toplevel()        self.menuBar = Menu(top)        top["menu"] = self.menuBar        self.subMenu = Menu(self.menuBar)        self.menuBar.add_cascade(label = "File", menu = self.subMenu)        self.subMenu.add_command( label = "Read Data",command = self.readCSV)    def readCSV(self):        self.filename = tkFileDialog.askopenfilename()        f = open(self.filename,"rb")        read = csv.reader(f, delimiter = ",")        buttons = read.next()        print        for btn in buttons:            new_btn = Button(self, text=btn, command=self.btnClick)            new_btn.pack()    def btnClick(self):        passapp = Application()app.master.title("test")app.mainloop()