How to use a file that was selected through file dialog? - python

My problem is how to use the file I will select with askopenfilename() later on, for example to put it the canvas ?
What should I put instead of the "?" at "Im = ?" ?
Thank you !
Sorry I am very much a beginner
import tkinter as tk
from tkinter import *
from tkinter.filedialog import *
root=tk.Tk()
root.geometry('1000x690')
root.title("Baccalauréat ISN 2017")
# # #
def Open_Image():
askopenfilename()
# # #
B13= Button(root, text='Open Image', height=5, width= 25, command = askopenfilename)
B13.grid(row=1, column=5, sticky= W + E)
Im = ?
# # #
Nim = Im.resize((int((Im.width*514)/Im.height), 514)) #maxsize =(821, 514) ---> size of the canvas 821-length; 514 -height
nshow = ImageTk.PhotoImage(Nim)
Can = tk.Canvas(root, background = 'blue')
Can.grid(row = 1, column = 0, rowspan = 6, columnspan = 5, sticky = W + E + N + S)
Cim = Can.create_image(0, 0, anchor = NW, image = nshow) # "0, 0" space between the picture and the borders
# # #
mainloop()

Use this code to store a file as a variable:
path = tkFileDialog.askdirectory()
os.chdir(path)
f = open(file_name, mode)
mode can be:
'r' - if you want to read data from the file.
'w' - if you want to write data to the file.
You can read the data of the file using the command: file.read().
And write data using the command: file.write(data)(The mode should be accordingly).
Read further in here.
Hopefully this will help you,Yahli.

Related

How to give the location of "Input" and "Output" for a python code using User Interface and run the code from UI itself?

