好的,所以我正在尝试创建一个用于控制Selenium进程的Tkinter GUI。我想在GUI中添加一个标签,显示从我按下启动按钮开始的运行时。我想要的是运行时时钟在代码运行Selenium进程时进行自我更新。问题是当我按下";启动";按钮,运行时时钟将更新前一秒,并且在Selenium进程完成之前不会再次更新。这里有一个最小的可复制示例:
注意:如果运行此代码,请注意connect_driver()
函数安装CHOMEDRIVER
import tkinter as tk
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
class GUI():
def __init__(self):
self.root = tk.Tk()
self.root.geometry("200x200")
# run-time clock
self.run_time_label = tk.Label(self.root, text="Run Time:")
self.run_time_label.pack()
self.run_time = tk.Label(self.root, text="00:00:00:00")
self.run_time.pack()
self.run_time_data = {"days":0, "hours":0, "minutes":0, "seconds":0}
# start button
self.start_button = tk.Button(self.root, text="Start", bg="green", height=1, width=10, command=self.start)
self.start_button.pack()
self.root.mainloop()
def start(self):
self.update_runtime()
self.change_state()
self.connect_driver()
self.work()
def stop(self):
driver.close()
driver.quit()
self.change_state()
def update_runtime(self):
if self.run_time_data["seconds"] < 59:
self.run_time_data["seconds"] += 1
elif self.run_time_data["seconds"] == 59:
self.run_time_data["seconds"] = 0
if self.run_time_data["minutes"] < 59:
self.run_time_data["minutes"] += 1
elif self.run_time_data["minutes"] == 59:
self.run_time_data["minutes"] = 0
if self.run_time_data["hours"] < 24:
self.run_time_data["hours"] += 1
elif self.run_time_data["hours"] == 23:
self.run_time_data["hours"] = 0
self.run_time_data["days"] += 1
time_string = "{:02d}:{:02d}:{:02d}:{:02d}".format(self.run_time_data["days"],self.run_time_data["hours"],self.run_time_data["minutes"],self.run_time_data["seconds"])
self.run_time.config(text=time_string)
self.root.after(1000, self.update_runtime)
def change_state(self):
if self.start_button.cget("text") == "Start":
self.start_button.configure(text="Stop", bg="red", command=self.stop)
elif self.start_button.cget("text") == "Stop":
self.start_button.configure(text="Start", bg="green", command=self.start)
self.root.update_idletasks()
def connect_driver(self):
global driver
driver = webdriver.Chrome(ChromeDriverManager().install())
def work(self):
for _ in range(3):
driver.get("http://www.google.com")
driver.get("https://stackoverflow.com")
driver.get("https://www.youtube.com")
if __name__ == "__main__":
app = GUI()
当运行此代码时,正如您所看到的,运行时时钟将在按下"0"后更新为1秒;"开始";,然后继续进行硒处理,但直到最后才再次更新运行时时钟。因此,我试图寻找解决方案,并认为我可能需要使用多处理器来独立运行update_runtime()
函数。问题是,我对多处理器很陌生(很明显(,我不清楚我应该如何在这里使用它。以下是我尝试过的:
import tkinter as tk
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
import multiprocessing as mp
class GUI():
def __init__(self):
self.root = tk.Tk()
self.root.geometry("200x200")
# run-time clock
self.run_time_label = tk.Label(self.root, text="Run Time:")
self.run_time_label.pack()
self.run_time = tk.Label(self.root, text="00:00:00:00")
self.run_time.pack()
self.run_time_data = {"days":0, "hours":0, "minutes":0, "seconds":0}
# start button
self.start_button = tk.Button(self.root, text="Start", bg="green", height=1, width=10, command=self.start)
self.start_button.pack()
self.root.mainloop()
def start(self):
self.process = mp.Process(target=self.update_runtime)
self.process.start()
self.process.join()
self.change_state()
self.connect_driver()
self.work()
def stop(self):
driver.close()
driver.quit()
self.change_state()
def update_runtime(self):
if self.run_time_data["seconds"] < 59:
self.run_time_data["seconds"] += 1
elif self.run_time_data["seconds"] == 59:
self.run_time_data["seconds"] = 0
if self.run_time_data["minutes"] < 59:
self.run_time_data["minutes"] += 1
elif self.run_time_data["minutes"] == 59:
self.run_time_data["minutes"] = 0
if self.run_time_data["hours"] < 24:
self.run_time_data["hours"] += 1
elif self.run_time_data["hours"] == 23:
self.run_time_data["hours"] = 0
self.run_time_data["days"] += 1
time_string = "{:02d}:{:02d}:{:02d}:{:02d}".format(self.run_time_data["days"],self.run_time_data["hours"],self.run_time_data["minutes"],self.run_time_data["seconds"])
self.run_time.config(text=time_string)
self.root.after(1000, self.update_runtime)
def change_state(self):
if self.start_button.cget("text") == "Start":
self.start_button.configure(text="Stop", bg="red", command=self.stop)
elif self.start_button.cget("text") == "Stop":
self.start_button.configure(text="Start", bg="green", command=self.start)
self.root.update_idletasks()
def connect_driver(self):
global driver
driver = webdriver.Chrome(ChromeDriverManager().install())
def work(self):
for _ in range(3):
driver.get("http://www.google.com")
driver.get("https://stackoverflow.com")
driver.get("https://www.youtube.com")
if __name__ == "__main__":
app = GUI()
但是当我运行这个时,我得到一个错误:EOFError: Ran out of input
请帮忙!:(
与其使用多处理,不如尝试使用线程,因为对于更新一个标签这样简单的事情,它会更好地工作。
https://realpython.com/intro-to-python-threading/
通过这种方式,您可以创建一个新函数,只用于用循环更新标签,并在start((函数中调用它。