How am I able to open the contents of a text file into a GUI with each character in the text being an individual button. Say the text file contains 25 characters that are either "A" or "B" in random order. The text file also have 5 rows with which each row having five characters. All i have is this:

from Tkinter import *
from tkFileDialog import *

class Open(Frame):
	def __init__(self,root):
		Frame.__init__(self,root)
		self.grid()
		self.button()
	
	def button(self):
		self.button_list = []
		for x in range(5):
			self.button_list.append([])
			for y in range(5):
				button = Button (self, text = "")
				button.bind("<Button-1>",self.open_file)
				button.grid(row=x,column=y)
				self.button_list[x].append(button)
        def open_file(self,event):
		file = askopenfile(mode = "rU")
		lines = file.readlines()
		
		for x in range(5):
			for y in range(5):
				if lines.config() == "X'
					
		file.close()

(Driver code is goes here)

Try this

from Tkinter import *
from tkFileDialog import *

class Open(Frame):
    def __init__(self,root):
        Frame.__init__(self,root)
        self.grid()
        self.buttonvar = []
        self.button()
    
    def button(self):        
        button = []
        for x in range(5):
            button.append([])            
            self.buttonvar.append([])           
            for y in range(5):
                self.buttonvar[x].append(StringVar())
                self.buttonvar[x][y].set('')
                button[x].append([])
                button[x][y] = Button (self, textvariable = self.buttonvar[x][y])               
                button[x][y].bind("<Button-1>",self.open_file)
                button[x][y].grid(row=x,column=y)                
    def open_file(self,event):
        file = askopenfile(mode = "rU")
        lines = file.readlines()        
        for x in range(5):
            if len(lines) > x:
                for y in range(5):
                    if len(lines[x]) > y:
                        self.buttonvar[x][y].set(lines[x][y])
        file.close()

if __name__ == '__main__':
    tk = Tk()
    ls = Open(tk)
    tk.mainloop()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.