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 ?
#1
Hello,can you tell me how the statuss bar after i move with button to return it alone after 1 minute in centre.Thank you
import tkinter as tk
import tkinter.ttk as ttk

root = tk.Tk()

progressbar = ttk.Progressbar(root, length=200, maximum=10, value=5)
progressbar.grid(row=1)

process = tk.IntVar(value=5)
def add_water():
    if process.get() < progressbar['maximum']:
        process.set( process.get() + 1)
        progressbar['value'] = process.get()

def sub_water():
    if process.get() > 0:
        process.set( process.get() - 1)
        progressbar['value'] = process.get()

add = ttk.Button(root, text='Water +', command=add_water)
sub = ttk.Button(root, text='Water -', command=sub_water)

label = ttk.Label(root, textvariable=process)

label.grid(row=0)
add.grid(row=0, sticky='e')
sub.grid(row=0, sticky='w')

root.mainloop()
 
Reply
#2
We don't know what value is centre on your Progressbar. So generally I think something like this will work.
def return_to_centre():
    process.set(centre_value)

def add_water():
    if process.get() < progressbar['maximum']:
        process.set( process.get() + 1)
    else:
        process.after(1000*60, return_to_centre)  ## 60 seconds
Reply
#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
#4
(May-21-2019, 06:30 PM)Yoriz Wrote: 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()

you are gold men,all work,but i test with another proggresbar and can't to make,i have a mistake,can you help me? i use this button for two function,1 muve bar ,2 GPIO start water.
import tkinter as tk
from tkinter import *
import smtplib
from tkinter import Tk, Toplevel, Button
import time
import tkinter.ttk as ttk
from tkinter.ttk import Progressbar, Style, Button

master2 =Toplevel()

master2.minsize(1000,10+0+0)
master2.geometry("1250x110+0+0")
master2.configure(background="blue",)
master2.title("water")
master2.overrideredirect(True)
###########################################

s = Style(master2)
# add the label to the progressbar style
s.layout("LabeledProgressbar",
         [('LabeledProgressbar.trough',
           {'children': [('LabeledProgressbar.pbar',
                          {'side': 'bottom', 'sticky': 'ns'}),
                         ("LabeledProgressbar.label",
                          {"sticky": ""})],
           'sticky': 'wens'})])


s.configure("LabeledProgressbar",text="-water+", troughcolor ='black',background='red')
p=progressbar = ttk.Progressbar(master2, length=250, maximum=6,value=3, mode='indeterminate',style="LabeledProgressbar")
progressbar.grid(row=1)
p.place(x = 550, y = 80)
process = tk.IntVar(value=3)

########################################

def barplus_1_2():
    barplus()

def barminus_1_2():
    barminus()
 ############   

def barplus():
 if process.get() < progressbar['maximum']:
    process.set( process.get() + 1)
    progressbar['value'] = process.get()

def barminus():
 if process.get() > 0:
    process.set( process.get() - 1)
    progressbar['value'] = process.get()
    
     
    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(10000)
 
    def sub_water(self):
        progress_value = self.process.get()
        if progress_value > 0:
            self.process.set(progress_value - 1)
            self.reset_after(10000)


##############################
##########################

image15 = tk.PhotoImage(file="")
b = button = tk.Button(master2,image=image15, height=40, width=130,background="red",font="0",bd=10,fg="black",command= barminus_1_2)
b.place(x = 520, y = 0)
label15=Label(master2,text="  water-",height=1, width=15,bg="#100C19",font="Times 13",fg="white",relief="solid",bd=0).place(x=535,y=60)
#################################


image16 = tk.PhotoImage(file="")
b = button = tk.Button(master2,image=image16, height=40, width=130,background="red",font="0",bd=10,fg="black",command= barplus_1_2)
b.place(x = 690, y = 0)
label16=Label(master2,text="  water+",height=1, width=15,bg="#100C19",font="Times 13",fg="white",relief="solid",bd=0).place(x=695,y=60)
###################################
Reply
#5
You cant just paste my class methods into your function code, your code either needs turning into classes or my code needs re-organising into your functions
Reply
#6
(May-27-2019, 10:45 AM)Yoriz Wrote: You cant just paste my class methods into your function code, your code either needs turning into classes or my code needs re-organising into your functions

two days i can't to make this,can you help me?
Reply
#7
Added the code to reset the status bar to the original function code post
import tkinter as tk
import tkinter.ttk as ttk

root = tk.Tk()

process = tk.IntVar(value=5)
root.after_id = None
progressbar = ttk.Progressbar(root, length=200, maximum=10, variable=process)
progressbar.grid(row=1)


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


def reset_after(delay_ms):
    if root.after_id:
        root.after_cancel(root.after_id)

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


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


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


add = ttk.Button(root, text="Water +", command=add_water)
sub = ttk.Button(root, text="Water -", command=sub_water)

label = ttk.Label(root, textvariable=process)

label.grid(row=0)
add.grid(row=0, sticky="e")
sub.grid(row=0, sticky="w")

root.mainloop()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Clicking on the button crashes the TK window ODOshmockenberg 1 2,196 Mar-10-2022, 05:18 PM
Last Post: deanhystad
  adding button status updates to countdown counter knoxvilles_joker 7 3,339 Apr-18-2021, 01:59 AM
Last Post: knoxvilles_joker
  [Tkinter] showing return from button on main screen blazejwiecha 4 2,594 Nov-22-2020, 04:33 PM
Last Post: blazejwiecha
  Need tkinter help with clicking buttons pythonprogrammer 2 2,398 Jan-03-2020, 04:43 AM
Last Post: joe_momma
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 4,947 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,541 Apr-18-2019, 12:48 PM
Last Post: ShashankDS
  [Tkinter] Adding New TAB to NoteBook Widget by Clicking Vicolas 0 2,582 Feb-15-2019, 06:03 PM
Last Post: Vicolas
  [Tkinter] Clicking a RadioButton in a for Loop & Getting the Appropriate Return Vicolas 1 5,097 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,773 Feb-01-2019, 11:00 AM
Last Post: Vicolas
  [PyQt] No reaction and no error message when clicking button Atalanttore 4 4,747 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