Python Forum
[Tkinter] RE: status bar to return to the centre after 1 minute of clicking a button ? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: [Tkinter] RE: status bar to return to the centre after 1 minute of clicking a button ? (/thread-18532.html)



RE: status bar to return to the centre after 1 minute of clicking a button ? - chano - May-21-2019

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()
 



RE: RE: status bar to return alone on the centre - woooee - May-21-2019

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



RE: RE: status bar to return alone on the centre - Yoriz - May-21-2019

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-How-to-deal-with-code-that-blocks-the-mainloop-freezing-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()



RE: RE: status bar to return alone on the centre - chano - May-24-2019

(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-How-to-deal-with-code-that-blocks-the-mainloop-freezing-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)
###################################



RE: RE: status bar to return to the centre after 1 minute of clicking a button ? - Yoriz - May-27-2019

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


RE: RE: status bar to return to the centre after 1 minute of clicking a button ? - chano - May-27-2019

(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?


RE: RE: status bar to return to the centre after 1 minute of clicking a button ? - Yoriz - May-27-2019

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()