Home > Software engineering >  how do I make Python tkinter message box show a number from elsewhere in the script
how do I make Python tkinter message box show a number from elsewhere in the script

Time:10-28

Will preface this by saying i am very new to python and coding in general, I followed a tutorial on how to make a countdown timer, and have managed to code a button that measures the amount of times it has been clicked by mashing my code for the button with a post i found on a forum, the count down timer displays a "Times up" message box at the end, what i want to do is have it also display the amount of times the button is clicked in the allotted time. I've tried calling the global count from within the countdown timer and reusing the line that displays the count in the GUI but this seems to break it and doing it without it as displayed here simply shows the string as it is written, any help or guidance is appreciated

'''

import time
from tkinter import *
from tkinter import messagebox

f = ("Arial",24)

root = Tk()
root.title("Click Counter") 
root.config(bg='#345')
root.state('zoomed') #opens as maximised, negating the need for the 'Geometry' command

count = 0

def clicked(): 
    global count

    count = count   1

    myLabel.configure(text=f'Button was clicked {count} times!!!')

hour=StringVar()
minute=StringVar()
second=StringVar()

hour.set("00")
minute.set("00")
second.set("10")

hour_tf= Entry(
    root, 
    width=3, 
    font=f,
    textvariable=hour
    )

hour_tf.place(x=80,y=20)

mins_tf= Entry(
    root, 
    width=3, 
    font=f,
    textvariable=minute)

mins_tf.place(x=130,y=20)

sec_tf = Entry(
    root, 
    width=3, 
    font=f,
    textvariable=second)

sec_tf .place(x=180,y=20)

    
TheButton = Button(root, height= 30, width=100, bg="light blue", text="Click For Your Life", command=clicked) #tells the button to call the function when clicked

TheButton.pack()

myLabel = Label(root) 
myLabel.pack()  


def startCountdown():
    
    try:
        userinput = int(hour.get())*3600   int(minute.get())*60   int(second.get())
    except:
        messagebox.showwarning('', 'Invalid Input!')
    while userinput >-1:
        mins,secs = divmod(userinput,60) 

        hours=0
        if mins >60:
            
        
            hours, mins = divmod(mins, 60)
    
        hour.set("{0:2d}".format(hours))
        minute.set("{0:2d}".format(mins))
        second.set("{0:2d}".format(secs))

    
        root.update()
        time.sleep(1)

    
        if (userinput == 0):
            messagebox.showinfo("Time's Up!!", "you clicked (count) times")
        

        userinput -= 1




start_btn = Button(
    root, 
    text='START', 
    bd='5',
    command= startCountdown
    )

start_btn.place(x = 120,y = 120)



root.mainloop()
'''

CodePudding user response:

You need to use an f-String with "{}", like you did in the clicked function:

if (userinput == 0):
    messagebox.showinfo(f"Time's Up!! you clicked {count} times")

CodePudding user response:

A kind user provided the correct answer but has seen fit to delete their comment so in case anyone like me is trawling through old posts looking for help here is what worked:

To have your messagebox display the number of clicks (in this case count), you can simply use

messagebox.showinfo("Time's Up!!", "you clicked "   str(count)    " times")

instead of

messagebox.showinfo("Time's Up!!", "you clicked (count) times")

Your attempt won't work, because you included count inside the quotation marks, meaning that Python will completely ignore the fact that count is also a variable and just print it as a string. There are a lot of ways you can format strings to include variables etc. Check out here for a good tutorial/explanation.

  • Related