'Using returned variable from 1 function in another
I have spent many hours all over the internet and this site trying to determine what is going on. All I'm trying to do is, using tkinter, have 1 button (Browse Files) that runs the first function (get_file_name
) and opens a window to browse for a files location (.csv to be exact). It then displays that file location on the open window. I'm trying to retrieve that displayed location and pass that to the 2nd function (Analyze
), so that it can retrieve and analyze the .csv file with a press of the second button (Analyze
)
So far, the window opens as expected, there are 3 buttons that all read correctly. I can open a file explorer window and select the file I'm looking for, and it displays that file path in the window. I've also verified that my 2nd function works correctly to analyze and output a new .csv file.
What I can't figure out is how to get the file name/location from the first function into the second function. As you can see I did create a class at one point, but was unsuccessful in getting it to work so it currently just reads pass under def __init__
.
Any help would be much appreciated, I'm sure that there is a simple solution that I'm just missing here.
# import all components
# from the tkinter library
from ast import Return
from pyclbr import Class
from tkinter import *
# import filedialog module
from tkinter import filedialog
import os
# Function for opening the
# file explorer window
# Create the root window
window = Tk()
# Set window title
window.title('Log Peruser 9000')
# Set window size
window.geometry("500x300")
#Set window background color
window.config(background = "white")
# Create a File Explorer label
label_file_explorer = Label(window,
text = "Select the Desired Log File",
width = 70, height = 4,
fg = "blue")
#Class Creation
class Log:
def __init__(self, file):
pass
#Get File Location
def get_file_name():
filename = filedialog.askopenfilename(initialdir = "/",
title = "Select a File",
filetypes = (("CSV", "*.csv*"),
("all files",
"*.*")))
# Change label contents
label_file_explorer.configure(text="Log File Selected: "+filename)
file = filename
return file
def Analyze():
#Import CSV
import csv
TCLoggerFile = open(file)
TCLoggerReader = csv.reader(TCLoggerFile)
#Create Dictionary
fields = [' Row #', ' Priority', ' Message Text']
data = []
# extracting each data row one by one
for rows in TCLoggerReader:
for row in rows:
position_error = 'position error","FSS_Control/controller_fss_master.c:'
if position_error in row:
data.append('Row #' + str(TCLoggerReader.line_num) + ', ' + 'Priority 3' + ', ' + row)
#data.append(row)
tfs = 'TFS passed -> Set ROTC speed NORMAL'
if tfs in row:
data.append('Row #' + str(TCLoggerReader.line_num) + ', ' + 'Priority 1' + ', ' + row)
#Export CSV File
outputFile = open('Analyzed_TCLogger.csv', 'w', newline='')
outputWriter = csv.writer(outputFile)
outputWriter.writerow(fields)
for row in data:
outputWriter.writerow([row])
outputFile.close()
button_explore = Button(window,
text = "Browse Files",
command = get_file_name)
button_analyze = Button(window,
text = "Analyze",
command = Analyze)
button_exit = Button(window,
text = "Exit",
command = exit)
# Grid method is chosen for placing
# the widgets at respective positions
# in a table like structure by
# specifying rows and columns
label_file_explorer.place(x=0, y=0)
button_explore.place(x=150, y=100)
button_analyze.place(x=240, y=100)
button_exit.place(x=305, y=100)
# Let the window wait for any events
window.mainloop()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|