Sep-04-2020, 07:55 PM
(This post was last modified: Sep-04-2020, 07:55 PM by LearningLittlebyLittle.)
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() |