Zitomak
Zitomak

Reputation: 11

Change label text tkinter

I got this code from usingpython.com which is a "type the colour not the word" game.

I am using this code to build an improved version of this game and something is wrong and I can't figure why.

So, I want to change the label where the words are (named "label"), to something like "Game Over! Your score is bla bla bla" when the countdown hits 0. So, i did this (what I added is just the 2 last lines):

def nextColour():

#use the globally declared 'score' and 'play' variables above.
global score
global timeleft

#if a game is currently in play...
if timeleft > 0:

    #...make the text entry box active.
    e.focus_set()

    #if the colour typed is equal to the colour of the text...
    if e.get().lower() == colours[1].lower():
        #...add one to the score.
        score += 1

    #clear the text entry box.
    e.delete(0, tkinter.END)
    #shuffle the list of colours.
    random.shuffle(colours)
    #change the colour to type, by changing the text _and_ the colour to a random colour value
    label.config(fg=str(colours[1]), text=str(colours[0]))
    #update the score.
    scoreLabel.config(text="Score: " + str(score))

elif timeleft == 0:
    ĺabel.config(text="Game Over! Your score is: " + score)

This is not working. When the countdown hits 0 the game just does nothing and stops.

I was thinking if I can do this with a while loop...

Upvotes: 1

Views: 15129

Answers (2)

Hietsh Kumar
Hietsh Kumar

Reputation: 1329

initial label-

    lbl_selection_value1=Label(root, text="Search Option 1")
    lbl_selection_value1.grid(row=0,column=0,padx=1)

updated label-

    lbl_selection_value1.destroy()
    lbl_selection_value1_updated = Label(root, text='New Text')
    lbl_selection_value1_updated.grid(row=0, column=0, padx=1)

Upvotes: -1

Jamie Phan
Jamie Phan

Reputation: 1132

Updating a widgets value

See this answer for more details.

You can change the text value of a Label widget 'dynamically' using its textvariable option with a StringVar object, or with the .configure() method of the Label object. As mentioned in the answer above, the .configure() method has the benefit of one less object to track

With textvariable and StringVar:

# Use tkinter for Python 3.x
import Tkinter as tk
from Tkinter import Label

root = tk.Tk()

# ...
my_string_var = tk.StringVar(value="Default Value")

my_label = Label(root, textvariable=my_string_var)
my_label.pack()

#Now to update the Label text, simply `.set()` the `StringVar`
my_string_var.set("New text value")

With .configure()

# ...

my_label = Label(root, text="Default string")
my_label.pack()

#NB: .config() can also be used
my_label.configure(text="New String")

See effbot.org for more details.

Debugging Checks

Without looking at all your code, I would also recommend checking various other issues listed below for possible cause. To extend on your comments (on this post), there may be various reasons why the program doesn't 'work' as expected:

  • The program never enters the final if block (if timeleft == 0) and so the .config method does not get the chance to update the variable
  • The global variable timeleft does reach 0, but after that iteration, it increments above 0 and re-enters the first if block (if timeleft>0), overriding the .config() you desire.
  • Another part of the code may be calling a .config() on your widget and overriding your desired changes

Planning your GUI

To prevent these things from happening, I highly recommend taking a step back, getting some pen and paper and thinking about the overall design of your application. Specifically ask yourself:

  • How can the user interact with this widget? What actions/events will cause changes to this widget?
  • Think of all the combinations of these events and ask yourself if these events conflict with one-another.

Also consider drawing a flow-chart for the application, from when the user launches the application to the possible paths they can take before closing, making sure blocks in the flow do not contradict each other.

Finally, also have a look into the Model-View-Controller architecture (and its variants) for good application design

Upvotes: 5

Related Questions