如何将我的 python GUI 与我的 python 代码结合起来

How do I combine my python GUI with my python code

所以我用 Selenium 制作了一个脚本,可以从 CSV 中输入表单详细信息。现在的目标是制作一个 GUI,我也这样做了,现在我无法解决的棘手部分是两者的结合。

我的图形用户界面:


import PySimpleGUI as sg

def make_window(theme):
    sg.theme(theme)
    menu_def = [['&Application', ['E&xit']],
                ['&Help', ['&About']] ]
    right_click_menu_def = [[], ['Exit']]

    # Table Data


    input_layout =  [[sg.Menu(menu_def, key='-MENU-')],
                 
                
                [sg.Button("Open File")],
               
                [sg.Text('Chrome Version')],

                [sg.OptionMenu(values=('96', '97', '98'),  k='-OPTION MENU-'),],
               
                [sg.Button('Submit')]]



    
    
    layout = [[sg.Text('Email', size=(38, 1), justification='center', font=("Helvetica", 16), relief=sg.RELIEF_RIDGE, k='-TEXT HEADING-', enable_events=True)]]
    layout +=[[sg.TabGroup([[  sg.Tab('Setup CSV and Chrome Version', input_layout),
                               
                               ]], key='-TAB GROUP-')]]
              
    return sg.Window('Email', layout, right_click_menu=right_click_menu_def)


def main():
    window = make_window(sg.theme())
    
    # This is an Event Loop 
    while True:
        event, values = window.read(timeout=100)
        # keep an animation running so show things are happening
       
        if event not in (sg.TIMEOUT_EVENT, sg.WIN_CLOSED):
            print('============ Event = ', event, ' ==============')
            print('-------- Values Dictionary (key=value) --------')
            for key in values:
                print(key, ' = ',values[key])
        if event in (None, 'Exit'):
            print("[LOG] Clicked Exit!")
            break
        elif event == 'About':
            print("[LOG] Clicked About!")
            sg.popup('email',
                     'Select CSV file',
                     'Select Chrome Version',
                     'Submit',
                     '')
        elif event == 'Popup':
            print("[LOG] Clicked Popup Button!")
            sg.popup("You pressed a button!")
            print("[LOG] Dismissing Popup!")


        elif event == "Open File":
            print("[LOG] Clicked Open File!")
            folder_or_file = sg.popup_get_file('Choose your file')
            sg.popup("You chose: " + str(folder_or_file))
            print("[LOG] User chose file: " + str(folder_or_file))


    window.close()
    exit(0)

if __name__ == '__main__':
    main()

我的脚本:

#-------------------------------------------------------------------------------
# Imports
import csv
import requests
from selenium import webdriver
import time

#-------------------------------------------------------------------------------
# Setup


with open('data.csv', 'r') as csv_file:

    csv_reader = csv.reader(csv_file)

#-------------------------------------------------------------------------------
# Web Automation
    
    driver = webdriver.Chrome(executable_path='./chromedriver.exe')
    driver.get('site/')

    title_field = driver.find_element_by_xpath('//*[@id="TITLE"]')
    fname_field = driver.find_element_by_xpath('//*[@id="FIRSTNAME"]')
    lname_field = driver.find_element_by_xpath('//*[@id="LASTNAME"]')
    phone_field = driver.find_element_by_xpath('//*[@id="PHONE"]')
    mail_field = driver.find_element_by_xpath('//*[@id="EMAIL"]')
    depost_field = driver.find_element_by_xpath('//*[@id="DEPOSIT"]')
    submit = driver.find_element_by_xpath('//*[@id="sib-form"]/div[9]/div/button')

    for line in csv_reader:

        time.sleep(3)

        title_field.send_keys(line[0])


        fname_field.send_keys(line[1])

        
        lname_field.send_keys(line[2])
        
        
        phone_field.send_keys(line[3])

        
        mail_field.send_keys(line[4])

        
        depost_field.send_keys(line[5])

        
        submit.click()

#-------------------------------------------------------------------------------

我想从 GUI select CSV 文件,然后在 select 相应 chromedriver.exe 文件的选项上使用 if 和 else,提交给 运行 应用程序。我知道我必须将自动化部分变成一个函数,但是我应该在哪里放置自动化代码和文件、chromeversion 和条件的参数?

感谢任何帮助。

Submit 按钮被点击时,然后去你的 my script。大概是这样

import csv
import time
import threading
import selenium
import PySimpleGUI as sg

def run_selenium(window, file, driver):

    #-------------------------------------------------------------------------------
    # Web Automation
    driver = webdriver.Chrome(executable_path=driver)
    driver.get('site/')

    title_field = driver.find_element_by_xpath('//*[@id="TITLE"]')
    fname_field = driver.find_element_by_xpath('//*[@id="FIRSTNAME"]')
    lname_field = driver.find_element_by_xpath('//*[@id="LASTNAME"]')
    phone_field = driver.find_element_by_xpath('//*[@id="PHONE"]')
    mail_field = driver.find_element_by_xpath('//*[@id="EMAIL"]')
    depost_field = driver.find_element_by_xpath('//*[@id="DEPOSIT"]')
    submit = driver.find_element_by_xpath('//*[@id="sib-form"]/div[9]/div/button')

    with open(file, 'rt') as csv_file:
        csv_reader = csv.reader(csv_file)
        for line in csv_reader:
            time.sleep(3)
            title_field.send_keys(line[0])
            fname_field.send_keys(line[1])
            lname_field.send_keys(line[2])
            phone_field.send_keys(line[3])
            mail_field.send_keys(line[4])
            depost_field.send_keys(line[5])
            submit.click()

    # Not to update GUI in thread, but generate an event which will be processed in event loop.
    window.write_event_value('Done', None)

