AaYan Yasin
AaYan Yasin

Reputation: 490

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

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

Answers (6)

Dan Šafránek
Dan Šafránek

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

Prakhar Parikh
Prakhar Parikh

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

Shamim Sanisales
Shamim Sanisales

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

user15801675
user15801675

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

AaYan Yasin
AaYan Yasin

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

PCM
PCM

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

Related Questions