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
  • 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](https://stackoverflow.com/questions/13099908/python-tkinter-return-value-from-function-used-in-command) might help. – steTATO Jul 13 '21 at 07: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 – Frederick Man Jul 13 '21 at 07: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. – imxitiz Jul 13 '21 at 07: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 / – Frederick Man Jul 13 '21 at 07:52

1 Answers1

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"
imxitiz
  • 3,920
  • 3
  • 9
  • 33
  • 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 – Frederick Man Jul 13 '21 at 08:08
  • 1
    @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 – Matiiss Jul 13 '21 at 08:27
  • @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. – Frederick Man Jul 13 '21 at 09:03
  • 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. – imxitiz Jul 13 '21 at 09:28
  • @Kshitiz Ah ok, yes, if so I agree. You are right. True. However, you are also right for my forgetting the Label of the message. I accidentally deleted it. The errors are displayed in the label :). Maybe there is some problem with the red icon regarding if records_added_Resultati == 0 :, while except seems to be fine. Anyway, I'll tell you in a few hours that now I have to go away. I would like to test first better with more calm. Anyway quiet, I will give you the vote on the answer and I will accept. See you later. Thank you – Frederick Man Jul 13 '21 at 09:39
  • @Kshitiz Okok. However, everything works fine, we're almost there. Only if records_added_Resultati == 0 doesn't work. Even if 0 records are added, the icon does not turn red (and consequently the related error message is not displayed). I probably wrote the wrong IF part records_added_Resultati == 0: raise ValueError ("FAILED: 0 record scraping"). Or not? The two except are detected as errors, but the IF part is not. Did I write well? Should I have put records_added_Resultati == 0 inside an except and not inside an if? Can you help me? I think it's a very simple thing to solve. Thanks – Frederick Man Jul 13 '21 at 13:19
  • 1
    Yeah! You are doing mistake there also. `raise` is used for [this](https://stackoverflow.com/questions/13957829/how-to-use-raise-keyword-in-python) 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.! – imxitiz Jul 13 '21 at 13:43
  • @Kshitiz I figured this was the mistake. You are great, thank you. Should I write if records_added_Resultati == 0: return "message error"? Or (but I don't think so) except records_added_Resultati == 0: return "message error". Or more? If it's not a bother, can you please help me? So I vote you, accept your answer and we end this question here. Thank you :) – Frederick Man Jul 13 '21 at 21:08
  • @Kshitiz I had forgotten. Sorry. I have removed them now. – Frederick Man Jul 14 '21 at 02:52