Bottom Page

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()
 
Quote
#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
Quote
#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()
Quote
#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)
###################################
Quote
#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
Quote
#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?
Quote
#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()
chano likes this post
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 354 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  tkinter- adding a new window after clicking a button built on the gui ShashankDS 2 680 Apr-18-2019, 12:48 PM
Last Post: ShashankDS
  [Tkinter] Clicking a RadioButton in a for Loop & Getting the Appropriate Return Vicolas 1 651 Feb-02-2019, 01:53 AM
Last Post: woooee
  [Tkinter] How to get & delete details from each input by clicking a button Vicolas 6 561 Feb-01-2019, 11:00 AM
Last Post: Vicolas
  [PyQt] No reaction and no error message when clicking button Atalanttore 4 742 Nov-23-2018, 01:48 PM
Last Post: Atalanttore
  Unable to return value from callback function of a button in Python Tkinter nilaybnrj 4 6,556 Aug-05-2018, 11:01 PM
Last Post: woooee
  [Tkinter] Close Toplevel after clicking button jollydragon 3 3,054 Jul-12-2018, 06:22 AM
Last Post: jollydragon
  tkinter button acts normal ONLY after clicking off the window justin_m 8 1,383 Apr-24-2018, 06:14 AM
Last Post: Larz60+
  [Tkinter] tkinter freezes by clicking button Zatox11 33 6,782 Apr-10-2018, 09:03 AM
Last Post: Zatox11
  [Tkinter] Selected radio button in push button in Tkinter prashantfunde91 1 6,397 Jun-22-2017, 05:27 PM
Last Post: DeaD_EyE

Forum Jump:


Users browsing this thread: 1 Guest(s)