Home > front end >  Call a fonction from message box Tkinter
Call a fonction from message box Tkinter

Time:09-02

I want to call the fonction (submit) after clicking on the message box. With this code I have to reclick on button everytime after the messagebox but would appreciate if the timer launch automatically after clicking on the message box.

If anyone have a clue I will appreciate.

The following code :

import time
from tkinter import *



root = Tk()
root.resizable(width=False, height=False)

root.geometry("300x250")
root['background']='#39E5F9'

root.title("Time To Drink Water")

minute = StringVar()
second = StringVar()
minute.set("45")
second.set("00")

minuteEntry = Entry(root, width=3, font=("Arial", 35, ""),
                textvariable=minute,justify='center')
minuteEntry.place(x=50, y=60)
secondEntry = Entry(root, width=3, font=("Arial", 35, ""),
                textvariable=second,justify='center')
secondEntry.place(x=170, y=60)

def submit():
    # stored in here : 2700 = 45 mins
 temp = 2700
 while temp > -1:
    # divmod(firstvalue = temp//60, secondvalue = temp`)
    mins, secs = divmod(temp, 60)
    if mins > 60:
        hours, mins = divmod(mins, 60)
    minute.set("{0:2d}".format(mins))
    second.set("{0:2d}".format(secs))

    root.update()
    time.sleep(1)
    if (temp == 0):
         messagebox.showinfo("Time Countdown", "Time To Drink !")
    temp -= 1

def go():
  btn = Button(root, text='Goodbye dehydration!', bd='6',
             command=submit)
  btn.place(x=90, y=160)

go()

root.mainloop()

CodePudding user response:

messagebox is waiting for your click so you can run submit() after messagebox and it will run it after clicking in messagebox

But you shouldn't use while and sleep because it may freeze GUI (in any framework, and in any language). You can use root.after(1000, function) to run function again after 1000ms (1s) and it will work as sleep and while together.

import time
import tkinter as tk   # PEP8: `import *` is not preferred
from tkinter import messagebox

# --- functions ---  # PEP8: all functions before main code

def submit():
    update_counter(2700)
    
def update_counter(temp):    
    if temp > -1:   # `if` instead of `while` because `after` will work as loop
        # divmod(firstvalue = temp//60, secondvalue = temp`)
        mins, secs = divmod(temp, 60)
        if mins > 60:
            hours, mins = divmod(mins, 60)
        minute.set("{:02d}".format(mins))  # use `:02` to get `09` instead of ` 9` (with space)
        second.set("{:02d}".format(secs))

        temp -= 1
        root.after(1000, update_counter, temp)  # run again after 1000ms
    else:
        messagebox.showinfo("Time Countdown", "Time To Drink !")
        root.after(0, update_counter, 2700)  # run again after 0ms
        #update_counter(2700)  # run again
        
# --- main ---  # PEP8: `lower_case_names` for variables

running = False
temp = 2700
root = tk.Tk()

minute = tk.StringVar(root)
second = tk.StringVar(root)
minute.set("45")
second.set("00")

minute_entry = tk.Entry(root, width=3, textvariable=minute, font=("Arial", 35, ""), justify='center')
minute_entry.grid(row=0, column=0)

second_entry = tk.Entry(root, width=3, textvariable=second, font=("Arial", 35, ""), justify='center')
second_entry.grid(row=0, column=1)

btn = tk.Button(root, text='Goodbye dehydration!', command=submit)
btn.grid(row=1, column=0, columnspan=2)

root.mainloop()

PEP 8 -- Style Guide for Python Code


There is other problem. You can click button two times and it will run two update_counter() at the same time. It may need to disable button, or you would have to use boolean variable - ie. running = False - to control if it has to run update_counter() or not.

  • Related