0
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 一次,然后什么也没有发生,但是我想要的是,当我按下任何按钮时,数字会不断地加上一个。

4

6 回答 6

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

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

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

于 2021-06-25T11:29:04.367 回答
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)文本。这对我有用。它应该适合你

于 2021-06-25T11:28:47.347 回答
1
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()

您应该在变量中保存新值

于 2021-06-25T11:32:22.670 回答
1

你的代码应该是这样的:- from tkinter import *

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() 作为标签将其更新为最后设置的值...

希望它有帮助......它正在工作我已经检查过......

于 2021-06-26T05:53:32.813 回答
1

如果你知道我的意思,也许你应该尝试使用 lambda 函数。这确实意味着:command = lambda: like. 我没有尝试过,但我想它应该可以工作:)

于 2021-06-26T06:37:39.013 回答
0
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()

它的工作!

于 2021-06-25T11:34:47.750 回答