Displaying filenames in python tkinter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Varunkrishna
    New Member
    • May 2013
    • 5

    Displaying filenames in python tkinter

    I am currently working on a translator project, in which the input will be a text file, that is created in English and I want the same to be translated to German. So I have used askopenfilename to look for files to be selected after selecting the file the name of the file should be displayed in the label button.How do I display the file name within the label button is that possible?
    Code:
    import Tkinter as tk
    from tkFileDialog import askopenfilename
    import os
    class En2De(tk.Frame):
    	def __init__(self,master=None):
    		tk.Frame.__init__(self,master)
    		self.grid()
    		self.createWidgets()
    	def createWidgets(self):
    		self.quitButton = tk.Button(self, text='Quit',command=self.quit)
    		self.UploadButton = tk.Button(self, text='UPLOAD FILES HERE',command= self.uploadButton)
    		self.Label1=tk.Label(self)
    		self.Label2 = tk.Label(self,text='Please Select a language:')
    		optionlist = ('--Select--','Afrikaans','Albanian','English','French','German','Hindi','Tamil','Telugu')
    		self.var=tk.StringVar()
    		self.var.set(optionlist[0])
    		self.om=tk.OptionMenu(self,self.var,*optionlist)#om=optionmenu
    		#self.ConvertButton=tk.button(self, text'Convert Files',command=self.convertButton)
    		#Registering it to the window
    		self.quitButton.grid(sticky=tk.NE)
    		self.UploadButton.grid()
    		self.Label1.grid()
    		self.Label2.grid()
    		self.om.grid()
    	def uploadButton(self):
    		filename = askopenfilename(filetypes=(("Template Files","*.tplate"),("Portable Document File","*.pdf"),("Text File","*.txt"),("Word Document","*.docx"),("Word 97-2003 Document","*.doc"),("All Files","*.*")))
    
    Translater = En2De()
    Translater.master.title('A Translater for Translating Documents From English to German')
    Translater.mainloop()
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    There are more that one way. Here's one:
    Code:
    self.Label1.config(text="the_file_name")

    Comment

    • Varunkrishna
      New Member
      • May 2013
      • 5

      #3
      @bvdet
      Thank you for your help. It is working,but I want only the filename not the full path. And initially it should display 'No files uploaded' in red colour. If the file is updated it must display a file name in some colour(Green). How can I do it any ideas ?

      Comment

      • Varunkrishna
        New Member
        • May 2013
        • 5

        #4
        @bdvet
        Please refer line no 26.
        Code:
        self.Label1.config(text=filename, fg="blue")
        I tried it. Is it the correct way to do ?

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Option fg or foreground specifies the color of the text. Yes, that would be the correct way.

          To display the file name only:
          Code:
          >>> import os
          >>> path = r"D:\SDS2_7.3B\plugins\SagRod\SagRod.py"
          >>> os.path.basename(path)
          'SagRod.py'
          >>>
          To display an initial message in red:
          Code:
          w = Tkinter.Label(parent, text="No files uploaded", fg="red")

          Comment

          • Varunkrishna
            New Member
            • May 2013
            • 5

            #6
            @bdvet
            I had tried the following to display the filenames within a label
            The first method is to display full path and the filename
            Code:
            self.Label1.config(text=filename, fg="blue")
            The second way is to display only the filename
            Code:
            self.Label1.config(text=os.path.basename(filename), fg="blue")
            both seems to be working.

            Comment

            Working...