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.
Related
Good day folks,
I created a simple GUI that let's the user browse through their files with tkinter's filedialog.
After picking one, python reads the data from the (excel) file, and with that creates a new .txt file.
Now after doing that, I would like python to make that .txt file pop up, so that the user gets to see the result.
How do I do this?
Edit for the bot-moderator- he wanted code examples:
File1.py
from contextlib import nullcontext
import File2 as tp
import tkinter as tk
from tkinter import StringVar, messagebox
from tkinter import filedialog
filename = None
def pickFile():
global filename
filename = filedialog.askopenfilename()
#Creating main window
master = tk.Tk()
masterWidth = 350
masterHeight = 250
master.iconbitmap('C:\...')#directory
master.title('Title')
# get the screen dimension
screen_width = master.winfo_screenwidth()
screen_height = master.winfo_screenheight()
# find the center point
center_x = int(screen_width/2 - masterWidth / 2)
center_y = int(screen_height/2 - masterHeight / 2)-100
# set the position of the window to the center of the screen
master.geometry(f'{masterWidth}x{masterHeight}+{center_x}+{center_y}')
#Creating startbutton
startButton = tk.Button (master, text="Start", height=1, width=3, command=lambda: tp.readFile(filename))
#Creating bladerknop
browseButton = tk.Button (master, text='browse...', command=pickFile)
browseLabel = tk.Label(master, text='Choose a file')
startButton.place(x=175,y=200)
browseButton.place(x=210,y=50)
browseLabel.place (x=110,y=52)
master.mainloop()
File2.py
import pandas as pd
import tkinter as tk
from tkinter import messagebox
#declaring variables for later use
data = None
def missingValues(path):
if path is not None:
data = pd.read_excel(path, header=None)
return data
else:
messagebox.showinfo('No file chosen', 'Choose a file first.')
def readFile(path):
data = missingValues(path)
with open("C:\...\newFile.txt", 'w') as newTxt:
count = 0
for index, row in data.iterrows():
code = data.loc[count,0]
price = toEightBits(data.loc[count,1])
newTxt.write(str(code))
newTxt.write(str(price))
newTxt.write("\n")
count += 1
newTxt.close()
I would like to create a table similar to Excel's style. It should be depending on the size of the array to show.
For this reason the table columns and rows are inside loop cycles.
In the first line I would have a combobox to select the value to filter.
Also the combobox is inside a loop cycle for the different columns number.
When the table is created I am not able to recognize which combobox is chosen.
How can I do?
Example:
import tkinter
from tkinter import ttk #per button, label etc
from tkinter.ttk import *
import numpy as np #for the matrix tools
def newselection(event, output):
print("Value selected:", event, output)
def ShowTableGui(MatrixToShowIn):
MatrixToShow=np.array(MatrixToShowIn)
RowNumber = MatrixToShow.shape[0]
ArrayCombo=[]
windowx=tkinter.Tk()
windowx.title("Table viewer")
buttonExit = ttk.Button(windowx, text="Close table", command=windowx.destroy)
# buttonExit = ttk.Button(windowx, text="Run Filter", command= lambda: Run_filter(MatrixToShow.shape[1]))
buttonExit.grid(column=0, row=0)
for Col in range (int(MatrixToShow.shape[1])):
ValuesInsert=MatrixToShow[:,Col] # values in column
ValuesInsert=[row[Col]for row in MatrixToShowIn]
ValuesInsert=list(set(ValuesInsert)) # values listed only once
ValuesInsert.sort()
ValuesInsert.insert(0,"*") # add * to filter all
comboExample0 = ttk.Combobox(windowx, state='readonly', values=ValuesInsert)
comboExample0.grid(column=Col+2, row=0)
comboExample0.bind("<<ComboboxSelected>>", lambda event:newselection(comboExample0.get(), "output"))
# comboExample0.bind("<<ComboboxSelected>>", lambda event:newselection(event, "output"))
ArrayCombo.append(comboExample0)
Select=comboExample0.get()
print(Select)
for Row in range (RowNumber):
b = Entry(windowx, text="")
b.grid(row=Row+1, column=Col+2)
b.insert(0,str(MatrixToShow[Row][Col]))
windowx.mainloop()
return()
MatrixToShowIn=[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
ShowTableGui(MatrixToShowIn)
Finally I have the time to post the solution found thanks to your help:
from tkinter import *
from functools import partial
from tkinter.ttk import *
class ComboTest:
def __init__(self, MatrixToShow):
self.top = Tk()
self.top.title('Combos Test')
self.top_frame = Frame(self.top, width =400, height=400)
self.button_dic = {}
self.combos_dic = {}
self.var = StringVar()
self.top_frame.grid(row=0, column=1)
Button(self.top_frame, text='Exit',
command=self.top.destroy).grid(row=0,column=0, columnspan=5)
self.combos(MatrixToShow)
self.top.mainloop()
##-------------------------------------------------------------------
def combos(self,MatrixToShow):
b_row=1
Columns=[]
for com_num in range(len(MatrixToShow[0])):
Column=["*"]
for Row in range(len(MatrixToShow)):
Column.append(MatrixToShow[Row][com_num])
Columns.append(Column)
## note that the correct "com_num" is stored
# self.combos_dic[com_num] = "self.cb_combo_%d()" % (com_num)
e = Entry(self.top_frame)
e.insert(0, com_num)
e.insert(0, "Column")
e.grid(row=b_row, column=com_num)
b = Combobox(self.top_frame, state='readonly', values=Column)
b.bind("<<ComboboxSelected>>", partial(self.cb_handler, com_num))
b.current(0)
b.grid(row=b_row+1, column=com_num)
##----------------------------------------------------------------
def cb_handler( self, cb_number, event ):
print ("ComboNumber", cb_number, "SelectedValue", event.widget.get())
##=================================================================
MatrixToShowIn=[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
CT=ComboTest(MatrixToShowIn)
Enjoy Roberto
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?
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
I have a problem using lambda: ... in my program. As I don't want my TkInter GUI to execute the functions automatically, I am using lambda. Problem is though that for Button3 it makes my program crash and I have no idea why. Without lambda it works absolutely fine. Here is my code:
import ogr, osr, sys, os
import psycopg2
import ppygis
from datetime import datetime, date
import time
import re
from Tkinter import *
import tkFileDialog
from tkFileDialog import askopenfilename # Open dialog box
from tkMessageBox import showerror
class trip_calculator:
def __init__(self):
global root
root = Tk()
def open_file_dialog():
returned_values = {}
returned_values['filename'] = askopenfilename()
Label(root, text= returned_values.get('filename')[52:] + ' selected').grid(row=2)
filepath = returned_values.get('filename')
#OPEN GPX DRIVER
driver = ogr.GetDriverByName('GPX')
datasource = driver.Open(filepath)
if datasource is None:
print 'not open'
else:
print 'open'
#GEOMETRY
datasource_layer = datasource.GetLayer(2)
#GRAB TIMESTAMPS, ELEVATION, CADENCE ETC.
datasource_layer2 = datasource.GetLayer(4)
#GRAB GEOMETRY INFORMATION AND TRANSFORM TO UTM
datasource_feature = datasource_layer.GetNextFeature()
geoSR = osr.SpatialReference()
geoSR.ImportFromEPSG(4326)
utmSR = osr.SpatialReference()
utmSR.ImportFromEPSG(32633)
coordTrans = osr.CoordinateTransformation(geoSR, utmSR)
geom = datasource_feature.GetGeometryRef()
geom1 = geom.Simplify(0)
geom.Transform(coordTrans)
geom1.Transform(coordTrans)
Label(root, text= 'geometries transformed successfully').grid(row=2, column=5)
#
# This is where the crash of Python occurs,
# `lambda: calculation(...)` won't start.
# It crashes at `features = iter(datasource_layer2)`
#
self.button3 = Button(root, text='calculate attributes', command=lambda:calculation(self,geom1,datasource_layer2)).grid(row=10, column=10, pady=10, padx=10)
def quit_me():
root.quit()
def calculation(self, geom1, datasource_layer2):
#NET AND GROSS TIME CALCULATION
timestamps_net = []
timestamps_net_helper = []
timestamps_elapsed = []
elevation_helper = []
print datasource_layer2
features = iter(datasource_layer2)
next(features)
for feature in features:
if len(timestamps_net_helper) == 2:
timestamps_net_helper = timestamps_net_helper[-1:]
timestamp = feature.GetField(4)
elevation = feature.GetField(3)
elevation_helper.append(elevation)
timestamp_stripped = timestamp[:-3]
day = timestamp[:-11]
#FOR GROSS CALCULATION
timestamps_elapsed.append(timestamp_stripped)
#FOR NET CALCULATION
timestamps_net_helper.append(timestamp_stripped)
if len(timestamps_net_helper) == 2:
#CALCULATE SECONDS BETWEEN
time_a = datetime.strptime(timestamps_net_helper[0], "%Y/%m/%d %H:%M:%S")
time_b = datetime.strptime(timestamps_net_helper[1], "%Y/%m/%d %H:%M:%S")
time_difference = time.mktime(time_b.timetuple()) - time.mktime(time_a.timetuple())
#IF SECONDS LESS THAN 20 BETWEEN GPS TIMESTAMP THEN ADD UP NET TIME
if time_difference < 20:
timestamps_net.append(time_difference)
seconds = sum(timestamps_net)
hours = seconds/60/60
time_length_net = time.strftime('%H:%M:%S', time.gmtime(seconds))
#CLIMB.....
positive_climb = []
negative_climb = []
for a, b in zip(elevation_helper, elevation_helper[1:]):
if a > 0.0 and b > 0.0:
if b > a:
positive_climb.append(b-a)
elif b == a:
pass
else:
negative_climb.append(a-b)
positive_climb = sum(positive_climb)
negative_climb = sum(negative_climb)
#GROSS (ELAPSED TIME)
start = datetime.strptime(timestamps_elapsed[0], "%Y/%m/%d %H:%M:%S")
end = datetime.strptime(timestamps_elapsed[-1], "%Y/%m/%d %H:%M:%S")
time_length = end - start
#LENGTH
length_km = float(geom1.Length()/1000)
#AVERAGE SPEED
avg_speed = (geom1.Length()/1000)/hours
#CREATE LINESTRING FOR PPYGIS AND OGR LINESTRING
myLine = ogr.Geometry(ogr.wkbLineString)
polyline = []
for z in range(geom1.GetPointCount()):
x = geom1.GetX(z)
y = geom1.GetY(z)
myLine.AddPoint(x, y)
point = ppygis.Point(x, y)
polyline.append(point)
myLine_ppy = ppygis.LineString(polyline)
Label(root, text= time_length).grid(row=10, column=5)
Label(root, text= length_km).grid(row=11, column=5)
Label(root, text= avg_speed).grid(row=12, column=5)
self.button1 = Button(root, text='browse', command= open_file_dialog).grid(row=0,pady=10, padx=25)
self.button2 = Button(root, text='close', command= quit_me).grid(row=3, pady=10, padx=25)
root.mainloop()
trip_calculator()
The error occuring is libc++abi.dylib: pure virtual method called but only using lambda in the command of button3. Any ideas how to fix this?
The problem is likely due to the fact that some of the arguments you have in thelambdaexpression -- namelygeom1anddatasource_layer2-- are variables local to the nestedopen_file_dialog() function and don't exist later when the button is pressed and it has returned.
A simple fix would be to make them attributes of the trip_calculator instance, by adding a self.datasource_layer2 = datasource_layer2andself.geom1 = geom1 statements somewhere before the function returns (or just assigning them toselfand referencing them that way everywhere else).