我如何通过在 Python tkinter 中多次按下按钮来重复一个功能?

How can i make one function repeat by pressing button several times in Python tkinter?

from tkinter import *

root = Tk()

numL = 0
numD = 0

root.geometry("200x100")

def like():
    root_label1.config(text=numL+1)

def dlike():
    root_label2.config(text=numD+1)

root_button1 = Button(root, text="Like", command=like)
root_button2 = Button(root, text="Dislike", command=dlike)

root_label1 = Label(root, text=numL)
root_label2 = Label(root, text=numD)

root_button1.grid(row=0,column=0)
root_button2.grid(row=0,column=1)

root_label1.grid(row=1,column=0)
root_label2.grid(row=1,column=1)

root.mainloop()

这段代码没有错误,但是当我按下喜欢或不喜欢按钮时,标签只从 0 变为 1 一次然后没有任何反应但是我想要的是每当我按下任何按钮时数字都会不断添加自己一.

使用列表让事情变得更容易。还有,len()还有更多的功能可以使用-

from tkinter import *

root = Tk()

numL = []
numD = []

root.geometry("200x100")

def like():
       numL.append(0)
       root_label1.config(text=len(numL))

def dlike():
    numD.append(0)
    root_label2.config(text=len(numD))

root_button1 = Button(root, text="Like", command=like)
root_button2 = Button(root, text="Dislike", command=dlike)

root_label1 = Label(root, text=len(numL))
root_label2 = Label(root, text=len(numD))

root_button1.grid(row=0,column=0)
root_button2.grid(row=0,column=1)

root_label1.grid(row=1,column=0)
root_label2.grid(row=1,column=1)

我用 len(lists) 作为文本。这对我有用。它应该适合你

def like():
    global numL
    numL+=1
    root_label1.config(text=numL)
    

def dlike():
    global numD
    numD+=1
    root_label2.config(text=numD)
    

您需要更新 numLnumD。您的函数将 numLnumD 的值取为 0,因为您从不更新它。

from tkinter import *

root = Tk()

global numL
numL = 0
numD = 0

root.geometry("200x100")

def like():
    numL = int(str(root_label1['text']))
    numL = numL+1
    root_label1.config(text=numL)

def dlike():
    numD = int(str(root_label2['text']))
    numD = numD + 1
    root_label2.config(text=numD)

root_button1 = Button(root, text="Like", command= like)
root_button2 = Button(root, text="Dislike", command=dlike)

root_label1 = Label(root, text=numL)
root_label2 = Label(root, text=numD)

root_button1.grid(row=0,column=0)
root_button2.grid(row=0,column=1)

root_label1.grid(row=1,column=0)
root_label2.grid(row=1,column=1)

root.mainloop()

您应该在变量中保存新值

from tkinter import *

root = Tk()

numL = 0
numD = 0

root.geometry("200x100")

def like():
    global numL
    root_label1.config(text=numL+1)
    numL = numL + 1

def dlike():
    global numD
    root_label2.config(text=numD+1)
    numD = numD + 1

root_button1 = Button(root, text="Like", command=like)
root_button2 = Button(root, text="Dislike", command=dlike)

root_label1 = Label(root, text=numL)
root_label2 = Label(root, text=numD)

root_button1.grid(row=0,column=0)
root_button2.grid(row=0,column=1)

root_label1.grid(row=1,column=0)
root_label2.grid(row=1,column=1)

root.mainloop()

正在运行!

你的代码应该是这样的:- 从 tkinter 导入 *

root = Tk()
numLvar = 0
numL = StringVar()
numL.set(numLvar)
numDvar = 0
numD = StringVar()
numD.set(numDvar)

root.geometry("200x100")

def like():
    global numLvar
    # root_label1.config(text=numL+1)
    numLvar += 1
    numL.set(numLvar)
    root_label1.update()


def dlike():
    global numDvar
    # root_label1.config(text=numL+1)
    numDvar += 1
    numD.set(numDvar)
    root_label2.update()

root_button1 = Button(root, text="Like", command=like)
root_button2 = Button(root, text="Dislike", command=dlike)

root_label1 = Label(root, textvariable=numL)
root_label2 = Label(root, textvariable=numD)

root_button1.grid(row=0,column=0)
root_button2.grid(row=0,column=1)

root_label1.grid(row=1,column=0)
root_label2.grid(row=1,column=1)

root.mainloop()

代码解释: 每当您想更改标签中的文本时...text = text 将不起作用...。为此您需要 textvariable = varname 并且它必须是 tkinter 中受支持的可变类型...在这里我使用 StringVar 作为它是最方便的一个......也在改变它的函数值之后......你需要使用.set()方法再次设置textvariable......并且还使用.update()来更新标签它到最后一个设定值...

希望对您有所帮助...我已经检查过了...

如果您明白我的意思,也许您应该尝试使用 lambda 函数。这确实意味着:command = lambda: like。我没试过,但我想它应该有用:)