mike_f
mike_f

Reputation: 23

How to get button to launch a script

I am attempting to get a basic gui to run using PySimpleGui. The purpose of the application is to run a separate script (that works on its own) that splits email addresses i.e. [email protected] becomes |foo|bar|com in its own .csv file. I am having two issues when using the gui.

  1. I cannot get the 'Run' button to appear when I specify file_type.

layout = [[sg.In(),sg.FileBrowse(file_types=(("Text Files", "*.csv"),))]] [sg.Text('Email Parser')], [sg.Button('Run,key='_BUTTON_KEY_')] #button doesn't appear

When I omit specifying file type, I can see the 'Run' button.

layout = [[sg.Text('Email Parser')],[sg.Text('Source for Files',size=(15, 1)),sg.InputText(), sg.FileBrowse()],[sg.Button('Run',key='_BUTTON_KEY_')]]

I would ideally like the 'Run' button to appear while specifying the file type.

  1. I am not certain how to get the button to actually do what its supposed to do. I tried calling the 'parser' function, but to no avail. Here is a snippet of the code:
def parser():
  newFile = sys.argv[1]
  df = pd.read_csv(newFile)
  df['DOMAIN'] = [x.split('@')[-1] for x in df['EMAILADDRESS']]
  df['DNAME'] = [x.split('.')[-2] for x in df['DOMAIN'].str.lower()]
  df['TLD'] = [x.split('.')[-1] for x in df['EMAILADDRESS']]

  pd.DataFrame.to_csv(df,"bar" + time.strftime('%Y-%m-%d') + ".csv",',')

Any help is appreciated. If there is something in the docs for PySimpleGui I missed by all means show me the way. Thank you in advance.

Edit. So now I have this:

`  import pandas as pd
   import numpy as np
   import time
   import sys
   import PySimpleGUI as sg
   import argparse
   
   newFile = sys.argv[1]
   
   def parser(newFile):
     
       df = pd.read_csv(newFile)
       df['DOMAIN'] = [x.split('@')[-1] for x in df['EMAILADDRESS']]
       pd.DataFrame.to_csv(df,"bar" + time.strftime('%Y-%m-%d') + ".csv",',') 
   parser(newFile)
   
   sg.theme('Default')

   layout = [[sg.In(), sg.FileBrowse(file_types=(("Text Files", "*.csv"),))], 
   [sg.Text('Email Parser')], [sg.Button('Run',key='_BUTTON_KEY_')]]
   [sg.Text('Email Parser')],

window = sg.Window('Email Parser', layout)

while True:  # Event Loop
    event, values = window.read()
    print(event, values)
    if event == sg.WIN_CLOSED:
        break
    if event == 'Run':
        parser(newFile)
window.close()

The 'Run' button works now, but the script runs automatically on startup.Thank you in advance any help is appreciated.

Upvotes: 0

Views: 284

Answers (2)

mike_f
mike_f

Reputation: 23

I figured out a solution. Below is the complete code:

def parser(newFile):
     
       df = pd.read_csv(newFile)
       df['DOMAIN'] = [x.split('@')[-1] for x in df['EMAILADDRESS']]
       pd.DataFrame.to_csv(df,"bar" + time.strftime('%Y-%m-%d') + ".csv",',') 
sg.theme('DefaultNoMoreNagging')

layout = [[sg.In(), sg.FileBrowse(file_types=(("Text Files", "*.csv"),))],
         [sg.Text('Select file then hit run button to complete process.')], 
         [sg.Button('Run'), sg.Exit()]]

window = sg.Window('Email Pattern finder.', layout)

while True:  # Event Loop
    event, values = window.read()
    print(values[0]) #For testing
    
    if event == 'Run':
        parser(values[0])  #Need to use event values
    if event == sg.WIN_CLOSED or event == 'Exit':
        break
window.close()

Upvotes: 1

Mike from PSG
Mike from PSG

Reputation: 5764

Your first line of code is filled with syntax errors. Mismatched brackets, missing quote and comma. Are you using an IDE so these are evident before you even try and run it?

This them results in this code which works fine:

import PySimpleGUI as sg

layout = [[sg.In(), sg.FileBrowse(file_types=(("Text Files", "*.csv"),))],[sg.Text('Email Parser')], [sg.Button('Run',key='_BUTTON_KEY_')]]

sg.Window('Window Title', layout).read()

Upvotes: 0

Related Questions