473,503 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Displaying filenames in python tkinter

5 New Member
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
5 8550
bvdet
2,851 Recognized Expert Moderator Specialist
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
Varunkrishna
5 New Member
@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
Varunkrishna
5 New Member
@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
2,851 Recognized Expert Moderator Specialist
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
Varunkrishna
5 New Member
@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

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

Similar topics

11
23511
by: Leo | last post by:
hi there for somebody who wants tostart small/medium GUI apps with python: what's the best toolkit: tkinter, wxPython or what? stability, ease of use and portability between mac and windows...
3
2311
by: Mickel Grönroos | last post by:
Hi everybody, I'm using QuickTimeTcl (3.1) to be able to play movie files in my Tkinter application (Python 2.3.2) on Windows 2000. I was planning to write a simple wrapper class,...
0
2143
by: SteveFerrigno | last post by:
Ok, firstly I’m really sorry if this is in the wrong place :( but anyway. I have created a program which I want to convert into a .exe so I use PY2EXE but even if I have saved the python file I...
1
2212
by: John Chambers | last post by:
Sp my latest adventure is attempting to use python's Tkinter module on a few machines. On my PB (OSX 10.3.9), I got the following confusing results: /Users/jc: python Python 2.3 (#1, Sep 13...
1
1893
by: dabrowsa | last post by:
I'm working on a simple Python program, with Tkinter, and I need to display the math symbols for set union and interesection, in unicode \N{N-ARY UNION} and \N{N-ARY INTERSECTION}. Everything was...
2
9400
by: sj | last post by:
I am just learning to use Tkinter and am having problems displaying image files. I am able to display an image using tutorials (such as http://www.daniweb.com/code/snippet296.html) But when I try...
14
2646
by: Hendrik van Rooyen | last post by:
Hi, I get the following: hvr@LINUXBOXMicrocorp:~/Controller/libpython display.py UpdateStringProc should not be invoked for type font Aborted and I am back at the bash prompt - this is...
1
14567
by: ShambhuHubli | last post by:
Hi ! I am developing front end for some application using Python Tkinter. And I am new to this GUI development. In my application, I have to create buttons other than square or rectangle. I want...
2
2901
by: ShambhuHubli | last post by:
Hi all ! I am developing GUI in Python Tkinter for one application. In this I have created one Text box. And I need to insert the text inside the text box directly in to some paticular line...
1
6203
by: alivip | last post by:
How to brows and select a directory of file by using python TKinter like brows to find attachment in email but I can select not only file but whole directory any help
0
7205
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7349
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7008
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3168
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
746
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
399
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.