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()
inside the callback() instead of opening the file, execute the file using execfile("abc.py")
def callback():
abc = askopenfilename()
execfile("abc.py")
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()
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)
Related
This is for my assignment.
I was given a template like this... def_choosefile() :
import tkinter
from tkinter import filedialog
root_window = tkinter.Tk()
root_window.withdraw()
return filedialog.askopenfilename()
So if i get this correct, it'll prompt a dialog window, asking to select a file. And when a file is selected, the program is supposed to tell what files did it pick. Using these:
selected_file = choose_file()
print ("Selected file: {}".format (selected_file))
after that, how do i make the program read the files normally? Normally i would use:
infile = open('text')
infile.readline()
import tkinter
from tkinter import filedialog
root_window = tkinter.Tk()
root_window.withdraw()
def choose_file():
return filedialog.askopenfilename()
selected_file = choose_file()
with open (selected_file,'r') as readfile:
lines = readfile.read().splitlines()
for line in lines[0:2]:
print (line)
I am trying to open a exe file from C drive by clicking a button from my GUI and up till now i am unable to select the specific file. Can i know if there is any function to directly open the file in Tkinter. Currently,tkFileDialog.askdirectory only direct me to the FILES.
import Tkinter
import tkMessageBox
import tkFileDialog
import os
import subprocess
top = Tkinter.Tk()
def run():
File = tkFileDialog.askdirectory()
os.system(File)
b = Tkinter.Button(top, text = 'DAQoutput', command= run)
b.pack()
top.mainloop()
Directly using Tkinter? No there is no command for it but you don't need one. Tkinter is a GUI toolkit and that's all it does.
Instead use os.system() to launch your exe.
Your script should look something like this:
import Tkinter
import tkMessageBox
import tkFileDialog
import os
top = Tkinter.Tk()
def run(self):
File = tkFileDialog.askdirectory()
os.system(File)
b = Tkinter.Button(top, text = 'DAQoutput', command= self.run)
b.pack()
top.mainloop()
I'm trying to simply get a file name from the user by tkinter.filedialog.askopenfilename(). The function returns fine and the code below displays the file name okay but the dialog window doesn't close immediately after hitting 'open' or 'cancel', it freezes. I'm using python 3.3.3 or OSX 10.9.1 and tcl/tK 8.5.9.
from tkinter import *
from tkinter.messagebox import *
from tkinter.filedialog import *
top = Tk()
top.withdraw()
file_name = filedialog.askopenfilename()
print (file_name)
Adding root.update() after filedialog.askopenfilename() gets the open file dialog to close after the file is chosen.
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename()
root.update()
Refer to: Tkinter askopenfilename() won't close
A work around that I used for this is to "withdraw" the tkinter window AFTER selecting the file from file explorer. Here is the code snippet I used -
import tkinter
from tkinter import filedialog
def selectCustomerFileTK():
root = tkinter.Tk()
root.wm_attributes('-topmost', 1)
filename = filedialog.askopenfilename()
root.withdraw()
return filename
getfile = selectCustomerFileTK()
It opens a tkinter window while you are selecting the file, but the moment you select the file and press "Open", the tkinter window and file explorer both close because of the "root.withdraw()" command after.
You don't need to specify module name in
file_name = filedialog.askopenfilename()
Try
file_name = askopenfilename()
instead
I'm making a program that you use the askopenname file dialog to select a file, which I then want to save the directory to a string so I can use another function (which I already made) to extract the file to a different location that is predetermined.
My button code that opens the file dialog is this:
`a = tkinter.Button(gui, command=lambda: tkinter.filedialog.askopenfilename(initialdir='C:/Users/%s' % user))`
This should be what you want:
import tkinter
import tkinter.filedialog
import getpass
# Need this for the `os.path.split` function
import os
gui = tkinter.Tk()
user = getpass.getuser()
def click():
# Get the file
file = tkinter.filedialog.askopenfilename(initialdir='C:/Users/%s' % user)
# Split the filepath to get the directory
directory = os.path.split(file)[0]
print(directory)
button = tkinter.Button(gui, command=click)
button.grid()
gui.mainloop()
If you know where the file actually is, you could always just ask for a directory instead of the file using:
from tkFileDialog import askdirectory
directory= askdirectory()
Then in the code:
import tkinter
import tkinter.filedialog
import getpass
from tkFileDialog import askdirectory
# Need this for the `os.path.split` function
import os
gui = tkinter.Tk()
user = getpass.getuser()
def click():
directory= askdirectory()
print (directory)
button = tkinter.Button(gui, command=click)
button.grid()
gui.mainloop()
I have a python file which is called from other files
Every time the python file is imported and mainApp is called from others, the tkinter button inside the python file is executed automatically.
here is part of the python file code
from Tktable import *
def exp(Output):
import csv
from tkFileDialog import askdirectory
folder=askdirectory();
if folder:
path = folder+'/outputTable.csv';
file = open(path, 'w');
writer = csv.writer(file)
title = ['Premise','Conclusion','Support','Confidence','Lift']
writer.writerow(title);
zip(*Output)
for item in zip(*Output):
writer.writerow(item)
file.close()
def mainApp(Output):
from Tkinter import Tk, Label, Button, Scrollbar, Frame
root = Tk()
top = Frame(root).pack(side = 'top', fill = 'x')
...
export = Button(top, text='EXPORT', command=exp(Output))
export.grid(row=0, column=4, sticky = 'e')
...
How could I stop the auto execution of the button? And why is this happening? Can anyone help me? Thank you!
It happens because you're calling the function. Pass it a function object instead, such as one created with lambda.
..., command=(lambda: exp(Output)))