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
Related
I have created a simple application where you can enter a word and press a button, and each time you press the button, an image and 2 labels are packed onto the screen, 1 label containing the words you have typed. I want to make it so that if you close the application and reopen it, the images and labels you have placed by pressing the button are still there. How could this be done?
from tkinter import *
import pickle
import calendar
from tkcalendar import *
import calendar
from tkcalendar import *
from datetime import datetime
from datetime import date
from time import strftime
from datetime import timedelta, datetime, date
from ttkthemes import ThemedTk, THEMES
import pickle
self = Tk()
self.title('Storing data')
self.geometry("850x800")
x = 200
y = 250
c = 460
d = 290
e = 325
f = 355
g = 390
h = 420
i = 460
j = 490
LARGE_FONT= ("Verdana", 24)
SMALL_FONT=("Verdana", 12)
def get_text(file):
with open(file, "r") as MyFile:
return MyFile.read()
def edit_text(file, text, img_dir):
with open(file, "w") as MyFile:
MyFile.write(text + "\n" + "apple.png")
def step(self):
my_progress['value']+= 5
def display():
global x , y , c , d , e , f , g, h, i, j
box_image = PhotoImage(file='apple.png')
panel2 = Label(self, image=box_image, bg='#f7f6f6')
panel2.image = box_image
panel2.place(x=x, y=y)
x=x
y = y+260
#assessment name
n = Label(self, text="", bg="#e0f6fc", font=60)
n.configure(text=assessment_name.get())
n.pack()
#due date
d = Label(self, text="Due:", bg="#e0f6fc", font=SMALL_FONT)
d.pack()
#cal date
c= Label(self, text="", bg="#e0f6fc", font=SMALL_FONT)
c.pack()
button = Button(self, text="place", command=display)
button.pack()
save = Button(self, text="save", command=edit_text)
save.pack()
open_button =Button(self, text="open", command=get_text)
open_button.pack()
edit_text("textfile.txt", "label contents", "apple.png")
assessment_name = Entry(self)
assessment_name.place(relx=0.5, y=220, anchor='center')
global cal2
cal2 = Calendar(self, background="#e0f6fc", disabledbackground="white", bordercolor="light blue", headersbackground="light blue", normalbackground="#e0f6fc", foreground="black", normalforeground='black', headersforeground='white', selectmode="day", year=2021, month=8, day=9)
cal2.place(relx=0.5, y=400, anchor='center')
due_date = Button(self, text="Submit")
due_date.place(relx=0.5, y=510, anchor='center')
self.mainloop()
If you want to save something for later, you can save it to a text file. You can save the text and the directory for the image
First, create a .txt file where you want ( within the same folder as the code is best ).
Next, create a function which gets the text from the file:
def get_text(file):
with open(file, "r") as MyFile:
return MyFile.read()
This code will return a string containing what is in the text file.
Next, create a function which can edit the file. It is best to do this by deleting it and re-writing it again
def edit_text(file, text, img_dir):
with open(file "w") as MyFile:
MyFile.write(text + "\n" + img_dir)
When calling this function, it will change the contents of the file to what is inputted. For example:
edit_text("textfile.txt", "label contents", "apple.png")
This would change the contents of "textfile.txt" to:
label contents
apple.png
You can use this to store data for your application. I hope this helped!
While you could save data into a txt, it is much easier imo to store it as JSON. With JSON, you could store a dictionary of values and recall them quickly and easily. You could then take these values and set them to the various parameters within your app.
Python also has a built in JSON library, so all you have to do is import json. You can find the documentation here
Here is a more detailed explanation:
To accomplish this you will need to store the saved data in an external file. This will require knowledge of the .open() , .read() , json.loads() , and json.dumps() functions
We will first start by creating the json file we want to store out data in, ex: myJson.json.
In this file, create a dictionary with the value you want to store, ex: {"entryVal": "Hello World!"}
The following is a basic GUI that we will build on:
from tkinter import *
import json
root = Tk()
toSave = StringVar()
Entry(root, textvariable = toSave) .pack()
Button(root, text = "save value!") .pack()
root.mainloop()
Next, we need to define two functions: one to open the Json, and one to save the Json.
def openJson():
with open('myJson.json', 'r') as f:
value = json.loads(f.read())["entryVal"]
toSave.set(value)
def saveJson():
with open('myJson.json', 'w') as f:
currentVal = json.dumps({"entryVal":toSave.get()})
f.write(currentVal)
openJson() will open the Json file, decode from Json, take the key-value of 'entryVal' and set our entry box to have the text it found.
saveJson() will take the value from our entry, put it in a dictionary under the key 'entryVal', encode to Json, and then write it into the save file.
Now we just need to call openJson() when we start the program, and create a button to trigger saveJson():
from tkinter import *
import json
root = Tk()
def openJson():
with open('myJson.json', 'r') as f:
value = json.loads(f.read())["entryVal"]
toSave.set(value)
def saveJson():
with open('myJson.json', 'w') as f:
currentVal = json.dumps({"entryVal":toSave.get()})
f.write(currentVal)
toSave = StringVar()
Entry(root, textvariable = toSave) .pack()
Button(root, text = "save value!", command = saveJson) .pack()
openJson()
root.mainloop()
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()
I have created a mini app that is used for combining a bunch of excel files, but I cant execute the exe due to this error. What I find strange is that I am not even using Numpy in my script. I have installed and reinstalled numpy, but that did not fix the issue.
I created a virtual environment which has the following libraries installed.
Error on Executing exe
Here is my code:
import tkinter as tk
from tkinter.simpledialog import askstring, askinteger
from tkinter.messagebox import showerror
from tkinter import messagebox
from tkinter import filedialog
from tkinter import ttk
import os
from os import path
import pandas as pd
import fnmatch
import glob
import datetime
from datetime import datetime
import time
import calendar
class Splash(tk.Toplevel):
def __init__(self, parent, width=0.8, height=0.6, useFactor=True):
tk.Toplevel.__init__(self, parent)
self.title("Splash")
w = 300
h = 200
x = 50
y = 100
self.geometry('%dx%d+%d+%d' % (w, h, x, y))
lbl = tk.Label(self,text="Combine Excel\n Version 1.0", bg = 'lightgrey' )
lbl.place(x=25, y=30)
self.master.overrideredirect(True)
self.lift()
## required to make window show before the program gets to the mainloop
self.update()
class App(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.withdraw()
splash = Splash(self)
## simulate a delay while loading
time.sleep(1)
## finished loading so destroy splash
splash.destroy()
def getfiles():
listbox.delete(0,tk.END)
beginput = entry_1.get()
endinput = entry_2.get()
timefmt = "%m/%d/%y"
start = calendar.timegm(datetime.strptime(beginput, timefmt).timetuple())
end = calendar.timegm(datetime.strptime(endinput, timefmt).timetuple())
year = datetime.strptime(beginput, timefmt).strftime('%Y')
monthyr = datetime.strptime(beginput, timefmt).strftime('%m-%y') + ' Daily Collection'
yearmm = 'CA_LoanLvl_' + time.strftime("%Y%m%d")
yrnmsum = 'CA_Sum_' + time.strftime("%Y%m%d")
frame = pd.DataFrame()
list_ = []
cols = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,37,39,40,41,43,44,45,46,47,49,50,51,52,53,54,55,56,57]
path1 = path.join(r'\\corp.int\cms\DeptData\Monthly Reporting'
'\Daily Collection',year,monthyr,'Data')
pathexpt = path.join(r'\\corp.int\cms\DeptData\XX\DS\DataDownloads\Combine',yearmm)
pathexptsum = path.join(r'\\corp.int\cms\DeptData\XX\DS\DataDownloads\Combine',yrnmsum)
mypath = path1
def test(f):
if (not os.path.isfile(f)):
return 0
(mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(f)
return start<=ctime and end>=ctime
files = [f for f in glob.glob(os.path.join(mypath, "adv*")) if test(f)]
for item in files:
listbox.insert(tk.END, os.path.basename(item))
if __name__ == "__main__":
App()
# Create the main window
root = tk.Tk()
s = ttk.Style(root)
s.theme_use('clam')
root.title("Combine GNMA Files")
# set the root window's height, width and x,y position
# x and y are the coordinates of the upper left corner
w = 600
h = 400
x = 50
y = 100
# use width x height + x_offset + y_offset (no spaces!)
root.geometry("%dx%d+%d+%d" % (w, h, x, y))
# use a colorful frame
frame = tk.Frame(root, bg='darkblue')
frame.pack(fill='both', expand='yes')
label = tk.Label(frame, text="Start Date-mm/dd/yy", bg = 'lightblue')
label.place(x=20, y=30)
label2 = tk.Label(frame, text="End Date-mm/dd/yy", bg = 'lightblue')
label2.place(x=20, y=100)
entry_1 = tk.Entry(root)
entry_1.place(x=20,y=60)
entry_2 = tk.Entry(root)
entry_2.place(x=20,y=130)
btn_1 = tk.Button(root,text="Get Files", bg='light grey', command = getfiles)
btn_1.place(x=400, y=15)
listbox = tk.Listbox(root)
listbox.place(x=20, y=160, width = 400)
def on_closing():
if messagebox.askokcancel("Quit", "Do you want to quit?"):
root.destroy()
root.protocol("WM_DELETE_WINDOW", on_closing)
root.mainloop()
If anyone runs across this, it seems it has to do with the version of Numpy that is causing errors with Pyinstaller. Take a look at the link below.
[How to fix 'Missing required dependencies ['numpy']' when running packaged app made with PyInstaller?
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.
I have created a GUI in which I read a CSV file and calculate the liquid output from the data. Now I want to do two things:
1) I want to generate the output based on time just like date
2) I want to generate graphs on a separate window in my GUI for a user specific time or date
This is my code:
import csv
from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter.messagebox import showwarning, showinfo
import datetime
#csv_file = csv.reader(open("C:\Users\Lala Rushan\Downloads\ARIF Drop Monitoring Final\ARIF Drop Monitoring Final\DataLog.csv"))
from Tools.scripts.treesync import raw_input
class App(Frame):
def __init__(self, master):
Frame.__init__(self, master)
button1 = Button(self, text="Browse for a file", command=self.askfilename)
button2 = Button(self, text="Count the file", command=self.takedate)
button3 = Button(self, text="Exit", command=master.destroy)
button1.grid()
button2.grid()
button3.grid()
self.userInputFromRaw = Entry(self)
self.userInputFromRaw.grid()
self.userInputToRaw = Entry(self)
self.userInputToRaw.grid()
self.grid()
def askfilename(self):
in_file = askopenfilename()
if not in_file.endswith(('.CSV')):
showwarning('Are you trying to annoy me?', 'How about giving me a CSV file, genius?')
else:
self.in_file=in_file
def CsvImport(self,csv_file):
dist = 0
for row in csv_file:
_dist = row[0]
try:
_dist = float(_dist)
except ValueError:
_dist = 0
dist += _dist
print ("Urine Volume is: %.2f" % (_dist*0.05))
def takedate(self):
from_raw = self.userInputFromRaw.get()
from_date = datetime.date(*map(int, from_raw.split('/')))
print ('From date: = ' + str(from_date))
to_raw = self.userInputToRaw.get()
to_date = datetime.date(*map(int, to_raw.split('/')))
in_file = ("H:\DataLog.csv")
in_file= csv.reader(open(in_file,"r"))
for line in in_file:
_dist = line[0]
try:
file_date = datetime.date(*map(int, line[1].split(' ')[1].split('/')))
if from_date <= file_date <= to_date:
self.CsvImport(in_file)
except IndexError:
pass
root = Tk()
root.title("Urine Measurement")
root.geometry("500x500")
app = App(root)
root.mainloop()
How can I achieve the above 2 tasks?
I must agree with Jacques de Hooge, you should be using matplotlib for that.
At the beggining of your file, import it:
import matplotlib.pyplot as plt
As you only want to open a new window with the plot, a matplotlib window should suffice. You can use a scatter plot:
plt.scatter(X, Y)
Where X is an iteratable with the x coordinates and Y an iteratable with the y coordinates. Since you want to do something with a constant change in time, you can, having a values list with the values to plot, do the following:
plt.scatter(range(len(values)), values)
plt.show()
You might also want to run this inside a thread, so that the rest of the program doesn't "freeze" while the matplotlib window is open. There are plenty of places where this is explained.