I'm having trouble linking B.py script functions in the MAIN.py graphics window. This is a test of Tkinter's graphics window to check if the scraping (for the purpose of my personal and didactic study) was successful or there were errors. B.py is the script (works fine, okay) for scraping and error detection, while MAIN.py is the graphical test window. I have problems with Main.py
I write in order the steps I would like to obtain. I wrote their code (below), but something is wrong. I think they are very simple, but I searched the web and couldn't solve:
- I open the MAIN.py graphics window and click the Start button. The B.py file is executed
- If the scraping was successful, the small blue image / icon "boh.png" is replaced with the green (ok) image contained in /home/mypc/Desktop/Folder/img/green.png
- If the scraping was not successful and there is an error, I get the red icon contained in /home/mypc/Desktop/Folder/img/red.png which replaces the blue one.
- In case of errors, besides the image replacement, I would like to display the related 3 error (expect 1, expect 2 or if records_added_Resultati == 0) instead of the label "Message"
ERROR and PROBLEMS: After scraping, the blue icon is not replaced by the red or green icon. Also, the error message (except 1, or expect 2, or if ....) is not displayed in the Messages label.
MAIN.PY (graphics window)
from tkinter import *
from tkinter import ttk
import tkinter as tk
import tkinter.font as tkFont
from PIL import ImageTk, Image
from File import B
def draw_graph():
test_scraping=tk.Toplevel()
test_scraping.title("Test")
test_scraping.geometry("800x600")
test_scraping.configure(bg='#282828')
testN1 = Label(test, text="TEST N.1", bg="#282828", foreground='white')
testN1.place(x=6, y=12)
image_blu= Image.open("/home/mypc/Desktop/Folder/File/img/blu.png")
render1 = ImageTk.PhotoImage(image_blu)
image_blu = Label(test, image=render1, bg='#282828')
image_blu.place(x=76, y=12)
message = Label(test, text="Message ", bg="#282828", foreground='white')
message.place(x=156, y=12)
def do_scraping():
msg = B.scraping()
if msg:
message.configure(text=msg)
image_red= Image.open("/home/mypc/Desktop/Folder/File/img/error.png")
render7 = ImageTk.PhotoImage(image_red)
image_red = Label(test_scraping, image=render7, bg='#282828')
image_red.place(x=400, y=12)
else:
image_green= Image.open("/home/mypc/Desktop/Folder/File/img/ok.png")
render8 = ImageTk.PhotoImage(image_green)
image_green = Label(test_scraping, image=render8, bg='#282828')
image_green.place(x=400, y=12)
button = Button(test, text="Avvia", bg='#e95420', foreground='white', command=do_scraping)
button.place(x=6, y=112)
test.mainloop()
B.PY
from tkinter import *
from tkinter import ttk
import tkinter as tk
import sqlite3
from selenium.common.exceptions import NoSuchElementException
def scraping:
#Code Tor Connection. Useless to write it down. not important for the purposes of the example
try:
#Serie A
driver.get("link")
driver.close
SerieA=driver.find_element_by_class_name("teamHeader__name")
SerieA_text = SerieA.text
print(SerieA.text)
#Serie B
driver.get("link")
driver.close
SerieB=driver.find_element_by_class_name("teamHeader__name")
SerieB_text = SerieB.text
print(SerieB.text)
except NoSuchElementException:
return "FAILED: Error class name html"
except ValueError:
return "FAILED: Error ValueError"
if records_added_Risultati == 0:
raise ValueError("FAILED: 0 record scraping")
#Code for insert in database