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

  1. 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'
  2. 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):
    def createWidgets(self):
        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')
        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.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.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,
            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')

Recommended Answers

All 3 Replies

askopenfile() is supposed to return a file object. See askopenfilename() and check the value it returns.

One more doubt please refer the line numbers 38 and 40 I had used print() to display the file name, but I did not tell where to print the name. So how do I instruct the compiler to display the filename.ext just below the upload button ? Is it possible ?

So how do I instruct the compiler to display the filename.ext just below the upload button ? Is it possible ?

Perhaps create a new tk.Label and update its content with its .set() method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.