2

Hi i have some python scripts. I need to create a GUI with few buttons, to open those files by browsing. I have to run those scripts just by clicking the GUI button, and should write the output into a n excel sheet. I tried with the below code, but with that i can just read the file! please help? Thank you

from Tkinter import *            
from tkFileDialog   
import askopenfilename      

def callback():
    r = open(askopenfilename(),'r')

a = Button(text='click me', command=callback)
a.pack()

mainloop()
1
  • Bind an action to the button Commented Nov 21, 2012 at 5:25

3 Answers 3

2

inside the callback() instead of opening the file, execute the file using execfile("abc.py")

def callback():
    abc = askopenfilename()
    execfile("abc.py")
Sign up to request clarification or add additional context in comments.

Comments

0

This code below is a simple example that reads each line from the selected input file and then writes it to a new excel file called output.xls. It use the excellent xlwt library for creating the excel file. You could also chose to create a csv file using the csv module that is part of the python standard library which is also readable by excel. The advantages of using xlwt is that you can apply formating, merge cells, formulas, and so many other features that are part of the xls file format.

import xlwt
from Tkinter import *            
from tkFileDialog   import askopenfilename      

def callback():
    filename = askopenfilename()
    wb = xlwt.Workbook()
    ws0 = wb.add_sheet('Sheet1')
    with open(filename, 'r') as f:
        for i, line in enumerate(f):
            ws0.write(i, 0, line.strip())
    wb.save('output.xls')

errmsg = 'Error!'
a = Button(text='click me', command=callback)
a.pack()
mainloop()

2 Comments

Marwan Thanks for giving valuable suggession to try xlvt! but my problem actually is with running the script. Ya i can read the file, and even print all the lines. I'm getting error " Line_Read = linecache.getline(File_Name, Loop_Counter) NameError: global name 'linecache' is not defined" while running and linecache is already imported. please help if you have any idea about this error
You might have a problem with your python installation. Your code worked just fine with me with no errors. Try and re-install python, also try your code on another machine as a test.
0
        import Tkinter, tkFileDialog
        root = Tkinter.Tk()
        root.withdraw()
        dirname=tkFileDialog.askdirectory(parent=root,initialdir="/",title='Please select a directory')
        do something with dirname
        print dirname

#This code for file selection:

    from Tkinter import Tk
    from tkFileDialog import askopenfilename

    Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing

    filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
    print(filename)

2 Comments

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
@DonaldDuck Yeah, now go through the code i had edited it.Code for selecting file and Code for selecting directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.