By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,760 Members | 1,100 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,760 IT Pros & Developers. It's quick & easy.

Displaying filenames in python tkinter

P: 5
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?
Expand|Select|Wrap|Line Numbers
  1. import Tkinter as tk
  2. from tkFileDialog import askopenfilename
  3. import os
  4. class En2De(tk.Frame):
  5.     def __init__(self,master=None):
  6.         tk.Frame.__init__(self,master)
  7.         self.grid()
  8.         self.createWidgets()
  9.     def createWidgets(self):
  10.         self.quitButton = tk.Button(self, text='Quit',command=self.quit)
  11.         self.UploadButton = tk.Button(self, text='UPLOAD FILES HERE',command= self.uploadButton)
  12.         self.Label1=tk.Label(self)
  13.         self.Label2 = tk.Label(self,text='Please Select a language:')
  14.         optionlist = ('--Select--','Afrikaans','Albanian','English','French','German','Hindi','Tamil','Telugu')
  15.         self.var=tk.StringVar()
  16.         self.var.set(optionlist[0])
  17.         self.om=tk.OptionMenu(self,self.var,*optionlist)#om=optionmenu
  18.         #self.ConvertButton=tk.button(self, text'Convert Files',command=self.convertButton)
  19.         #Registering it to the window
  20.         self.quitButton.grid(sticky=tk.NE)
  21.         self.UploadButton.grid()
  22.         self.Label1.grid()
  23.         self.Label2.grid()
  24.         self.om.grid()
  25.     def uploadButton(self):
  26.         filename = askopenfilename(filetypes=(("Template Files","*.tplate"),("Portable Document File","*.pdf"),("Text File","*.txt"),("Word Document","*.docx"),("Word 97-2003 Document","*.doc"),("All Files","*.*")))
  27.  
  28. Translater = En2De()
  29. Translater.master.title('A Translater for Translating Documents From English to German')
  30. Translater.mainloop()
  31.  
May 6 '13 #1
Share this Question
Share on Google+
5 Replies

bvdet
Expert Mod 2.5K+
P: 2,851
There are more that one way. Here's one:
Expand|Select|Wrap|Line Numbers
  1. self.Label1.config(text="the_file_name")
May 6 '13 #2

P: 5
@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 ?
May 6 '13 #3

P: 5
@bdvet
Please refer line no 26.
Expand|Select|Wrap|Line Numbers
  1. self.Label1.config(text=filename, fg="blue")
I tried it. Is it the correct way to do ?
May 6 '13 #4

bvdet
Expert Mod 2.5K+
P: 2,851
Option fg or foreground specifies the color of the text. Yes, that would be the correct way.

To display the file name only:
Expand|Select|Wrap|Line Numbers
  1. >>> import os
  2. >>> path = r"D:\SDS2_7.3B\plugins\SagRod\SagRod.py"
  3. >>> os.path.basename(path)
  4. 'SagRod.py'
  5. >>> 
To display an initial message in red:
Expand|Select|Wrap|Line Numbers
  1. w = Tkinter.Label(parent, text="No files uploaded", fg="red")
May 6 '13 #5

P: 5
@bdvet
I had tried the following to display the filenames within a label
The first method is to display full path and the filename
Expand|Select|Wrap|Line Numbers
  1. self.Label1.config(text=filename, fg="blue")
The second way is to display only the filename
Expand|Select|Wrap|Line Numbers
  1. self.Label1.config(text=os.path.basename(filename), fg="blue")
both seems to be working.
May 7 '13 #6

Post your reply

Sign in to post your reply or Sign up for a free account.