0

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 recreation of Cookie Clicker, and I'm trying to make the label which I have the score on update, instead, it creates a new label.

from tkinter import *
import time

master = Tk()

def uiPrint():
print("")
print(click)
blankLine()

click = 0
mult = 1
dcp1 = 0

def blankLine():
    for i in range(20):
        print("") 

def buttonCommand():
    global click
    global mult
    click += 1*(mult)
    uiPrint()
scoreCommand = Button(text=click)
scoreCommand.pack()

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()
mainloop()
Odomontois
  • 15,918
  • 2
  • 36
  • 71
Jake
  • 772
  • 2
  • 8
  • 18
  • What does "didn't work" mean? Did you get an error? If so, it is likely telling exactly why it didn't work. Also, the indentation of the code in the question looks incorrect in some places. – Bryan Oakley Jan 13 '17 at 20:17
  • 1
    I apologise for my bad skills on programming and identifying the problem, but there is no error message, although it does not do what I want it to do. I made a video of what it does: https://gyazo.com/378a81554232f9421efb62b81f62f272 yet I only want it to update on one label, without creating numerous other ones. – Jake Jan 13 '17 at 20:25
  • Sorry could you edit the code part of the question a bit, right now it seems that the uiPrint function is not doing anything. You are also using click before it has been defined. – Imran Ali Jan 13 '17 at 20:46
  • `label['text'] = "Hello World"` or `label["image"] = new_image` – furas Jan 13 '17 at 21:14
  • BTW: or `label.config(text="Hello World")`. The same is with other widgets - ie. `mainClickButton["command"] = stop_clicking` or `mainClickButton.config(command=stop_clicking)` – furas Jan 13 '17 at 21:17
  • BTW: to make code more readable put all your functions before `master = Tk()` and better use `import tkinter as tk` and `tk.Tk()`, `tk.Button()` , `tk.Label()`. Later you can use other modul `ttk` and `ttk.Label()` or create own windget ie. `Label()` – furas Jan 13 '17 at 21:21

1 Answers1

0

I found this post :

Update Tkinter Label from variable

A bit modified this means :

from tkinter import *

master = Tk()

def uiPrint():
    print("")
    print(clickcount)
    blankLine()

clickcount=0
click=StringVar() #Updates label if changed
click.set("0");
mult = 1
dcp1 = 0

def blankLine():
    for i in range(20):
        print("") 

def buttonCommand():
    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()

def uiPrint():
    print("")
    print(clickcount)
    blankLine()

clickcount=0
click=StringVar() #Updates label if changed
click.set("0");
mult = 1
dcp1 = 0

def blankLine():
    for i in range(20):
        print("") 

def buttonCommand():
    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

Community
  • 1
  • 1
Luatic
  • 8,513
  • 2
  • 13
  • 34