Python Forum

Full Version: [Tkinter] Text Upload
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have an Ubuntu OS, and I am trying to create a GUI to display all the text files in the directory. It should have buttons for each text files, and each button when clicked on it should display each text file. However, I am trying to get it work, and it only displays one text file however I passed all the text files into the class. Any ideas? Or do I need to create a class for every text file?
from tkinter import *
import os
LARGEFONT = ("Verdana", 35)


class tkinterApp(Tk):

    # __init__ function for class tkinterApp  
    def __init__(self):
        # __init__ function for class Tk
        Tk.__init__(self)

        # creating a container 
        container = Frame(self)
        container.pack(side="top", fill="both", expand=True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        # initializing frames to an empty array 
        self.frames = {}

        os.chdir('/home/josemserrajr/ToDoList')
        for root,dir,file in os.walk('.'):
            self.files = file
        for i in self.files:
            brkpt = i.find('.txt')
            word = i[:brkpt]
            frame = txtPage(container,self,file=i)
            self.frames[word] =  frame
            frame.grid(row=0, column=0, sticky="nsew")
        frame = StartPage(container, self)
        self.frames[StartPage] = frame

        frame.grid(row=0, column=0, sticky="nsew")

            # initializing frame of that object from 
            # startpage, page1, page2 respectively with  
            # for loop




        self.show_frame(StartPage)

        # to display the current frame passed as

    # parameter
    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

    # first window frame startpage


class StartPage(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)

        # label of frame Layout 2 
        label = Label(self, text="Startpage", font=LARGEFONT)

        # putting the grid in its place by using 
        # grid 
        label.grid(row=0, column=4, padx=10, pady=10)
        os.chdir('/home/josemserrajr/ToDoList')
        for root, dir, file in os.walk('.'):
            self.files = file
        for i in self.files:
            brkpt = i.find('.txt')
            word = i[:brkpt]
            Button(self, text=i,
                             command=lambda: controller.show_frame(word)).grid()



    # second window frame page1


class txtPage(Frame):

    def __init__(self, parent, controller,file):
        Frame.__init__(self, parent)
        label = Label(self, text="Page 1", font=LARGEFONT)
        label.grid(row=0, column=4, padx=10, pady=10)
        os.chdir('/home/josemserrajr/ToDoList')
        print(file)
        opfil = open(file,'r')
        # button to show frame 2 with text 
        # layout2
        Label(self,text=opfil.read()).grid()
        button1 = Button(self, text='Start Page',
                             command=lambda: controller.show_frame(StartPage))

        # putting the button in its place  
        # by using grid
        opfil.close()
        button1.grid(row=1, column=1, padx=10, pady=10)
    # Driver Code


app = tkinterApp()
app.mainloop()