Tkinter Gui for reading in a csv file and generating buttons based on the entries in the first line

I need to write gui in Tkinter, which can select the csv file, read it and generate a sequence of buttons based on the names in the first line of the csv file (later the data in the csv file should be used to run a series of simulations).

So far I have managed to write a Tkinter gui that will read the csv file, but I'm stomping about how I should act:

from Tkinter import * import tkFileDialog import csv class 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 = ",") app = Application() app.master.title("test") app.mainloop() 

Any help is much appreciated!

+4
source share
1 answer

Here is one way to do it. I'm not sure how you get attached to different event handlers, but you should be able to use the lambda method to pass by the button name (provided that it is unique) and handle different actions this way.

 from Tkinter import * import tkFileDialog import csv class 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): pass app = Application() app.master.title("test") app.mainloop() 
+6
source

All Articles