Python Forum
[Tkinter] Text Upload
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Text Upload
#1
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() 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter change the text of the checkbox zazas321 1 385 Sep-17-2021, 06:19 AM
Last Post: zazas321
  tkinter text widget word wrap position chrisdb 6 2,647 Mar-18-2021, 03:55 PM
Last Post: chrisdb
  [Tkinter] tkinter.Menu – How to make text-variable? Sir 3 2,187 Mar-10-2021, 04:21 PM
Last Post: Sir
Photo Tkinter TEXT background image _ShevaKadu 5 3,769 Nov-02-2020, 10:34 AM
Last Post: joe_momma
  tkinter | Button color text on Click Maryan 2 1,401 Oct-09-2020, 08:56 PM
Last Post: Maryan
  [Tkinter] Indentation for Tkinter-made Text Editor ShakeyPakey 4 2,275 Jun-08-2020, 03:13 PM
Last Post: menator01
  How to make button text bold in Tkinter? scratchmyhead 2 3,475 May-16-2020, 02:53 AM
Last Post: scratchmyhead
  [Tkinter] changing title text to bold in tkinter Kumarkv 2 4,163 May-09-2020, 10:41 PM
Last Post: Larz60+
  [Tkinter] Help setting text/title in dock/panel in tkinter gui FluxApex 2 2,396 Mar-18-2020, 07:15 PM
Last Post: FluxApex
  [Tkinter] Tkinter Winget python text Marc 5 1,302 Mar-13-2020, 11:17 PM
Last Post: Marc

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020