0

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 enter image description here

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:

  1. I open the MAIN.py graphics window and click the Start button. The B.py file is executed
  2. 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
  3. 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.
  4. 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
4
  • Multiple questions/remarks: When do you call draw_graph(), because this is creating your Toplevel. Also this example is not reproducible. But I thik your error is in the return of your scraping function. As it is called by the button, there is no variable where your return value can be stored. Maybe this might help. Commented Jul 13, 2021 at 7:38
  • @steTATO Scraping happens correctly. The problem is to change the icon (green or red) and display the possible error message in the window of the other py file. Do you think my mistake is in the return of the scraping function? Why? Anyway a curiosity: did I do the code for changing the icon right? Could you help me by writing me an answer with what I should change? I'm going crazy. Thank you Commented Jul 13, 2021 at 7:43
  • What is from file mean in import part? Is file name of folder? Because B is the name of file, right? Identation of do_scraping function is also wrong! Make sure you have that image in right path. Commented Jul 13, 2021 at 7:48
  • @Kshitiz By way of example, to facilitate the reader, yes, I wrote that B.py is the name of the file (the one of the scraping script that works correctly), while Main is the title of the window. From File means that the files are contained in a subfolder: / home / mypc / Desktop / Folder / File / Commented Jul 13, 2021 at 7:52

1 Answer 1

1

You hadn't actually placed label in right place. You had place it in x=400 and I think that's the reason you are not seeing that label(image) in right place. Try to change that label in x=76 where that blue image is located. And try to destroy blue image label before placing other label.

Edit1:

You can replace image doing :

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)
image_blu.place(x=76, y=12)

message = Label(test, text="Message ", bg="#282828", foreground='white')
message.place(x=156, y=12)

image_green= Image.open("/home/mypc/Desktop/Folder/File/img/ok.png")
render8 = ImageTk.PhotoImage(image_green)

image_red= Image.open("/home/mypc/Desktop/Folder/File/img/error.png")
render7 = ImageTk.PhotoImage(image_red)
            
def do_scraping():
        msg = B.scraping()
        if msg:
            message.configure(text=msg)
            image_blu.config(image=render7)
        else:
            image_blu.config(image=render8)

button = Button(test, text="Avvia", bg='#e95420', foreground='white', command=do_scraping)
button.place(x=6, y=112)

test.mainloop()

Instead of making new label/image. Replacing that existing label will be efficient.

Edit2:

records_added_Risultati == 0 if this condition is true then, there is no way. User can know what is going on without looking on terminal. So, It should return something, so that user will get the red icon and know, scraping was not successful.

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:
       return "FAILED: 0 record scraping"
Sign up to request clarification or add additional context in comments.

9 Comments

Can you show me with code how I can destroy the blue image label before placing another one? I just wanted to "replace", not destroy first. Anyway what about the error message (or except1, or except2, or if ....) that doesn't appear in the "Messagge" label? I'm new to Python, sorry. Thank you
@FrederickMan I would say that it would be pretty inefficient if someone designed it to only overlap the image, basically meaning that the label now stores two images, considering that I would say it gets replaced
@Kshitiz Yesssssssssssss, with the green icon it works when the scraping is successful. Thanks :) Now I try to voluntarily create an error to try with the red icon. In the meantime, can you try to resolve the display of error messages? A question: the red error icon, in addition to the 2 except, should also appear for if records_added_Risultati == 0 :? Because I have set that even that is an error. So there are 3 errors: 2 with except and 1 with if.
I don't think there is anything wrong in my code! I do think that you forget to write message = Label(test, text="Message ", bg="#282828", foreground='white') message.place(x=156, y=12) these lines.
Yeah! You are doing mistake there also. raise is used for this and in that if condition you aren't returning anything so, it is not changing color.So, try to return any exceptional message or error message there also. Then your problem should be fixed.!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.