0
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from tkinter import *
while True:
    
    selain = webdriver.Chrome()
    selain.get("https://m.kellonaika.fi")
    kello = selain.find_element_by_id("clock-time")
    selain.get("https://www.foreca.fi/Finland/Salo")
    sää = selain.find_element_by_class_name("l")
    sää1 = selain.find_element_by_class_name("r")
    print(sää.text)
    print(sää1.text)
    root = Tk()
    myLabel1 = Label(root, text="sää salossa nyt")
    myLabel3 = Label(root, text=sää.text)
    myLabel4 = Label(root, text=sää1.text)
    myLabel1.grid(row=0, column=2)
 
    myLabel3.grid(row=2, column=1)
    myLabel4.grid(row=3, column=1)

    selain.close()
    time.sleep(10)
    root.mainloop() 

while loop works if I close the Tkinter window. What can I do with root.mainloop?

1
  • This will just freeze your GUI for 10 seconds before showing your GUI Commented Oct 22, 2020 at 13:59

2 Answers 2

1

You wanna display Helsinki-Finland weather via www.foreca.com, but It is a bad approach, you just froze your app for 10 seconds in each loop.

time.sleep(10)  # freeze for 10 secs

Eliminate the while loop and use root.mainloop() instead, Then your app stays running, up to user close the app by clicking on x button.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from tkinter import *

selain = webdriver.Chrome()
selain.get("https://m.kellonaika.fi")
kello = selain.find_element_by_id("clock-time")
selain.get("https://www.foreca.fi/Finland/Salo")
sää = selain.find_element_by_class_name("l")
sää1 = selain.find_element_by_class_name("r")
print(sää.text)
print(sää1.text)
root = Tk()
myLabel1 = Label(root, text="sää salossa nyt")
myLabel3 = Label(root, text=sää.text)
myLabel4 = Label(root, text=sää1.text)
myLabel1.grid(row=0, column=2)

myLabel3.grid(row=2, column=1)
myLabel4.grid(row=3, column=1)

selain.close()
root.mainloop()

But You should use threading to update the Tkinter form, to keep update and refresh data. like:

# run the while loop in a thread
Thread(target=refresh_data, daemon=True).start()

Which refresh_data() is a function

Sign up to request clarification or add additional context in comments.

Comments

0

You should move the tkinter code out of the while loop and run the while loop in a thread:

from selenium import webdriver
#from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
import time
from tkinter import *
from threading import Thread

root = Tk()

# labels for the result
myLabel1 = Label(root, text="sää salossa nyt")
myLabel3 = Label(root, justify='left')
myLabel4 = Label(root, justify='left')

myLabel1.grid(row=0, column=0)
myLabel3.grid(row=2, column=1, sticky='w')
myLabel4.grid(row=3, column=1, sticky='w')

def get_data():
    # using headless mode
    options = Options()
    options.add_argument("--headless")
    selain = webdriver.Chrome(options=options)
    while True:
        selain.get("https://m.kellonaika.fi")
        #kello = selain.find_element_by_id("clock-time")
        selain.get("https://www.foreca.fi/Finland/Salo")
        sää = selain.find_element_by_class_name("l")
        sää1 = selain.find_element_by_class_name("r")
        # show the result
        myLabel3.config(text=sää.text)
        myLabel4.config(text=sää1.text)
        print(sää.text)
        print(sää1.text)
        time.sleep(10)
    selain.close()

# run the while loop in a thread
Thread(target=get_data, daemon=True).start()

root.mainloop() 

2 Comments

What does daemon=True mean/does?
@CoolCloud See official document.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.