I am currently working on a translator which translates one document from English to German, I had created it as a GUI application using Tkinter and python 2.7, Here I am facing some problems like
- Assume that if a person uploads a document 'samp.txt' in the using the upload button. I want to print the name of the file along with the extension i.e here it must display 'samp.txt' if no file is uploaded it should display that 'no files were uploaded'
- Next is that I want to validate the widgets, the scenario is like this
* If the user fails to upload and he hits the convert button, it should display a message that please upload the file and try again.
* Next validation is for validation of option menu, it should check which option did the user select.
* I want to write the converted file to a particular directory say for example my program is in E:\py. Then the translated document should be written in E:\py\Translated\
How do I do this. .If I try to run the program I am getting the following error message:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
return self.func(*args)
File "samp.py", line 36, in UploadButton
if os.path.isfile(fname):
File "C:\python27\lib\genericpath.py", line 29, in isfile
st = os.stat(path)
TypeError: coercing to Unicode: need string or buffer, NoneType found.
Here is my code
import Tkinter as tk
import ttk
from tkFileDialog import askopenfile
from tkMessageBox import askquestion
import os
class App (tk.Frame):
def __init__(self,master=None):
tk.Frame.__init__(self,master)
self.grid(sticky=tk.N+tk.S+tk.E+tk.W)
self.createWidgets()
def createWidgets(self):
top=self.winfo_toplevel()
top.rowconfigure(0, weight=1)
top.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
self.columnconfigure(0, weight=1)
self.quitButton = tk.Button(self, text = 'Quit', command= self.quit)
self.UploadButton = tk.Button(self, text='Upload Files Here!!!', command =self.UploadButton)
self.progbar = ttk.Progressbar (self,mode='determinate')
#self.progbar.start(50)
lbl = tk.Label (self, text='Please selcet the desired language:')
optionList = ('--Select--','English','Tamil','Hindi','French','German','Spanish','Russian','Telegu','Punjabi','Italian')
self.v = tk.StringVar()
self.v.set(optionList[0])
self.om= tk.OptionMenu(self,self.v,*optionList)
self.ConvertButton = tk.Button(self, text = "Convert Files!", command = self.ConvertFiles)
#register the widget to the window
self.quitButton.grid(sticky=tk.NE, row=0, column=0)
self.UploadButton.grid()
lbl.grid()
self.om.grid(sticky=tk.E)
self.ConvertButton.grid(sticky=tk.NE)
self.progbar.grid(sticky= tk.NE)
def UploadButton(self):
fname = askopenfile (filetypes= (("Template Files","*.tplate"), ("Portable Document Files","*.pdf"),("Word Document","*.docx"),("Word 97-2003 Document","*.doc"),("All files","*.*")))
if os.path.isfile(fname):
for name in open(fname,'r'):
print name,
else:
print 'No file uploaded'
def ConvertFiles(self):
ques= askquestion("Proceed","Do you want to convert the files?")
app1 = App()
app1.master.title('a sample tkinter program in python')
app1.mainloop()