Python Forum
[Tkinter] RE: status bar to return to the centre after 1 minute of clicking a button ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] RE: status bar to return to the centre after 1 minute of clicking a button ?
#3
You are much better off programming GUI's with classes, easier to organise and access attributes.
Using the after method allows the calling of a function after an amount of milliseconds.
I have a forum thread a bout using after https://python-forum.io/Thread-Tkinter-H...ng-the-gui

The return of after gives an id that can be used to cancel a call,
that way if a button is pressed again before the call is made it will cancel the current one and a new after 1 minute call can be made.

See the following modified code.
import tkinter as tk
import tkinter.ttk as ttk


class MainFrame(tk.Frame):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.process = tk.IntVar(value=5)
        self.after_id = None

        self.progressbar = ttk.Progressbar(
            self.master, length=200, maximum=10, variable=self.process
        )
        self.progressbar.grid(row=1)

        self.add_button = ttk.Button(
            self.master, text="Water +", command=self.add_water
        )
        self.sub_button = ttk.Button(
            self.master, text="Water -", command=self.sub_water
        )

        self.label = ttk.Label(self.master, textvariable=self.process)

        self.label.grid(row=0)
        self.add_button.grid(row=0, sticky="e")
        self.sub_button.grid(row=0, sticky="w")

    def reset_water(self):
        self.process.set(5)
        self.after_id = None

    def reset_after(self, delay_ms):
        if self.after_id:
            self.after_cancel(self.after_id)

        self.after_id = self.after(delay_ms, self.reset_water)

    def add_water(self):
        progress_value = self.process.get()
        if progress_value < self.progressbar["maximum"]:
            self.process.set(progress_value + 1)
            self.reset_after(60000)

    def sub_water(self):
        progress_value = self.process.get()
        if progress_value > 0:
            self.process.set(progress_value - 1)
            self.reset_after(60000)


if __name__ == "__main__":
    tk_app = tk.Tk()
    main_frame = MainFrame()
    tk_app.mainloop()
Reply


Messages In This Thread
RE: RE: status bar to return alone on the centre - by Yoriz - May-21-2019, 06:30 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Clicking on the button crashes the TK window ODOshmockenberg 1 2,252 Mar-10-2022, 05:18 PM
Last Post: deanhystad
  adding button status updates to countdown counter knoxvilles_joker 7 3,422 Apr-18-2021, 01:59 AM
Last Post: knoxvilles_joker
  [Tkinter] showing return from button on main screen blazejwiecha 4 2,665 Nov-22-2020, 04:33 PM
Last Post: blazejwiecha
  Need tkinter help with clicking buttons pythonprogrammer 2 2,475 Jan-03-2020, 04:43 AM
Last Post: joe_momma
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 5,033 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  tkinter- adding a new window after clicking a button built on the gui ShashankDS 2 6,630 Apr-18-2019, 12:48 PM
Last Post: ShashankDS
  [Tkinter] Adding New TAB to NoteBook Widget by Clicking Vicolas 0 2,622 Feb-15-2019, 06:03 PM
Last Post: Vicolas
  [Tkinter] Clicking a RadioButton in a for Loop & Getting the Appropriate Return Vicolas 1 5,175 Feb-02-2019, 01:53 AM
Last Post: woooee
  [Tkinter] How to get & delete details from each input by clicking a button Vicolas 6 3,860 Feb-01-2019, 11:00 AM
Last Post: Vicolas
  [PyQt] No reaction and no error message when clicking button Atalanttore 4 4,832 Nov-23-2018, 01:48 PM
Last Post: Atalanttore

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020