Python Forum
[Tkinter] Returning always the last image into Label
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Returning always the last image into Label
#1
I'm making a web scrapping on a website for a private game server using Python. In a part of this website prints the result of the last nation war winner and show 2 messages: "Capella Ganhou!!" and "Procyon Ganhou!!" using the BeautfulSoup library. I made a condition for each event for show the winner nation image into the Label by an url. But for some reason it shows always the last image url and I don't know what is happening. Here is the code below:

from tkinter import *
import urllib.request
from bs4 import BeautifulSoup
from IPython.display import Image, display
from PIL import Image, ImageTk
from io import BytesIO

def capella_tg():
    resultado_tg = StringVar()
    try:
        resultado_tg.set(soup.find_all("font")[5].string)
        label_resultado_tg = Label(root, textvariable=resultado_tg, font=("Consolas", 25)).pack()
    except:
        return False

def procyon_tg():
    resultado_tg = StringVar()
    try:
        resultado_tg.set(soup.find_all("font")[4].string[3:])
        label_resultado_tg = Label(root, textvariable=resultado_tg, font=("Consolas", 25)).pack()
    except:
        return False

root = Tk()
root.title("Resultado TG Cabal Ghost")

with urllib.request.urlopen("http://www.cabaleasy.com") as url: page = url.read()
soup = BeautifulSoup(page, "html.parser")

if capella_tg():
    url = "https://i.imgur.com/AHLqtt0.jpg"    
    with urllib.request.urlopen(url) as u: raw_data = u.read()        
    im = Image.open(BytesIO(raw_data))
    image = ImageTk.PhotoImage(im)
    label = Label(image=image).pack()
else:
    procyon_tg()
    url = "https://i.imgur.com/TQyCnfD.jpg"    
    with urllib.request.urlopen(url) as u: raw_data = u.read()        
    im = Image.open(BytesIO(raw_data))
    image = ImageTk.PhotoImage(im)
    label = Label(image=image).pack()


root.mainloop()
Reply
#2
The only possible return values for capella_tg() are None or False.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter: Image to Label Maryan 10 668 Oct-29-2020, 01:48 PM
Last Post: joe_momma
  Refresh image in label after every 1s using simple function jenkins43 1 2,604 Jul-28-2019, 02:49 PM
Last Post: Larz60+
  [Tkinter] Returning Entry as a Label ashleyrfm94 2 1,003 Jul-22-2019, 04:47 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020