浏览器加载时+文本未更改,窗口无响应

时间:2018-09-05 15:14:57

标签: python python-3.x selenium tkinter selenium-chromedriver

为什么浏览器加载时根窗口没有响应...?

这是我代码中的错误还是所有错误?

可以解决吗?

如果是,怎么办?

enter image description here

我的代码:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.ui import Select
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import NoSuchElementException
import time
from tkinter import *
from tkinter import ttk
from tkinter import messagebox

root = Tk()
frame = Frame(root)
labelText = StringVar()
display = Label(frame, textvariable=labelText)
labelText.set("Connecting to the server...")
display.pack()
frame.pack()
display.update()

def change_root_label(message):
    labelText.set(message)
    display.pack()
    frame.pack()

change_root_label("Opening...") #updated code for updating question
driver = webdriver.Chrome()
driver.get('https://google.com')
change_root_label("Opened...")


root.mainloop()

为什么不显示文本Opening..

1 个答案:

答案 0 :(得分:0)

将函数change_root_label更改为此可以解决您的问题:

def change_root_label(message):
    labelText.set(message)
    display.update()

请记住,除非您想更改其包装(在其父级中的位置),否则无需再次包装您的小部件。在将代码设置为display.update()之后,将在代码中调用"Connecting to the server...",但是在将值更改为'Opening...'之后,您必须调用update或mainloop函数来更新UI。因此,在代码中,当mainloop最终被调用时,它将两次更新标签,但速度非常快,因此您不会看到文本'Opening...'