0

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()`
XisUnknown
  • 125
  • 1
  • 1
  • 10
  • 1
    Please isolate the update issue and provide [mcve] for it. – Nae Mar 20 '18 at 06:32
  • How do you define _updating_ a `StringVar`? What makes you think it's _not updated_? – Nae Mar 20 '18 at 06:33
  • 1
    You can read more about time looping via the method `.after` from [this](https://stackoverflow.com/a/2401181/5722359) and [this](https://stackoverflow.com/a/19887761/5722359) examples. Use this to call your `gettimefromGoogle` function. I recommend to class this question as a duplication of existing question. – Sun Bear Mar 20 '18 at 12:05
  • @Nae, I define updating a stringVar as giving it a new value. For instance, it is 11:11PM right now, in 4 minutes, it will be 11:15PM I want the variable to change in a near real time fashion. What makes me think it is not updated is that if I run the program 'as is' the time displayed does not reflect the current time. – XisUnknown Mar 21 '18 at 04:05
  • @SunBear The .after is provided to run a function once after a certain amount of time. Granted, I could use it to enter into some sort of loop, but couldn't I eventually run into a Stackoverflow error for too many iterations? Also, as the example you provided states, this does not guarantee that the function will run at a periodic interval, but only when it has nothing else going on. Since neither of those posts offer a solution for periodicity and that's explicitly what I'm asking about, I wouldn't call this a duplicate. – XisUnknown Mar 21 '18 at 04:22
  • Maybe you can sharpen up your question so that OSer can give you better comments and answers quicker? Another [link](https://python-forum.io/Thread-Tkinter-How-to-update-the-gui-in-a-loop-without-blocking-the-gui-mainloop?pid=4723#pid4723) that maybe can help you. Not periodic. All the best. – Sun Bear Mar 21 '18 at 09:16
  • Found an answer here, https://www.daniweb.com/programming/software-development/code/216785/tkinter-digital-clock-python the functionality could be called in the tick() function, or by using a similar structure to write the desired function. – XisUnknown Mar 25 '18 at 02:34

0 Answers0