0

I would need your help, I created this script in python, and now I added the GUI in Tkinter to it, the script itself works, it's about webscraping and then creating events for the calendar to save in csv... The problem is that when I start the script via the login button, the app stops for the time it takes to perform the web scraping, for the duration of about a minute, a user would think that the app is stuck and close it by mistake, if I do it via the terminal on the one hand it's almost better because I had printed for each day scanned the result so to see the progress. thing that in the graphical version I can not do ... any help? Here is the complete code ... (I'm learning python for a month) :-)

from logging import root
from tkinter import *
import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showinfo
from calendar import month_name
from datetime import datetime
from tkinter.ttk import Progressbar

def login():
    
    ivuusername= (entry1.get()) #IVU Benutzername
    ivupassword= (entry2.get()) #IVU Password
    jahr = (anno.get())
    monat = (monats.get())

    from selenium import webdriver
    from selenium.webdriver.support.ui import WebDriverWait
    import time
    import pandas as pd
    from bs4 import BeautifulSoup as bs
    from msedge.selenium_tools import EdgeOptions

    mytext.set('hello diriye')
    # IVU credentials
    username = ivuusername
    password = ivupassword
    # IVU credentials

    # options
    options = EdgeOptions()
    options.use_chromium = True
    options.add_argument("--headless")
    options.add_argument("disable-gpu")
    options.add_argument('--allow-running-insecure-content')
    options.add_argument('--ignore-certificate-errors')
    options.add_experimental_option('excludeSwitches', ['enable-logging'])


    # initialize the Chrome driver
    driver = webdriver.Chrome(executable_path="C:\Program Files (x86)\Microsoft\Edge\Application\msedgedriver.exe", options=options)

    # initialize the Chrome driver
    # head to github login page
    driver.get("https://sgv.ivu-cloud.com/mbweb/j_security_check")
    # find username/email field and send the username itself to the input field
    driver.find_element_by_id("j_username").send_keys(username)
    # find password input field and insert password as well
    driver.find_element_by_id("j_password").send_keys(password)
    # click login button
    driver.find_element_by_id("login-button").click()
    # wait the ready state to be complete
    WebDriverWait(driver=driver, timeout=5).until(
        lambda x: x.execute_script("return document.readyState === 'complete'")
    )
    error_message = "Incorrect username or password."
    # get the errors (if there are)
    errors = ''
    # print the errors optionally
    # for e in errors:
    #     print(e.text)
    # if we find that error message within errors, then login is failed
    if any(error_message in e.text for e in errors):
        mytext.set("[!] Login failed")
    else:
        mytext.set("[+] Login successful")

    

    year = jahr
    month = monat
    EmployeeId = '0000'
    mytext.set(year)
    mytext.set(month) 
    for day in range(1, 32):
        web1 = driver.get(f"https://sgv.ivu-cloud.com/mbweb/main/matter/desktop/main-menu#duty-details?beginDate={year}-{month}-{day}&allocatedEmployeeId={EmployeeId}")
        time.sleep(2)
        web1 = driver.page_source
        soup = bs(web1, features = "lxml")
        ############# USER FINDER ##############
        
        userfinder = soup.find('span', class_='ivupad-user-first-name')
        user = (userfinder.text)
        
        ############# USER FINDER ##############
        if soup.find_all('div', class_='cont'):
            df = pd.read_html(web1)
            table = df[0]
            Fahrzeugnummer = (df[0][2:].Fahrzeugnummer).to_list() # Kolonne 2
            DataFrame = table.drop_duplicates(subset=['Fahrt','Fahrzeugnummer'])
            Fahrt = (df[0][1:].Fahrt).to_list() # Kolonne 1
            soupdata = soup.find_all('div', class_='cont')
            soup_datum = (soupdata[1].text)
            soup_funktion = (soupdata[2].text)
            soup_dienstbegin = (soupdata[3].text)
            soup_dienstende = (soupdata[4].text)
            soup_schichtdauer = (soupdata[5].text)
            soup_bezahltezeit = (soupdata[6].text)
            
            ############# IVU KOMMENTAR ##############
            try:
                soup_kommentar = soupdata[7].text
            except IndexError:
                soup_kommentar = '-'
            ############# IVU KOMMENTAR ##############

            # Fahrzeugnummer (Schiffsname)
            Fahrzeugnummer
            a = Fahrzeugnummer
            # using loop + replace() + enumerate()
            for idx, ele in enumerate(Fahrzeugnummer):
                    Fahrzeugnummer[idx] = ele.replace('Fahrzeugnummer','')
            Fahrzeugnummer = ' '.join(map(str, Fahrzeugnummer))
            temp2 = []
            for element in Fahrzeugnummer:
                if element not in temp2:
                    temp2.append(element)
            b = temp2
            # trasformazione lista battelli in stringa
            b = ''.join(map(str, b))
            # Doppelschiffsname Ende
            
            ############ DIVERSE DIENST SUCHE ############
            tagesinfo = soup.find_all('h2', class_='mdl-card__title-text')
            infoliste = []  
            for tagesinfo in tagesinfo:  
                name = tagesinfo.find('span', attrs={'class':'allocation-title'}).text # result not results
                infoliste.append((name))
            liste = [x for x in infoliste if x]
            try:
                tagesinfo2 = (liste[0])
            except IndexError:
                tagesinfo2 = ''        
            ############ DIVERSE DIENST SUCHE ############
            
            ############ ERSTE KURS FÜR MANNSCHAFT SUCHE ############
            results = soup.find_all('td', attrs={'class':'trip_numbers mdl-data-table__cell--non-numeric'})
            records = []  
            for result in results:  
                name = result.find('span', attrs={'class':'value'}).text # result not results
                records.append((name))
            list2 = [x for x in records if x]
            try:
                ersterkurs = (list2[2])
            except IndexError:
                ersterkurs = ''
            ############ ERSTE KURS FÜR MANNSCHAFT SUCHE ############
                    # Doppelkurse entfernen
            temp = []
            for element in list2:
                if element not in temp:
                    temp.append(element)
            string1 = ' '.join(temp)
            # Doppelkurse Ende
                            ############### BESATZUNG FINDER ###############
            web2 = driver.get(f"https://sgv.ivu-cloud.com/mbweb/main/matter/desktop/_-crew-on-trip-table?tripNumber={ersterkurs}&beginDate={year}-{month}-{day}&_=16415838131080")
            time.sleep(2)
            web2 = driver.page_source
            soup2 = bs(web2, features = "lxml")   
            soupdata2 = soup2.find_all('span')
            crew_list = []
            try:
                crew_1 = soupdata2[5]
            except IndexError:
                crew_1 = ''
            for n in crew_1:
                    if n:
                        crew_list.append(n)
                    else:
                        pass
            try:
                crew_1f = soupdata2[6]
            except IndexError:
                crew_1f = ''
            for n in crew_1f:
                    if n:
                        crew_list.append(n)
                    else:
                        pass    
            try:
                crew_2 = soupdata2[7]
            except IndexError:
                crew_2 = ''
            for n in crew_2:
                    if n:
                        crew_list.append(n)
                    else:
                        pass
            try:
                crew_2f = soupdata2[8]
            except IndexError:
                crew_2f = ''
            for n in crew_2f:
                    if n:
                        crew_list.append(n)
                    else:
                        pass
            try:
                crew_3 = soupdata2[9]
            except IndexError:
                crew_3 = ''
            for n in crew_3:
                    if n:
                        crew_list.append(n)
                    else:
                        pass
            try:
                crew_3f = soupdata2[10]
            except IndexError:
                crew_3f = ''
            for n in crew_3f:
                    if n:
                        crew_list.append(n)
                    else:
                        pass
            try:
                crew_4 = soupdata2[11]
            except IndexError:
                crew_4 = ''
            for n in crew_4:
                    if n:
                        crew_list.append(n)
                    else:
                        pass
            try:
                crew_4f = soupdata2[12]
            except IndexError:
                crew_4f = ''
            for n in crew_4f:
                    if n:
                        crew_list.append(n)
                    else:
                        pass
            try:
                crew_5 = soupdata2[13]
            except IndexError:
                crew_5 = ''
            for n in crew_5:
                    if n:
                        crew_list.append(n)
                    else:
                        pass
            try:
                crew_5f = soupdata2[14]
            except IndexError:
                crew_5f = ''
            for n in crew_5f:
                    if n:
                        crew_list.append(n)
                    else:
                        pass
            try:
                crew_6 = soupdata2[15]
            except IndexError:
                crew_6 = ''
            for n in crew_6:
                    if n:
                        crew_list.append(n)
                    else:
                        pass
            try:
                crew_6f = soupdata2[16]
            except IndexError:
                crew_6f = ''
            for n in crew_6f:
                    if n:
                        crew_list.append(n)
                    else:
                        pass
            try:
                crew_7 = soupdata2[17]
            except IndexError:
                crew_7 = ''
            for n in crew_7:
                    if n:
                        crew_list.append(n)
                    else:
                        pass
            try:
                crew_7f = soupdata2[18]
            except IndexError:
                crew_7f = ''
            for n in crew_7f:
                    if n:
                        crew_list.append(n)
                    else:
                        pass
            try:
                crew_8 = soupdata2[19]
            except IndexError:
                crew_8 = ''
            for n in crew_8:
                    if n:
                        crew_list.append(n)
                    else:
                        pass
            try:
                crew_8f = soupdata2[20]
            except IndexError:
                crew_8f = ''
            for n in crew_8f:
                    if n:
                        crew_list.append(n)
                    else:
                        pass

            crew_list2 = ' '.join(crew_list)
            ############### BESATZUNG FINDER ###############
            
            ############### STRING1 ODER TAGESINFO ###############  
            
            items = string1

            if items.count('') > 1:
                tagesinfo2 = ''
            ############### BSTRING1 ODER TAGESINFO ENDE ###############
                
            # Writing on a CSV FILE
            fileToWrite = open(f"{user}-{month}-{year}.csv", "a")
            fileToWrite.write('Subject,Start Date,Start Time,End Date,End Time,All Day Event,Description\n')
            fileToWrite.write(f'{string1}{tagesinfo2},{soup_datum},{soup_dienstbegin},{soup_datum},{soup_dienstende},,Kommentar: {soup_kommentar} Schiff: {b} Funktion: {soup_funktion} Schichtdauer: {soup_schichtdauer} Bezahlte Zeit: {soup_bezahltezeit} Mannschaft: {crew_list2}\n')
            fileToWrite.close()
            mytext.set(f'Datum: {soup_datum} Dienst: {string1}{tagesinfo2} --> Mannschaft: {crew_list2} --> OK')
        elif soup.find_all('span', class_='allocation-title'):
            Ruhetagfinder = soup.find_all('span', class_='allocation-title')
            tagesinfo = (Ruhetagfinder[0].text)
            datumfinder = soup.find_all('div', id='info')
            datum = (datumfinder[0])
            datumcleaned = ''.join(map(str, datum))
            for character in ',':
                tagesinfo = tagesinfo.replace(character, '')
            for character in 'abcdefghilmnopqrstuvz()ABCDEFGHILMNOPQRSTUVZKk-_':
                datumcleaned = datumcleaned.replace(character, '')
            # Writing on a CSV FILE
            fileToWrite = open(f"{user}-{month}-{year}.csv", "a")
            fileToWrite.write('Subject,Start Date,Start Time,End Date,End Time,All Day Event,Description\n')
            fileToWrite.write(f'{tagesinfo},{datumcleaned[:10]},,,True,\n')
            fileToWrite.close()
            mytext.set(f'Datum: {datumcleaned[:10]} {tagesinfo} --> OK')
        else:
            mytext.set(f'- LESEFEHLER!- ')
        

    driver.close()



ws = Tk()
ws.title("Monatseinteilungs Tool")
ws.geometry("800x1000")
label = Label(ws)

mytext=StringVar()
anno=IntVar()
monats=IntVar()


Label(ws, text="Benuzername:", font= ('Helvetica 9')).grid(row=0, sticky=W)
entry1 = Entry(ws,width= 30,font= ('Helvetica 9'))
entry1.grid(row=0, column=1)

Label(ws, text="Password:", font= ('Helvetica 9')).grid(row=2, sticky=W)
entry2 = Entry(ws,show="*",width= 30,font= ('Helvetica 9'))
entry2.grid(row=2, column=1)

Label(ws, text="Ergebnis:", font= ('Helvetica 9')).grid(row=8, sticky=W)


result=Label(ws, height= 20, text='',textvariable=mytext, width= 80, bg = 'light blue').place(relx=.6, rely=.3,anchor= NE)


#outputtext = Text (ws, height= 20, width= 40, bg = 'light cyan').grid(column=1, row=15)

var = IntVar()
#Jahr Wählen#

Label(ws, text="Jahr auswählen:", font= ('Helvetica 10')).grid(row=5,column=0, sticky=W)
R1 = Radiobutton(ws, text = "2022", variable = anno, value = 2022, font= ('Helvetica 9'))
R1.grid(column=0, row=6)
R2 = Radiobutton(ws, text = "2021", variable = anno, value = 2021, font= ('Helvetica 9'))
R2.grid(column=0, row=7)

#Jahr Wählen#


#Monat Wählen#

Label(ws, text="Wähle das Jahr:").grid(row=4, sticky=W)
M1 = Radiobutton(ws, text = "Januar",variable = monats, value = 1)
M1.grid(column=1, row=5)
M2 = Radiobutton(ws, text = "Februar", variable = monats, value = 2)
M2.grid(column=1, row=6)
M3 = Radiobutton(ws, text = "März", variable = monats, value = 3)
M3.grid(column=1, row=7)
M4 = Radiobutton(ws, text = "April", variable = monats, value = 4)
M4.grid(column=2, row=5)
M5 = Radiobutton(ws, text = "Mai", variable = monats, value = 5)
M5.grid(column=2, row=6)
M6 = Radiobutton(ws, text = "Juni", variable = monats, value = 6)
M6.grid(column=2, row=7)
M7 = Radiobutton(ws, text = "Juli", variable = monats, value = 7)
M7.grid(column=3, row=5)
M8 = Radiobutton(ws, text = "August", variable = monats, value = 8)
M8.grid(column=3, row=6)
M9 = Radiobutton(ws, text = "September", variable = monats, value = 9)
M9.grid(column=3, row=7)
M10 = Radiobutton(ws, text = "Oktober", variable = monats, value = 10)
M10.grid(column=4, row=5)
M11 = Radiobutton(ws, text = "November", variable = monats, value = 11)
M11.grid(column=4, row=6)
M12 = Radiobutton(ws, text = "Dezember", variable = monats, value = 12)
M12.grid(column=4, row=7)

button = Button(ws, text="Login", command=login,width=20, height=2 ,font= ('Helvetica 10 bold')).grid(row=8,column=1, sticky=W)




ws.mainloop()
TUX One
  • 107
  • 3
  • 11
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Feb 07 '22 at 11:31
  • Does this answer your question? [Tkinter window says (not responding) but code is running](https://stackoverflow.com/questions/18522171/tkinter-window-says-not-responding-but-code-is-running) – wovano Mar 06 '22 at 14:33
  • You should never call long-running functions from the Tkinter thread (e.g. as a button action), since that will hang the GUI. The above-mentioned [question](https://stackoverflow.com/questions/18522171/tkinter-window-says-not-responding-but-code-is-running) is very similar and the answers definitely apply to your case as well. – wovano Mar 06 '22 at 14:45

0 Answers0