I have a python code :
import gdal
import numpy
from skimage.filters import threshold_otsu
ds = gdal.Open('A:\\algo\\f2.tif')
In this code, line 4 gives the location/path of "input file" and line 10 gives the location of "output file".
I want to create a user interface to give this location in the user interface itself and run the code from user interface itself.
I have tried making a user interface using "tkinter" module :
from tkinter import *
from tkinter import filedialog
def input():
file1 = filedialog.askopenfile()
label = Label(text=file1).pack()
def input2():
file2 = filedialog.asksaveasfile(mode="w", defaultextension=".tif")
label = Label(text=file2).pack()
w = Tk()
w.geometry("500x500")
w.title("FLOOD_MAPPER")
h = Label(text = "S1A FLOOD MAPPER", bg = "yellow", fg = "black", height = "3", width = "500")
h.pack()
i1 = Label(text = "Input*")
i1.place(x=10, y=70)
i1b = Button(w, text = "Select File", command =input)
i1b.place(x=250, y=70)
i2 = Label(text = "Intermediate Product*")
i2.place(x=10, y=140)
i2b = Button(w, text = "Save as", command =input2)
i2b.place(x=250, y=140)
button = Button(w, text="Generate Map", bg = "red", fg = "black", height = "2", width="30")
button.place(x=150, y=400)
w.mainloop()
But I didn't understand how to link these two codes.
The moment I click on button "generate map" in the user interface I want the location/path of Input and output given in the user interface box to move to their respective places in the 1st code and then run the same code aumoatically.
Kindly, help me to achieve my requirement.
It can look like this. I removed keep only important elements in tkinter.
I put code in your_code and it can get filenames as paramaters. So this code looks similar as before.
I create function gen_map which get run your_code with filenames which are assigned to global variables input_filename, `output_filename.
I assing gen_map to button Button( command=gen_map) so it will run it when you press button.
Other buttons open dialog to get file names and assign to global variables input_filename, output_filename.
from tkinter import *
from tkinter import filedialog
import gdal
import numpy
from skimage.filters import threshold_otsu
def your_code(input_file, output_file):
#ds = gdal.Open('A:\\algo\\f2.tif')
ds = gdal.Open(input_file)
band = ds.GetRasterBand(1)
arr = band.ReadAsArray()
thresh = threshold_otsu(arr,16)
binary = arr > thresh
driver = gdal.GetDriverByName("GTiff")
#outdata = driver.Create("A:\\algo\\test11.tif", 14823, 9985, 1, gdal.GDT_UInt16)
outdata = driver.Create(output_file, 14823, 9985, 1, gdal.GDT_UInt16)
outdata.SetGeoTransform(ds.GetGeoTransform())
outdata.SetProjection(ds.GetProjection())
outdata.GetRasterBand(1).WriteArray(binary)
outdata.GetRasterBand(1).SetNoDataValue(10000)
outdata.FlushCache() ##saves to disk!!
#outdata = None
#band = None
#ds = None
def get_input_filename():
global input_filename
# `askopenfilename` instead of `askopenfile` to get filename instead of object file
input_filename = filedialog.askopenfilename()
input_label['text'] = input_filename
def get_output_filename():
global output_filename
# `asksaveasfilename` instead of `asksaveasfile` to get filename instead of object file
output_filename = filedialog.asksaveasfilename(defaultextension=".tif")
output_label['text'] = output_filename
def gen_map():
#global input_filename
#global output_filename
print('input:', input_filename)
print('output:', output_filename)
your_code(input_filename, output_filename)
#---------------------------------------------
# global variables with default values at start
input_filename = 'A:\\algo\\f2.tif'
output_filename = "A:\\algo\\test11.tif"
root = Tk()
#input_label = Label(root, text=input_filename)
input_label = Label(root, text="Input*")
input_label.pack()
input_button = Button(root, text="Select File", command=get_input_filename)
input_button.pack()
#output_label = Label(root, text=output_filename)
output_label = Label(root, text="Intermediate Product*")
output_label.pack()
output_button = Button(root, text="Save as", command=get_output_filename)
output_button.pack()
gen_map_button = Button(root, text="Generate Map", command=gen_map)
gen_map_button.pack()
root.mainloop()

How to retrieve file name after choosing in dialog box using Python Tkinter

Please advise on how to retrieve a file's full path into a variable after i pick one using tkinter
The whole idea of my GUI is to:
1. Have few buttions
2. Have address bar with file's full address
Once user clicks the button and picks the file >> file's path is displayed in the address bar as well as stored in a separate variable for future usage later in code
I've done some testing, but when checking for retrieved value - I get None.
def file_picker():
"""Pick enova .xlsx file"""
path = filedialog.askopenfilename(filetypes=(('Excel Documents', '*.xlsx'), ('All Files', '*.*')))
return path
file_button = tkinter.Button(root, text='Users File', width=20, height=3,
bg='white', command=custom_functions.file_picker).place(x=30, y=50)
Apart form that I found another code snippet, but this simply captures line onto the GUI interface, not saving file path in any variable though:
def browsefunc():
filename = filedialog.askopenfilename()
pathlabel.config(text=filename)
print(pathlabel)
browsebutton = tkinter.Button(root, text="Browse", command=browsefunc).pack()
pathlabel = tkinter.Label(root).pack()
Expected result: https://imgur.com/a/NbiOPzG - unfortunatelly I cannot post images yet so uploaded one onto imgur
To capture the full path of a file using Tkinter you can do the following. The output of your full file path will be displayed in the "Entry" field / your address bar as per your requirement in your original post.
Update
import tkinter
from tkinter import ttk, StringVar
from tkinter.filedialog import askopenfilename
class GUI:
def __init__(self, window):
# 'StringVar()' is used to get the instance of input field
self.input_text = StringVar()
self.input_text1 = StringVar()
self.path = ''
self.path1 = ''
window.title("Request Notifier")
window.resizable(0, 0) # this prevents from resizing the window
window.geometry("700x300")
ttk.Button(window, text = "Users File", command = lambda: self.set_path_users_field()).grid(row = 0, ipadx=5, ipady=15) # this is placed in 0 0
ttk.Entry(window, textvariable = self.input_text, width = 70).grid( row = 0, column = 1, ipadx=1, ipady=1) # this is placed in 0 1
ttk.Button(window, text = "Enova File", command = lambda: self.set_path_Enova_field()).grid(row = 1, ipadx=5, ipady=15) # this is placed in 0 0
ttk.Entry(window, textvariable = self.input_text1, width = 70).grid( row = 1, column = 1, ipadx=1, ipady=1) # this is placed in 0 1
ttk.Button(window, text = "Send Notifications").grid(row = 2, ipadx=5, ipady=15) # this is placed in 0 0
def set_path_users_field(self):
self.path = askopenfilename()
self.input_text.set(self.path)
def set_path_Enova_field(self):
self.path1 = askopenfilename()
self.input_text1.set(self.path1)
def get_user_path(self):
""" Function provides the Users full file path."""
return self.path
def get_enova_path1(self):
"""Function provides the Enova full file path."""
return self.path1
if __name__ == '__main__':
window = tkinter.Tk()
gui = GUI(window)
window.mainloop()
# Extracting the full file path for re-use. Two ways to accomplish this task is below.
print(gui.path, '\n', gui.path1)
print(gui.get_user_path(), '\n', gui.get_enova_path1())
Note: I added a comment to point you to where the full file path is stored, in my example it's 'path' & 'path1'.

Problematic Binding of Python Function and TKinter GUI Properly

I am trying to build a GUI that takes the root directory and key phrases as inputs from the user, and output all the lines in that directory that contains the key phrases the user enters. My python function works by itself when I enter in parameters manually, but when I try to bind it to my TKinter layout, and pass in variables by using the get function from the user's input, I keep getting "None" as a result. I am relatively new to Python, thank you so much for your help!
My TKinter Layout
from myFunction import myFunction
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
class Feedback:
def __init__(self, master):
master.title('My Python Function')
master.resizable(False, False)
master.configure(background = '#ffffff')
self.frame_header = ttk.Frame(master)
self.frame_header.pack()
self.style = ttk.Style()
self.style.configure('TFrame', background = '#ffffff')
self.style.configure('TButton', background = '#ffffff')
self.style.configure('TLabel', background = '#ffffff', font = ('Arial', 11))
ttk.Label(self.frame_header, wraplength = 300, text = "Output the desired lines from the directory by entering the following.").grid(row = 0, column = 1)
self.frame_content = ttk.Frame(master)
self.frame_content.pack()
ttk.Label(self.frame_content, text = 'Root Directory:').grid(row = 0, column = 0, pady = 4)
ttk.Label(self.frame_content, text = 'Key Phrases:').grid(row = 2, column = 0)
self.entry_rootdirectory = ttk.Entry(self.frame_content, width = 35, font = ('Arial', 10))
self.text_keyphrases = Text(self.frame_content, width = 35, height = 5, font = ('Arial', 10))
self.entry_rootdirectory.grid(row = 0, column = 1)
self.text_keyphrases.grid(row = 2, column = 1)
ttk.Button(self.frame_content, text = 'Submit', command = self.submit).grid(row = 5, column = 0, pady = 5)
ttk.Button(self.frame_content, text = 'Clear', command = self.clear).grid(row = 5, column = 1, pady = 5)
def submit(self):
#My code that attents to bind to the python script
print(versionFilter(self.entry_rootdirectory.get(), self.text_keyphrases.get(1.0, 'end'))) #trying to get the users input and pass them as parameters for the function, and print out the results afterwards
self.clear()
messagebox.showinfo(title = "Search Requested", message = "Search Submitted!")
def clear(self):
self.entry_rootdirectory.delete(0, 'end')
#self.text_filetypes.delete(1.0, 'end')
self.text_keyphrases.delete(1.0, 'end')
def main():
root = Tk()
feedback = Feedback(root)
root.mainloop()
if __name__ == "__main__": main()
My Python Function
import os
def myFunction(rootdir, keyPhrases):
path = r"rootdir" #The root directory being passed as a parameter, this is where the key phrases are being searched from
key_phrases = ["keyPhrases"] #Passed as a parameter, this is the key phrases users input to find corresponding lines in the directory that contains the key phrases
# This for loop allows all sub directories and files to be searched
for (path, subdirs, files) in os.walk(path):
files = [f for f in os.listdir(path) if f.endswith('.txt') or f.endswith('.log')] # Specify here the format of files you hope to search from (ex: ".txt" or ".log")
files.sort() # file is sorted list
files = [os.path.join(path, name) for name in files] # Joins the path and the name, so the files can be opened and scanned by the open() function
# The following for loop searches all files with the selected format
for filename in files:
# Opens the individual files and to read their lines
with open(filename) as f:
f = f.readlines()
# The following loop scans for the key phrases entered by the user in every line of the files searched, and stores the lines that match into the "important" array
for line in f:
for phrase in key_phrases:
if phrase in line:
return print(line)
break
# Prints the selected lines after searching the desired files of the entire directory

win 10 python 3.5 tkinter entry ValueError: invalid literal for int() with base 10: ''

i have written a program that creates numberd directories inside other directories it works fine except one little issue that is the most important. the entry that is used in the while loop can't convert into a base 10 integer. i have tried several ways and found nothing that would help me..
from tkinter import *
from tkinter import filedialog as fd
import tkinter.messagebox as box
from tkinter import ttk
import os
#window propeties
window = Tk()
frame = Frame(window)
entry = Entry(frame)
window.wm_iconbitmap('icon.ico')
img = PhotoImage('')
directory = "C:\ "
#window geometry
window.title("numbered Directories creater")
window.geometry("600x400")
#label & buttons
explain_lab_1 = Label(window, text = "This is a tool used to create directories with matching numbers")
explain_lab_2 = Label(window, text = "have fun!!!")
explain_lab_3 = Label(window, text = "\bstay safe\b")
path_btn = ttk.Button(window, text = "Change path")
lab_path = Label(window, text = directory)
create_btn = ttk.Button(window, text = "CREATE")
num_entry = ttk.Entry(window)
lab_to_text_nums = Label(window, text = "fill the number of directories you
wish to create")
print(type(num_entry.get()))
num_entry_in = num_entry.get()
#functions
def say_dir():
box.showinfo("Directory creater", "The directory to create is set to C:\
that ia a default")
def change_path():
directory = fd.askdirectory()
lab_path.configure(text = directory)
def flood(i=0):
while i < num_entry_in:
try:
os.mkdir(directory + str(i))
i += 1
except FileExistsError:
i += 1
#configurations
path_btn.configure(command = change_path)
create_btn.configure(command = flood)
explain_lab_1.configure(font=("Courier", 9))
explain_lab_2.configure(font=("Courier", 9))
explain_lab_3.configure(font=("Courier", 9))
lab_to_text_nums.configure(font=("Courier", 9))
#show labs & buttons
explain_lab_1.grid(row = 1, column = 0)
explain_lab_2.grid(row = 2, column = 0)
explain_lab_3.grid(row = 3, column = 0)
path_btn.grid(row = 6, column = 2)
lab_path.grid(row = 7, column = 1 , columnspan = 2)
create_btn.grid(row = 9, column = 2)
lab_to_text_nums.grid(row = 8, column = 0)
num_entry.grid(row = 9, column = 0)
#call functions
say_dir()
#loads window
window.mainloop()
The problem is when you call
num_entry_in = num_entry.get()
Currently you call it right away, as soon as num_entry is created, so of course the field is empty and returns "" (an empty string), and int("") gives ValueError.
You need to put it inside flood() so that it runs when the button is pressed, after the user has entered something. You also need to check the result to make sure it actually is a number, ie
try:
num_entry_in = int(num_entry.get())
except ValueError:
# not a an int!
# pop up a warning to let the user know what went wrong

Python: "IOError: C not found"

I am writing a python script that imports multiple input data files in csv format and plots statistical graphs. However I keep on getting an error which I am unable to figure out.
Any suggestions would be highly appreciated.
Here's the snippet of the relevant part of the code
import numpy as np
import matplotlib
import Tkinter
matplotlib.use('TkAgg')
from matplotlib import pyplot as plt
from matplotlib import gridspec
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from Tkinter import Frame,Button,Canvas, Scrollbar, Tk, Checkbutton, W,E,N,S, VERTICAL, Y, BOTH, FALSE, TRUE, RIGHT, LEFT, Label, StringVar,IntVar
from tkFileDialog import askopenfilename, askopenfilenames
from tkMessageBox import askokcancel, showwarning
import sys
class MyGuiPlot(Frame):
def open_csv(self): # open file + control defaultextension of it
fname = askopenfilenames(defaultextension='.csv',
filetypes=[('CSV file','*.csv')])
if fname:
self.length = len(fname)
self.get_data_multi(fname)
def get_data_multi(self, fname):
self.fname = fname
# button_show_all = Button(self.root, text='Show all', command = lambda d = dataset, vars_all = vars_all,v=vars: self.var_states(v,dataset,vars_all))
# button_show_all.grid(row = len(fname)+1, column=0, sticky = W)
check_frame = Frame(self.root)
check_frame.grid(row=1,columnspan=12,sticky=W)
position = 0
vars_all = []
for data in range(len(fname)):
j=0
x=0
print(data)
vars = []
#position = data*len(fname)
dataset = np.genfromtxt(self.fname[data], dtype = float, delimiter = ',', names = True)
file_name = Label(check_frame, text='DATASET{0} => {1}'.format(data,self.fname[data]))
button_go = Button(check_frame, text= 'GO!', command = lambda dataset = dataset, v=vars: self.var_states(v,dataset))
file_name.grid(row=position,column=0,columnspan=12, sticky=W)
button_go.grid(row=position+3,columnspan=2, sticky=W)
for _ in dataset.dtype.names: # creating checkboxes
var_ = StringVar()
if _.startswith('x'):
ch_btn = Checkbutton(check_frame, text='{}'.format(str(data)+_), variable=var_, onvalue=str(data)+':'+_)
ch_btn.deselect()
ch_btn.grid(row=position+2,column=x, sticky=W)
x+=1
vars.append(var_)
vars_all.append(var_)
else:
ch_btn = Checkbutton(check_frame, text='{}'.format(str(data)+_), variable=var_, onvalue=str(data)+':'+_)
ch_btn.deselect()
ch_btn.grid(row=position+1,column=j, sticky=W)
vars.append(var_)
j+=1
vars_all.append(var_)
if len(fname) ==2:position +=len(fname)+2
else:position +=len(fname)+1
#print(vars_all)
button_show_all = Button(self.root, text='Show all', command = lambda id=0: self.var_states(dataset = dataset,vars_all=vars_all))
button_show_all.grid(row = len(fname)+1, column=0, sticky = W)
This is the error I get:
You have problem in line with dataset = ... so use print() to see what you have in variables which you use in this line print(data, self.fname, self.fname[data])
I think you have path to file in self.fname and you get first char using self.fname[data] and you use this single char as name in np.genfromtxt()
You use Windows so full path starts with C:\ and first char is C
and now you see message C not found

Categories

Resources