I have the following oops code, I am also monitoring user activity to see if it is idle for more than 5 seconds. I am very new to oops, so just want to understand if there are better way to implement it.


import Tkinter
import time

class simpleapp_tk(Tkinter.Tk):
    def __init__(self,parent):
        self.parent = parent
        self.running = None

    def initialize(self):

        self.entryVariable = Tkinter.StringVar()
        self.entry = Tkinter.Entry(self,textvariable=self.entryVariable)
        self.entry.bind("<Return>", self.OnPressEnter)
        self.entryVariable.set(u"Enter text here.")

        button = Tkinter.Button(self,text=u"Click me !",

        self.labelVariable = Tkinter.StringVar()
        label = Tkinter.Label(self,textvariable=self.labelVariable,
        self.labelVariable.set(u"Hello !")

        self.entry.selection_range(0, Tkinter.END)
        self.after(1000, self.tick)

    def OnButtonClick(self):
        self.labelVariable.set( self.entryVariable.get()+" (You clicked the button)" )
        self.entry.selection_range(0, Tkinter.END)

    def OnPressEnter(self,event):
        self.labelVariable.set( self.entryVariable.get()+" (You pressed ENTER)" )
        self.entry.selection_range(0, Tkinter.END)

    def reset(self, *ignore): 
        self.running = None

    def tick(self, *ignore):
        if not self.running:
            self.running = time.time()
        elif time.time() - self.running > 5:
            print 'I waited 5 seconds...'
            self.running = None

    def eventbind(self):

if __name__ == "__main__":
    app = simpleapp_tk(None)
    app.title('my application')
  • \$\begingroup\$ What is oops? Oops! Did you mean OOP? See also programmers.stackexchange.com/questions/92174/… \$\endgroup\$ – Roman Susi Sep 22 '13 at 5:06
  • \$\begingroup\$ @RomanSusi I mean object oriented programming... I just want to know is there a any more better way to write above code \$\endgroup\$ – erkav Sep 23 '13 at 4:45

Tkinter is not just OOP, it's about http://en.wikipedia.org/wiki/Event-driven_programming . The code you posted does not yet show enough to be judged, because it's too small and the application purpose is not yet clearly visible. When code will grow, you will need to decompose it into modules, but it is good to think about the strategy before hand.

Tkinter allows to process events in event-driven style, so it's hard to see why do you need "a tick" to check if a user presses something. (I am not sure, may be it is intended this way).

One more note I can make is about style. Take alook at http://www.python.org/dev/peps/pep-0008/ It's not very important, but makes the code look much nicer.

UPDATE: MVC (model-view-controller) is very natural approach for Tkinter (as is the case with other GUI frameworks). Please, take a look at toy MVC (link from an answer to https://stackoverflow.com/questions/7638139/python-tk-with-mvc-pattern)

  • \$\begingroup\$ The reason for using tick is, I am writing client - server code and after some interval I want to check that if the connection is still alive..Tkinter waits for events on mainloop, so I couldn't find any other better way to do it.. or another one is , I want to disconnect if the client connection is idle for more than 5 minutes \$\endgroup\$ – erkav Sep 23 '13 at 9:36
  • \$\begingroup\$ Ok. Network connections may require their own threads and queues. Maybe this recipe can help you: code.activestate.com/recipes/82965 (Hinted by Alex Martelli here: stackoverflow.com/questions/1988286/…) \$\endgroup\$ – Roman Susi Sep 23 '13 at 10:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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