I've got a function in a class in which I am creating several tkinter buttons within, where the number of buttons and the button properties depends on a file (so I can't create a specific number of variables to hold the buttons).
The code I have looks like this (sparing the complexities of the whole code):
import os import tkinter as tk
def ButtonCreator(self): self.HomeworkList = open("Files\HWNameList.txt", "r") x = self.HomeworkList.readline() while not x == "END": x = x[0:-1] HomeworkFileName = str("Files\HW-" + x + ".txt") locals()["self.Button" + x] = tk.Button(master, text = x, command = lambda: self.DisplayHomeworkFile(FileName)) locals()["self.Button" + x].pack() x = self.HomeworkList.readline() self.HomeworkList.close() def DisplayHomeworkFile(self, filename): os.startfile(filename)
The file I am opening looks like this...
HomeworkName1 HomeworkName2 HomeworkName3 END
When the code runs, it displays the buttons with the correct text written on them but when i click on them they only ever display the file who's file name is written last in the HomeworkList file. Not sure what I've done wrong.
If there is another way to achieve what I'm trying I'm open to all suggestions.