-1

I have a simple script which saves some values to a database , I also have a window built in Tkinter. So basically my problem is I want the savebase() function to be continuously called upon, till the window remains open. How can this be done ?

Till now I'm able just to run the function only once, when the window opens itself. I can also put a button to repeat it , but it doesn't solve the purpose as i want this thing to be done like 2 times per second.

A simplified version of my attempt at this is like :

     import Tkinter
     import saveDB

     def doing_it():
          a = saveDB.save()
          a.savebase()

     window = Tkinter.Tk()
     window.title("Saving Database")
     window.geometry("300x300+100+100")


     first_button=Tkinter.Button(window, text='Save DB', command=doing_it,
                                fg='white', bg='black').grid(row=3,column=2)

     window.mainloop()

is if name == main() , the way to do it ? during my search for the above mentioned problem, I came across it , though i know nothing about it.

4
  • 5
  • I dont want to just delay the script , that is not the prime concern . the prime concern is to make it run repeatedly (continuously). @EricLevieil Jun 15, 2015 at 16:53
  • 1
    @ManipalKing put a call to window.after inside doing_it so it schedules itself to be called again.
    – Adam Smith
    Jun 15, 2015 at 16:55
  • 3
    You should read the actual content of the linked doc instead of just looking at the URL. Jun 15, 2015 at 16:56

2 Answers 2

2

Use the after-method:

def doing_it():
     a = saveDB.save()
     a.savebase()
     window.after(500, doing_it)

 window = Tkinter.Tk()
 window.title("Saving Database")
 window.geometry("300x300+100+100")
 window.after(500, doing_it)
 window.mainloop()
4
  • The OP wants 2 calls per second. Would be 500ms.
    – Daniel
    Jun 15, 2015 at 17:02
  • is there a way to keep count of how many times was the function called ? @Daniel Jun 15, 2015 at 17:59
  • increment a global variable.
    – Daniel
    Jun 15, 2015 at 18:06
  • I tried as u said but where should i direct this output to, at first I thought i'll assign it to a label and the label would update itself , but as u might have guessed it , it didn't. So any idea about what I'm doing wrong or should i use something other thn label for this @Daniel Jun 15, 2015 at 18:10
1

You had a follow up question in a commment, and I don't have enough rep to comment, so I'll leave this here.

You said you were assigning the text to a label, but it was not updating. I'm not 100% sure what you mean, but here's a working example of what I'm doing that should work for you as well.

#! /usr/bin/env python3.4
from tkinter import *
import time

GLOBVAR = 0
class App(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.geometry("300x300+500+200")
        self.wm_title("Test update label")
        self.label = Label(text="you  won't see this")
        self.label.pack(pady = (150, 0)) 
        self.update_clock()

    def update_clock(self):
        global GLOBVAR
        GLOBVAR += 1
        self.label.configure(text = GLOBVAR)
        self.after(1000, self.update_clock)

app=App()
app.mainloop()

In this case, the key part that updates the label widget is the self.label.configure() call.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.