Reputation: 490
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.
Upvotes: 0
Views: 759
Reputation: 39
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:)
Upvotes: 1
Reputation: 197
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....
Upvotes: 1
Reputation: 21
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
Upvotes: 1
Reputation:
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.
Upvotes: 3
Reputation: 490
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 !
Upvotes: 0
Reputation: 3011
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
Upvotes: 1