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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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] Updating tkinter text BliepMonster 5 12,302 Nov-28-2022, 01:42 AM
Last Post: deanhystad
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 10,694 Jun-26-2022, 06:26 PM
Last Post: menator01
  tkinter change the text of the checkbox zazas321 1 5,462 Sep-17-2021, 06:19 AM
Last Post: zazas321
  tkinter text widget word wrap position chrisdb 6 10,731 Mar-18-2021, 03:55 PM
Last Post: chrisdb
  [Tkinter] tkinter.Menu – How to make text-variable? Sir 3 7,315 Mar-10-2021, 04:21 PM
Last Post: Sir
Photo Tkinter TEXT background image _ShevaKadu 5 10,629 Nov-02-2020, 10:34 AM
Last Post: joe_momma
  tkinter | Button color text on Click Maryan 2 4,496 Oct-09-2020, 08:56 PM
Last Post: Maryan
  [Tkinter] Indentation for Tkinter-made Text Editor ShakeyPakey 4 6,803 Jun-08-2020, 03:13 PM
Last Post: menator01
  How to make button text bold in Tkinter? scratchmyhead 2 14,815 May-16-2020, 02:53 AM
Last Post: scratchmyhead
  [Tkinter] changing title text to bold in tkinter Kumarkv 2 10,528 May-09-2020, 10:41 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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