def main():
    # My GUI
    window = make_window(sg.theme())
    folder_or_file = None
    # Using your path for all the drivers of all versions
    paths = {
        '96': './chromedriver96.exe',
        '97': './chromedriver97.exe',
        '98': './chromedriver98.exe',
    }

    while True:
        event, values = window.read(timeout=100)
        # keep an animation running so show things are happening

        if event not in (sg.TIMEOUT_EVENT, sg.WIN_CLOSED):
            print('============ Event = ', event, ' ==============')
            print('-------- Values Dictionary (key=value) --------')
            for key in values:
                print(key, ' = ',values[key])
        if event in (None, 'Exit'):
            print("[LOG] Clicked Exit!")
            break
        elif event == 'About':
            print("[LOG] Clicked About!")
            sg.popup('email',
                     'Select CSV file',
                     'Select Chrome Version',
                     'Submit',
                     '')
        elif event == 'Popup':
            print("[LOG] Clicked Popup Button!")
            sg.popup("You pressed a button!")
            print("[LOG] Dismissing Popup!")
        elif event == "Open File":
            print("[LOG] Clicked Open File!")
            folder_or_file = sg.popup_get_file('Choose your file')
            sg.popup("You chose: " + str(folder_or_file))
            print("[LOG] User chose file: " + str(folder_or_file))
        elif event == 'Submit':
            version = values['-OPTION MENU-']
            if folder_or_file is None or version not in paths:
                print("No CSV file selected or wrong Chrome version selected")
                continue
            # Using thread to avoid long time job will block and cause GUI no response
            threading.Thread(target=run_selenium, args=(window, folder_or_file, paths[version])).start()
            # Disable Submit button to prevent submit again when threading
            window['Submit'].update(disabled=True)
            print('[LOG] Run Selenium ...')
        elif event == 'Done':
            # Enable Submit button when thread done
            window['Submit'].update(disabled=False)
            print('[LOG] End Selenium')

    window.close()

main()

使用 tkinter 和 configparser
你可以通过 pip install tk

获得 tkinter

gui.py

import tkinter as tk
import tkinter.filedialog as file_dialog
import threading
import your_script
import configparser


#class Config helps writing and reading from config.ini file.
class Config:
    def __init__(self):
        self.config = configparser.ConfigParser()
        self.read()

    def set(self, key, value):
        self.config.set('setup', key, value)
        self.write()

    def write(self):
        with open('config.ini', 'w') as f:
            self.config.write(f)

    def read(self):
        self.config.read('config.ini')

    def get(self, key):
        self.read()
        return self.config.get('setup', key)


def start_your_script():
    threading.Thread(target=your_script.start,args=(Config().get("csv_path"), Config().get("exe_path"))).start()


def open_file_dialog(chromedriver_path=False,csv_path=False):
    if csv_path:
        path = file_dialog.askopenfilename(filetypes=[("CSV Files", "*.csv")])
        Config().set("csv_path",path)

    elif chromedriver_path:
        path = file_dialog.askopenfilename(filetypes=[("Applications", "*.exe")])
        Config().set("exe_path",path)

Config().write()
window = tk.Tk()
window.title("My Window")
window.geometry('350x200')

panel = tk.Frame(window)
panel.pack(expand=True)

lbl = tk.Label(window, text="CSV FILE")
lbl.pack(expand=True)


btnCSV = tk.Button(window, text="Select",command=lambda: open_file_dialog(csv_path=True))
btnCSV.pack(expand=True)

lbl = tk.Label(window, text="Chromedriver")
lbl.pack(expand=True)

btnCD = tk.Button(window, text="Select",command=lambda: open_file_dialog(chromedriver_path=True))
btnCD.pack(expand=True)

btnCD = tk.Button(window, text="Start",command=start_your_script)
btnCD.pack(expand=True)

tk.mainloop()

并将您的脚本包装在一个名为 start 的函数中,例如

# -------------------------------------------------------------------------------
# Imports
import csv
import requests
from selenium import webdriver
import time
# -------------------------------------------------------------------------------
# Setup

def start(data,exe_path):
    with open(data, 'r') as csv_file:
        csv_reader = csv.reader(csv_file)
        # -------------------------------------------------------------------------------
        # Web Automation

        driver = webdriver.Chrome(executable_path=exe_path)
        #add this line
        driver.implicitly_wait(10)

        driver.get('https://www.google.si/')
        
        ids = "TITLE FIRSTNAME LASTNAME PHONE EMAIL DEPOSIT".split()
        for line in csv_reader:
            time.sleep(0.2)
            for i,id in enumerate(ids):
                driver.find_element_by_xpath(f'//*[@id="{id}"]').send_keys(line[i])

            #submit
            driver.find_element_by_xpath('//*[@id="sib-form"]/div[9]/div/button').click()
            # wait for notifcation --> driver.implicitly_wait(10) sets up the driver so it will wait for 10 seconds
            try:
                if "Bidder added successfully" == driver.find_element_by_class_name('sib-form-message-panel__inner-text').text:
                    # close the notifcation or just continue
                    pass
                else:
                    #bidder wasn't added succesfully
                    pass
            except:
                print("driver didn't find the element with 10 seconds wait period")
        driver.quit()