Skip to content Skip to sidebar Skip to footer

How To Update A Label In Python?

I'm new to python, and I was creating a small game when I got a problem. I searched for an answer, found one, yet it didn't work. The game I'm trying to make is more or less a rec

Solution 1:

I found this post :

Update Tkinter Label from variable

A bit modified this means :

from tkinter import *

master = Tk()

defuiPrint():
    print("")
    print(clickcount)
    blankLine()

clickcount=0
click=StringVar() #Updates label if changed
click.set("0");
mult = 1
dcp1 = 0defblankLine():
    for i inrange(20):
        print("") 

defbuttonCommand():
    global clickcount
    global click
    global mult
    clickcount += 1*(mult)
    click.set(str(clickcount)); #Update score
    uiPrint()

mainClickButton = Button(master, text="Click me!", command = buttonCommand)
mainClickButton.pack()

master.title("Cookie Clicker")
photo = PhotoImage(file=r"C:\Users\---\Desktop\Cookie.gif")
label = Label(image=photo)
label.image = photo
label.pack()

l = Label(master, textvariable = click) #Score
l.pack()

mainloop();

I would additonaly suggest to make the cookie image as a button :

from tkinter import *

master = Tk()

defuiPrint():
    print("")
    print(clickcount)
    blankLine()

clickcount=0
click=StringVar() #Updates label if changed
click.set("0");
mult = 1
dcp1 = 0defblankLine():
    for i inrange(20):
        print("") 

defbuttonCommand():
    global clickcount
    global click
    global mult
    clickcount += 1*(mult)
    click.set(str(clickcount)); #Update score
    uiPrint()

photo = PhotoImage(file=r"C:\Users\---\Desktop\Cookie.gif")
mainClickButton = Button(master, image=photo, command = buttonCommand)
mainClickButton.pack()

master.title("Cookie Clicker")

l = Label(master, textvariable = click) #Score
l.pack()

mainloop();

Both tested with python 2.7.11 and python 3.5.2, they work fine

Post a Comment for "How To Update A Label In Python?"