Daniel B
Daniel B

Reputation: 322

Python Tkinter: Buttons for every row in a .csv?

Trying my best at learning Tkinter and failing miserably every step of the way.

I'm trying to write a GUI that creates me either a drop-down list, either a button grid, based on row entries in a .csv file that my script imports.

Thing is, both attempts went up in flames, as I am new to this.

Can anyone link me to a resource/ give me the steps for achieving what I want?

Upvotes: 0

Views: 629

Answers (1)

noob
noob

Reputation: 141

Here is the solution. Next time please paste some code you've written so we know what exactly you've tried.

import tkinter as tk

dataMatrix = [] #this will be 2d list containing csv table elements as strings
with open("filename.csv") as file:
    for row in file:
        dataMatrix.append(row[:-1].split(";")) 

mainWindow = tk.Tk()

######## EITHER THIS
optionMenus = []
strVars = []
for i in range(len(dataMatrix)):
    strVars.append(tk.StringVar(mainWindow, dataMatrix[i][0]))
    #variable containing currently selected value in i-th row, initialised to first element
    optionMenus.append(tk.OptionMenu(mainWindow, strVars[-1], *dataMatrix[i])) #drop-down list for i-th row
    optionMenus[-1].grid(row = 0, column = i) #placing i-th OptionMenu in window
######## OR THIS
for r in range(len(dataMatrix)):
    for c in range(len(dataMatrix[r])):
        b = tk.Button(mainWindow, text = dataMatrix[r][c], width = 10)
        #creating button of width 10 characters
        b.grid(row = r, column = c)

OptionMenu Control Variables

Generally infohost.nmt.edu and effbot.org are the best resources for learning tkinter.

Upvotes: 1

Related Questions