将Python脚本转换为GUI [已关闭]

2nc8po8w  于 2023-04-22  发布在  Python
关注(0)|答案(1)|浏览(105)

已关闭,该问题需要details or clarity,目前不接受回答。
**想要改进此问题?**通过editing this post添加详细信息并澄清问题。

8天前关闭
Improve this question
你好,我是一个在Python中创建基于GUI的代码的新手。如何通过传递Month_nameYear_Num值将下面的完整代码转换为GUI?

from selenium.webdriver.support import expected_conditions as EC  

from selenium import webdriver  
from selenium.webdriver.support.ui import Select  
from selenium.webdriver.support.ui import WebDriverWait  
from webdriver_manager.chrome import ChromeDriverManager  
from selenium.webdriver.common.by import By  
from selenium.webdriver.common.keys import Keys  
from selenium.webdriver.common.action_chains import ActionChains  
    
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://www.wunderground.com/")
time.sleep(10)
driver.maximize_window()

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="wuSearch"]'))).send_keys("new york")
#WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[contains(., 'New York City')]"))).click()
time.sleep(10)

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="wuForm"]/search-autocomplete/ul/li[2]/a'))).click()  
History = driver.find_element("xpath",'//*[@id="inner-content"]/div[2]/lib-city-header/div[2]/lib-subnav/div/div[3]/ul/li[5]/a').click()  
time.sleep(5)

Monthly = driver.find_element("xpath",'//*[@id="inner-content"]/div[2]/div[1]/div[1]/div[1]/div/lib-link-selector/div/div/div/a[3]').click()  
time.sleep(5)  
Month_name = 'July'
Month_Selection = driver.find_element("xpath",'//*[@id="monthSelection"]')
Month_Selection.send_keys(Month_name)

Yean_Num = '2019'   
Year_Selection = driver.find_element("xpath",'//*[@id="yearSelection"]')  
Year_Selection.send_keys(Yean_Num)  

    # View Data
    SelectedData = driver.find_element(By.ID,'dateSubmit').click()
    Data= WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH,'//*[@id="inner-content"]/div[2]/div[1]/div[5]/div[1]/div/lib-city-history-observation/div/div[2]/table'))).text #extract daily average temp from that day and place
42fyovps

42fyovps1#

用你提供的代码将你的代码移动到一个函数def searchWeb(month,year)
然后,您可以运行以下代码,使用命令searchWeb(value['--MONTH--'],value['--YEAR--'])调用该函数,其中print语句为:

import PySimpleGUI as sg

if __name__ == '__main__':
    currentYear = 2023
    currentMonth = 6
    months = ['January','February','March','April','May','June','July','August','September','October','November','December']
    years = [str(i) for i in range(currentYear-10,currentYear+1)]
    layout = [[sg.T("Month:",size=5),sg.Combo(months,default_value=months[currentMonth-1],k="--MONTH--",size=(20,1))],
              [sg.T("Year:",size=5),sg.Combo(years,default_value=years[-1],size=(20,1),k="--YEAR--")],
              [sg.Push(),sg.B("Search",k="BUTTON"),sg.B("Quit",k="QUIT"),sg.Push()]]
    window = sg.Window("Search by Month and year",layout)
    while True:
        event,value = window.read()
        if event in ('QUIT', sg.WIN_CLOSED):
            window.close()
            break
        if event == 'BUTTON':
            print(f"Call function to search web here with values: Month:{value['--MONTH--']} Year:{value['--YEAR--']}")

相关问题