简体   繁体   English

如何让 Python tkinter 消息框显示脚本中其他地方的数字

[英]how do I make Python tkinter message box show a number from elsewhere in the script

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.首先说我对python和编码很陌生,我遵循了一个关于如何制作倒数计时器的教程,并设法编写了一个按钮,通过混合我的代码来测量它被点击的次数我在论坛上找到的带有帖子的按钮,倒数计时器在最后显示一个“超时”消息框,我想要做的是让它也显示在分配的时间内单击按钮的次数。 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我已经尝试从倒数计时器中调用全局计数并重用在 GUI 中显示计数的行,但这似乎破坏了它并且在没有它的情况下进行,如此处显示只显示写入的字符串,任何帮助或感谢指导

''' '''

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

You need to use an f-String with "{}", like you did in the clicked function:您需要使用带有“{}”的 f-String,就像您在clicked函数中所做的那样:

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

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."一个好心的用户提供了正确的答案,但认为删除他们的评论是合适的,所以如果像我这样的人在旧帖子中寻找帮助,这里是有效的“让你的消息框显示点击次数(在这种情况下是计数),你可以简单地使用 messagebox.showinfo("Time's Up!!", "you clicked " + str(count) + " times") 而不是 messagebox.showinfo("Time's Up!!", "you clicked (count) times") ). 你的尝试是行不通的, 因为你在引号内包含了 count, 这意味着 Python 将完全忽略 count 也是一个变量的事实, 只是将它打印为字符串. 有很多方法可以格式化字符串包括变量等。在这里查看一个很好的教程/解释。” Here is what they had linked It's disappointing they deleted thier comment as it was extraordinarily well worded and helpful, so if you read this..thank you!!这是他们链接的内容 令人失望的是,他们删除了他们的评论,因为它的措辞非常好,很有帮助,所以如果你读到这个……谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何让消息框上的数据井然有序? python-tkinter - How do I make data orderly on the message box? python-tkinter 如何在 Tkinter 消息框中显示 python 脚本错误 - How to Display python script Error in Tkinter message box 如何从 Tkinter 列表框中提取变量以在脚本的其他地方使用? - How do you pull out variables from a Tkinter Listbox to use elsewhere in your script? 在Python 2.6中使用Tkinter,如何使按钮小部件在文本框中打印出选中的Checkbuttons? - Using Tkinter in Python 2.6 how do I make a button widget print out selected Checkbuttons in a text box? 如何使我的python tkinter输出显示在GUI中而不是python shell中? - How do I make my python tkinter output to show in GUI instead of python shell? Python tkinter 消息框 - Python tkinter message box 如何将 Tkinter 条目从条目框中输入到 Python 列表中 - How to Make Tkinter Entries from An Entry Box into A Python List 如何制作交互式 tkinter 文本框,然后是输入框,然后是文本框作为对输入的响应 - How do I make an interactive tkinter text box, then entry box, then text box as a response to the entry Mac OS 上的 Python TKinter 不显示消息框特定图标 - Python TKinter on Mac OS does not show message box specific icons Python 3.6使用Tkinter打开一个消息框并继续执行脚本 - Python 3.6 open a message box using Tkinter and continue with the script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM