开发者

Prevent method calls for n seconds, show message of the remaining time

I'm working on simple GUI but I'm stuck. This is the basic flow:

  1. Show text text and:
    • save time in time_pressed
    • start the progressbar and update it until time_notallow expires.
  2. If a user presses <Next> see if seconds specified in time_notallow have passed, and if not, don't allow the display of the next text.

Basically, I want to prevent users form calling a method bind to the <Right> key until time_notallow passes, and show a progressbar to inform them how long they'll have to wait. Since I use bind, such as...

    self.master.bind('<Right>', self.text_next)

...I don't have .after(), as in widgets.

What I've tried

  1. master.after() to set bind to None and after time_notallow to bind to self.text_next, but it didn't work.
  2. Created a thread which looped with while True to check constantly if time_notallow is passed or not, but the application crashes.

Any help appreciated.

Edit: A Solution. Using lambda() in .after to count seconds (thanks to Bryan Oakley)

"""

Stripped-down version to figue out time/event/threading stuff.

What this has to do:

1. Show the window and some text.
2. User presses Next arrow and new text shows. Paint the label red.
3. Prevent user form pressing again (unbind all keys), until 2 seconds passed 
   (time_wait).
4. Make notice of passed time and after 2 seconds bind the keys again and
   paint the label green.
5. Loop the steps 2-4.

"""

import sys
import tkinter as tk
from tkinter import W, E, S, N

class Test(tk.Frame):

    def __init__(self, master=None):
        """Draw the GUI"""
        tk.Frame.__init__(self, master)
        self.draw_widgets()
        self.grid()
        self.time_wait = 2
        self.locked = False
        self.bind_keys()
        self.counter = 0

    def draw_widgets(self):
        """Draw all the widgets on the frame."""
        text = 'Just a sample sentence.'
        #Label with the sentence
        self.lbl_text = tk.Label(self, anchor="center", relief='groove')
        self.lbl_text['text'] = text
        self.lbl_text['font'] = ('Helvetica', 27)
        self.lbl_text.grid(column=0, row=0, sticky=W+E+S+N)
        self.lbl_note = tk.Label(self, anchor="center", relief='groove',
            bg='green')
        self.lbl_note.grid(column=0, row=1, sticky=W+E+S+N)

    def text_next(self, event):
        """Get next text"""
        if not self.locked:
            self.counter += 1
            self.lbl_text['text'] = 'The text number %s!' % self.counter
            self.bind_tonone()
            self.lock(self.time_wait)

    def text_previous(self, event):
        """Get previous text"""
        if not self.locked:
            self.counter -= 1
            self.lbl_text['text'] = 'The text number %s!' % self.counter
            self.bind_tonone()
            self.lock(self.time_wait)

    def bind_keys(self):
        """Bind the keys"""
        self.master.bind('<Left>', self.text_previous)
        self.master.bind('<Right>', self.text_next)
        self.master.bind('<Escape>', self.exit)
        self.lbl_note['bg'] = 'green'

    def bind_tonone(self):
        """Unbind the keys"""
        self.master.bind('<Left>', None)
        self.master.bind('<Right>', None)
        self.master.bind('<Escape>', None)
        self.lbl_note['bg'] = 'red'

    def lock(self, n):
        if n == 0:
        开发者_JAVA技巧    self.locked = False
            self.lbl_note['text'] = ''
            self.lbl_note['bg'] = 'green'

        else:
            self.locked = True
            self.lbl_note['text'] = 'Locked for %s more seconds' % n
            self.lbl_note.after(1000, lambda n = n - 1: self.lock(n))

    def exit(self, event):
        """Exit the program."""
        sys.exit()

def start():
    """Start the gui part."""
    root = tk.Tk()
    app = Test(master=root)
    app.mainloop()

if __name__ == '__main__':
    start()


You don't need threads or timers to solve this problem. All you need is a procedure that takes a number of seconds to wait, and have it call itself once a second until the number gets down to zero.

It would look something like this (off the top of my head, untested):

def __init__(...):
    ...
    self.locked = False
    ...

def text_next(self, event):
    if not self.locked:
        <do the "next" logic>
        self.lock(10) # lock for 10 seconds

def text_previous(self, event):
    if not self.locked:
        <do the "previous" logic>
        self.lock(10) # lock for 10 seconds

def lock(self, n):
    if n == 0:
        self.locked = False
        self.status.config(text="")
    else:
        self.locked = True
        self.status.config(text="Locked for %s more seconds" % n)
        self.status.after(1000, lambda n=n-1: self.lock(n))


How about instead of a spin-waiting thread, you try the specifically-designed threading.Timer object?


A quick fix using your timer is to refactor a bit. Set your self.track initially to None, then only set it to run/block on arrow.

import sys
import threading
import Tkinter as tk
from Tkinter import W, E, S, N

class Test(tk.Frame):

    def __init__(self, master=None):
        """Draw the GUI"""
        tk.Frame.__init__(self, master)
        self.draw_widgets()
        self.grid()
        # Track wait times
        self.time_wait = 2
        # Timer
        self.track = None
        self.bind_keys()
        self.counter = 0

    def draw_widgets(self):
        """Draw all the widgets on the frame."""
        text = 'Just a sample sentence.'
        #Label with the sentence
        self.lbl_text = tk.Label(self, anchor="center", relief='groove')
        self.lbl_text['text'] = text
        self.lbl_text['font'] = ('Helvetica', 27)
        self.lbl_text.grid(column=0, row=0, sticky=W+E+S+N)
        self.lbl_note = tk.Label(self, anchor="center", relief='groove',
            bg='green')
        self.lbl_note.grid(column=0, row=1, sticky=W+E+S+N)

    def text_next(self, event):
        """Get next text"""
        if not self.track or not self.track.is_alive():
            self.track = threading.Timer(self.time_wait, self.bind_keys)
            self.counter += 1
            self.lbl_text['text'] = 'The text number %s!' % self.counter
            self.bind_tonone()
            self.track.start()

    def text_previous(self, event):
        """Get previous text"""
        self.counter -= 1
        self.lbl_text['text'] = 'The text number %s!' % self.counter

    def bind_keys(self):
        """Bind the keys"""
        self.master.bind('<Left>', self.text_previous)
        self.master.bind('<Right>', self.text_next)
        self.master.bind('<Escape>', self.exit)
        self.lbl_note['bg'] = 'green'

    def bind_tonone(self):
        """Unbind the keys"""
        self.master.bind('<Left>', None)
        self.master.bind('<Right>', None)
        self.master.bind('<Escape>', None)
        self.lbl_note['bg'] = 'red'

    def exit(self, event):
        """Exit the program."""
        sys.exit()

def start():
    """Start the gui part."""
    root = tk.Tk()
    app = Test(master=root)
    app.mainloop()

if __name__ == '__main__':
    start()

On preview though, @Bryan Oakley, has a better solution.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