我正在尝试创建这个应用程序,以便它找到拖缆视图,如果它找不到XPATH,我希望它输出"拖缆脱机";但它只是输出";拖缆脱机";即使是在线流媒体。此外,当键入新拖缆时,标签不会覆盖,而是创建一条新行。
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()
在tkinter
中,我们使用config()
来编辑创建的小部件的属性。在这里,我在函数外创建标签,然后只在函数内编辑它,这样就避免了重写。
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
避免串联的更好方法是使用f
字符串:
views_amount.config(text=f"There are currently {twitch_viewers} Live viewers in {twitchname.get()}'s stream.")
其他文本也是如此。
此外,还建议为每个小部件传递master
,以避免在使用多个窗口等时出现混淆:
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)
尽管你在这里问了不止一个问题,但我已经回答了tkinter
部分。对于selenium
部分,您可能想问一个新问题——每个问题一个主题是可行的。