Skip to content
Advertisement

How to display something else when XPath hasn’t been found and overwriting previous Label

I am trying to create this app so that it finds the streamers views, if it doesn’t find the XPATH I want it to output “Streamer is offline” but it just outputs “Streamer is offline” even for online streamers. Also, when typing a new streamer the label does not overwrite, rather creates a new line.

from tkinter import *
import tkinter.font as font
from functools import partial
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time




# Gets the webpage "Twitch" then finds the user inputted and eventually displays the viewer count.
    

def string_twitch(twitchname):
    PATH = "/home/networkmojito/python/Tkinter/chromedriver"
    options = webdriver.ChromeOptions()
    options.add_argument('headless')
    options.add_argument('window-size=1920x1080')
    options.add_argument("disable-gpu")
    driver = webdriver.Chrome(PATH, options = options)
    driver.get("https://twitch.tv/" + twitchname.get())

    try:
        twitch_views = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH, "/html/body/div[1]/div/div[2]/div/main/div[2]/div[3]/div/div/div[1]/div[1]/div[2]/div/div[1]/div/div/div/div[2]/div[2]/div[2]/div/div/div[1]/div[1]/div/p"))
        )
        twitch_viewers = twitch_views.text()
        views_amount = Label(text = "There are currently " + twitch_viewers + " Live viewers in " + twitchname.get() + "'s stream.", fg = 'white', bg = 'black', font = font_ubun_size2)
        views_amount.pack()
    except:
        streamer_offline = Label(text = "Streamer is currently offline.", fg = 'white', bg = 'black', font = font_ubun_size2)
        streamer_offline.pack()
        driver.quit()
        




# creates the window, and "className = x" names the window
window = Tk(className = "Twitch Viewer")

# Window size
window.geometry("700x150")
window.configure(bg = "black")

font_ubun_size = font.Font(size = 28, family = 'Ubuntu Mono')
font_ubun_size2 = font.Font(size = 16, family = 'Ubuntu Mono')

# Username: and also box to enter the username, StringVar, just reveals the text as it is.
user_input = Label(text = "Enter Twitch Username:", fg = "purple", bg = "black", font = font_ubun_size)
user_input.pack()
twitchname = StringVar()
twitch_username = Entry(textvariable = twitchname, bg = '#88979e')
twitch_username.pack()

string_twitch = partial(string_twitch, twitchname)

submit_button = Button(text = "Submit.", fg = "white", bg = "black", activebackground = '#00cc70', activeforeground = 'white', command = string_twitch)
submit_button.pack()

window.mainloop()

Advertisement

Answer

In tkinter we use config() to edit properties of created widgets. Here I create the label outside the functions, then I edit it inside the functions only, so over-writing is avoided.

def string_twitch(twitchname):
    # Same code
    try:

        .... # same code
        twitch_viewers = twitch_views.text()
        views_amount.config(text="There are currently " + twitch_viewers + " Live viewers in " + twitchname.get() + "'s stream.")
        views_amount.pack()
    except:
        views_amount.config(text = "Streamer is currently offline.")
        views_amount.pack()
        driver.quit()

# Same codes

views_amount = Label(fg='white',bg='black',font=font_ubun_size2) # Make initial widget with common properties

A better way to avoid concatenation is to use f strings:

views_amount.config(text=f"There are currently {twitch_viewers} Live viewers in {twitchname.get()}'s stream.")

and so on for other text too.

Plus it is also recommended to pass in the master for each widgets so as to avoid confusion when working with multiple windows and so on:

user_input = Label(window, text="Enter Twitch Username:", fg="purple", bg="black", font=font_ubun_size)
twitchname = StringVar(window)
twitch_username = Entry(window, textvariable=twitchname, bg='#88979e')
submit_button = Button(window, text="Submit.", fg="white", bg="black", activebackground='#00cc70', activeforeground='white', command=string_twitch)

However you have asked more than one question here, I have answered the tkinter part. For the selenium part, you might want to ask a new question – one topic per question is the way to go.

Advertisement