473,434 Members | 1,521 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

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?
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 8547
bvdet
2,851 Expert Mod 2GB
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
@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
@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 Expert Mod 2GB
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
@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
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
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
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
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
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
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
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
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
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
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.