954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Opening a file into buttons

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)

effBlam
Newbie Poster
13 posts since Oct 2011
Reputation Points: 10
Solved Threads: 0
 

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()
bawakrbs
Newbie Poster
4 posts since Nov 2011
Reputation Points: 10
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: