簡體   English   中英

如何將我的 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 的選項上使用 if 和 else 相應的 chromedriver.exe 文件,提交運行應用程序。 我知道我將不得不將自動化部分制作成 function 但是我在哪里放置自動化代碼和 arguments 文件,chromeversion和條件?

任何和所有的幫助表示贊賞。

單擊Submit按鈕時,然后 go 你my script 也許是這樣

import threading
import selenium
import PySimpleGUI as sg

def run_selenium(window, file, driver):
    # My script
    with open(file, 'rt') as csv_file:
        csv_reader = csv.reader(csv_file)
    driver = webdriver.Chrome(executable_path=driver)
    ...
    # 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
    paths = {
        '96': './chromedriver96.exe',
        '97': './chromedriver97.exe',
        '98': './chromedriver98.exe',
    }

    while True:

        event, values = window.read(timeout=100)
        ...
        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()
            window['Submit'].update(disabled=True)
            print('[LOG] Run Selenium ...')
        elif event == 'Done':
            window['Submit'].update(disabled=False)
            print('[LOG] End Selenium')

    window.close()

使用 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 的 function 中

# -------------------------------------------------------------------------------
# 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)
        driver.get('site/')

        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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM