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

This code is working without errors but when I press like or dislike button the labels change from 0 to 1 only one time then nothing happens however what I want is when ever I press any button the numbers keep on adding themselves with one.

AaYan Yasin
  • 566
  • 3
  • 15

6 Answers6

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

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

You need to update the numL and numD. Your function fetches the value of numL and numD as 0 because you never update it.

1

Use Lists to make it easier. Also, len() and more functions can be used -

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)

I used len(lists) for the text. This worked for me. And it should work for you

PCM
  • 2,881
  • 2
  • 8
  • 30
  • 3
    Why would you make `numL` and `numD` lists? Lists take up more memory than integers so there is no point. Also appending to a list takes more time than adding 1 to an integer. – TheLizzard Jun 25 '21 at 12:24
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()

You should save new values in variables

1

your code should be this :- 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()

Code explanation : whenever you want to change the text in a label... text = text will not work.... you need textvariable = varname for this purpose and it must be a supported vaiable type in tkinter... here i have used StringVar as its the most convinient one..... also after changing its value in function... you need to set the textvariable again using the .set() method.... and also use .update() for the label to update it to the last set value...

hope it helps... it is working i have checked....

Prakhar Parikh
  • 177
  • 2
  • 13
1

maybe you should try to use lambda function if u know what I mean. That does mean: command = lambda: like. I didnt try it but i guess it should work:)

  • did you mean: `command=lambda: like()` also not necessary if no arguments are passed, so this is probably not the issue – Matiiss Jul 28 '21 at 21:09
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()

Its working !

AaYan Yasin
  • 566
  • 3
  • 15
  • While this works, it is better to avoid globalizing variables, the list method by PCM seems fine. Also it is better to `numD = numD + 1` above `config` and use `+=` as it is more pythonic, and check `+1` inside `config()` too – Delrius Euphoria Jun 25 '21 at 11:35
  • 1
    @CoolCloud The list variables used by PCM are also global variables, so your comment *"avoid globalizing variables"* applies to it as well. – acw1668 Jun 25 '21 at 12:00
  • @acw1668 Ah yea, I did not think of that. Then better to pass parameters – Delrius Euphoria Jun 25 '21 at 12:06