Here is the question: How can I define an event that is occurring whenever a widget's function is not being called?
Or, wow can I periodically update an stringVar in tkinter(Example, updating time on a clock)? Particularly, the variable is changing based off of data scraped from the web, some applications I'm looking at are; stock tickers, weather indicators, home security systems, and monitoring sensors that report via web.
So far, the only thing I can think of is to create a function who's last call triggers an event that calls the function again. In this case, I couldn't find a standard event that looked suitable, so I would have to define one, but I'm not too familiar with doing this yet, and furthermore, I would like to avoid this if there is a simpler way.
Sources I've been using to research this project, www.automatetheboringstuff.com ... thank you Al Sweigart, this has been an excellent resource. "Tkinter GUI Application Development Blueprints" by Bhaskar Chaudhary "Tkinter GUI Development Hotshot: Develop Exciting and Engaging GUI Applications in Python and Tkinter by Working on 10 Real-world Applications" by Chaudhary, Bhaskar
Here is the code I've come up with so far.
from tkinter import *
import requests
import bs4
root = Tk()
svar1 = StringVar() #string variable to display data in the Entry Widget.
button1 = Button(root, text="What time is it?") #Button to be clicked to call a function, I don't want a button in the final product.
label1 = Label(root, text="Time From Google") #this snippet pulls the current time from google.
entry2 = Entry(root, textvariable = svar2)
def gettimefromGoogle():
site1 = requests.get("the url for a google search of 'what time is it right now?'")
if not site1.status_code ==200: # make sure site loaded, if not, did you replace the code in the previous line?
print('Time to play dino game!! ;)')
return
site1soup = bs4.BeautifulSoup(site1.text)
elems = site1soup.select('div')
time = elems[29].getText() #when I created the program, element 29 seemed to have the right data
time = time.replace(" ('your time zone') Time in 'your city', 'your state'",'') #for code to work, you'll have to replace the '' with your own info.
svar1.set(time)
site1= 1 #reassign the namespace, just to save space since Beautiful Soup objects can be quite large.
entry1 = Entry(root, textvariable = svar1, command=gettimefromGoogle)
button1.bind("<Button-1>",gettimefromGoogle) #this is where it would be nice to have an action that calls the function at a periodic interval, say every 10 seconds or so.
button1.grid(row=3,column=2) #currently, the button displays the time when clicked.
label1.grid(row=1,column=2)
entry1.grid(row=2,column=2, columnspan=4)
root.mainloop()`