0

I am building a process in tkinter which allows the user to select two CSV Files - the files will then be fed into a function and the function's output will consist of a dataframe ( The output is a result of Merge , GroupBy and Count operations on the selected files). My GUI should consist of 3 Buttons ( 2 buttons for selecting files from the Desktop) and 1 Run button that would print the resulting dataframe as a table in tkinter. I have been able to create the buttons; however I am unable to print the dataframe once the Run button is selected.

The buttons:

root = tk.Tk()
ttk.Button(root, text="Load CCE file", command=open_CCE_file).place(x=50,y=60,width=120)
ttk.Button(root, text="Load SMS output file", command=open_SMS_file).place(x=50,y=20,width=120)
ttk.Button(root, text='Run', command= process_files).place(x=50,y=100,width=120)

The functions:

def open_SMS_file():
    file = filedialog.askopenfile(mode='r', filetypes=[('All Files', '*.*')])
    global path_SMS
    if file:
        filepath = os.path.abspath(file.name)
        Label(root, text=str(filepath), font=('Aerial 11')).place(x=180,y=20)
        path_SMS=str(filepath)
# Function determining the path for the CCE file        
def open_CCE_file():
    file = filedialog.askopenfile(mode='r', filetypes=[('All Files', '*.*')])
    global path_CCE
    if file:
        filepath = os.path.abspath(file.name)
        Label(root, text=str(filepath), font=('Aerial 11')).place(x=180,y=60)
        path_CCE=str(filepath)
def process_files():
    
    
    CCE_file = pd.read_csv(path_CCE, encoding='latin1') # read CCE file into dataframe 
    SMS_file = pd.read_csv(path_SMS) # read SMS file into dataframe 
    
    # filter cell code column from SMS file - new SMS dataframe that consists of the CellCode column
    SMS_file_selected = SMS_file[['CellCode']]
    
    # Select Source,content and rules columns from the CCE file
    # Rules column contains the cell codes corresponding to each segement
    CCE_file_selected = CCE_file[['Name','Source','Content EN','Content FR','Rule(s)']]
    CCE_file_selected.rename(columns={'Rule(s)':'Rules'}, inplace=True)
    
    # loop that retreives the cell codes for each segment and puts them in a list
    # each row in the Rules column will consist of a list of cell codes corresponding to a given segement 
    for i in range(len(CCE_file_selected)):
        CCE_file_selected['Rules'][i] = get_access_codes(CCE_file_selected['Rules'][i])
        
    
    # series that outputs the count per cellcode in the SMS file
    count_series = SMS_file_selected.groupby(['CellCode']).size()
    
    # Count series to dataframe
    SMS_count_df = pd.DataFrame({'CellCode':count_series.index, 'count':count_series.values})
    
    # adding segment name column to the SMS dataframe to match each cell code to its corresponidng segement
    SMS_count_df["Segment_name"] = ""
    
    # loop that matches segement name to cell code - ouput is (segement_name - cell_code - count)
    for i in range(len(SMS_count_df)): # loop through each row in the SMS file
        for j in range(len(CCE_file_selected)): # inner loop each row in the CCE file
            if SMS_count_df['CellCode'][i] in CCE_file_selected['Rules'][j]: 
                SMS_count_df['Segment_name'][i] = CCE_file_selected['Source'][j] 
                
    SMS_count_df = SMS_count_df[['Segment_name', 'CellCode', 'count']]
    # group by segement and cell code and return number of rows 
    Counts_table = SMS_count_df.groupby(['Segment_name','CellCode']).sum('count') 
    
   

    CCE_file_output = CCE_file_selected[['Source','Content EN','Content FR']]
    
    
    return Counts_table

Counts_table should be displayed when the Run button is clicked

5
  • "Counts_table should be displayed" You didn't state how you want to display the table. What have you tried to display the table and what problem you came across? Also better use askopenfilename() instead of askopenfile() if you just want the filename at all. Commented Mar 8, 2022 at 6:17
  • I am trying to display the dataframe as part of the GUI (not a simple print statement when the function is executed. Commented Mar 8, 2022 at 6:20
  • I incorporated the following answer from a previous question into the process_files() function stackoverflow.com/questions/57829917/… (using ttk.treeview) - that did not work Commented Mar 8, 2022 at 6:22
  • I am not sure how to make the Run button do two things together. First,run the process_files function and then taken the output of the function and display it Commented Mar 8, 2022 at 6:24
  • As I said, you better post what have you tried on showing the dataframe using treeview and the issue you came across. Commented Mar 8, 2022 at 6:33

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.