简体   繁体   English

如何在 tkinter 中制作一个按钮来计算它被按下的次数?

[英]How do I make a button in tkinter that allows me to count the number of times it has been pressed?

I've been trying to create a button in tkinter that tracks the amount of times hat it has been clicked but it shows me a syntax error every time I click it.我一直在尝试在 tkinter 中创建一个按钮来跟踪它被点击的次数,但每次我点击它都会显示一个语法错误。

The way I intend it to work is for it to add one to the counter every time the top button is clicked, and then upon clicking the bottom one, the programme will close and the amount of times the top button has been clicked will be printed.我打算它的工作方式是每次单击顶部按钮时向计数器添加一个,然后单击底部按钮时,程序将关闭并打印顶部按钮被单击的次数.

from tkinter import *

count = 0
def counter():
    count = count + 1
def total():
    print(count)
    exit ()

window = Tk()
window.geometry("175x105")
window.resizable(0, 0)

text=Label(window, text="Click the button.", font=("Times New Roman", 10))
text.pack(padx=3, pady=3)

button=Button(window, text="CLICK HERE", width=12, bg="gray", command=counter)
button.pack(padx=2, pady=2)
button2=Button(window, text="FINISH", width=12, bg="gray", command=total)
button2.pack(padx=2, pady=2)

window.mainloop()

You only have to globalize the variable count inside the counter() function.您只需要将counter()函数中的变量count全球化。 It will update the value of count every time the function is called.每次调用该函数时,它都会更新 count 的值。

Like this:像这样:

import tkinter as tk

count = 0


def counter():
    global count
    count = count + 1   # you can also use: count += 1


def total():
    print(count)
    exit()


window = tk.Tk()
window.geometry("175x105")
window.resizable(0, 0)

text = tk.Label(window, text="Click the button.", font=("Times New Roman", 10))
text.pack(padx=3, pady=3)

button = tk.Button(window, text="CLICK HERE", width=12, bg="gray", command=counter)
button.pack(padx=2, pady=2)
button2 = tk.Button(window, text="FINISH", width=12, bg="gray", command=total)
button2.pack(padx=2, pady=2)

window.mainloop()

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

相关问题 如何制作一个按钮,允许我将两个变量发送到 Tkinter 中的同一个函数中? - How do I make a button that allows me to send two variables into the same function in Tkinter? 计算按下tkinter按钮的次数 - Count how many times tkinter button is pressed python) 当按下按钮时,如何使 tkinter 出现? - python) How do I make tkinter appear when a button is pressed? 选择并按下按钮后,如何从列表框中删除项目 - How do i remove a item from a Listbox when it has been chosen and a button has been pressed 如何为在 tkinter 中按下创建的按钮的次数创建计数器? - How to create a counter for the number of times a button that was created was pressed in tkinter? 如何检查是否已用 Tkinter 按下键? - How to check if key has been pressed with Tkinter? 如果 Tkinter 小部件中的条目已更正,如何使 tkinter 消息消失? - How do I make a tkinter message disappear if the entry in Tkinter Widget has been corrected? Tkinter:如何使按钮为变量分配数字 - Tkinter: How do I make a button assign a variable a number 如何计算按钮被点击的次数Python(tkinter) - How to count the number of times a button is clicked Python(tkinter) 每次在 tkinter 中按下按钮时,如何创建一个将列增加 1 的函数? - How do I make a function that increases the column by 1 every time a button is pressed in tkinter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM