我真的是python的新手,我并不想尝试做一些超级特别的事情,而且我不知道为什么我的点击计数器只计数1次点击,然后不再计数。这是我的代码:
from tkinter import *
click = 0
mult1 = 0
def mult():
global mult1
mult1 = mult1 + 1
def clicked():
global click1
global click2
click1 = click + 1
click2 = click1 + mult1
label1.configure(text=f'Total Clicks: {click2}', font=("Calibri", 26))
def mult():
global mult1
mult1 = mult1 + 1
def gui():
global label1
global gui
gui = Tk()
gui.geometry("800x500")
btn_click = Button(gui, text="Click to add Clicks!", padx=20, pady=20, font=("Times 16", 15), command=clicked).place(x=310, y=430)
label1 = Label(gui)
label1.grid(column=0, row=10)
gui.mainloop()
gui()
答案 0 :(得分:0)
首先,您只有一个按钮。 mult
从未被呼叫。因此,mult1
始终保持为0。其次,您使用3个不同的变量(click
,click1
和click2
),这些变量只需要1。
from tkinter import *
total_clicks = 0
clicks_to_add = 1
def mult():
global clicks_to_add
clicks_to_add += 1
def clicked():
global total_clicks
total_clicks += clicks_to_add
label1.configure(text=f'Total Clicks: {total_clicks}', font=("Calibri", 26))
def main():
global label1
global gui
gui = Tk()
gui.geometry("800x500")
btn_click = Button(gui, text="Click to add Clicks!", padx=20, pady=20, font=("Times 16", 15), command=clicked).place(x=310, y=430)
label1 = Label(gui)
label1.grid(column=0, row=10)
gui.mainloop()
main()