1

I am trying to display a random phrase from a dictionary every few seconds in a tkinter window.

I can get the phrase to display by just running a variable into a text box in tkinter, but I can't seem to get that phrase to change in the desired intervals.

So far this is the code that I have.

import time
import sys
import random
import tkinter as tk
from tkinter import *



""" DICTIONARY PHRASES """
phrases = ["Phrase1", "Phrase2", "Phrase3"]

def phraserefresh():
    while True:
        phrase_print = random.choice(phrases)
        time.sleep(1)
    return phrase_print

phrase = phraserefresh()

# Root is the name of the Tkinter Window. This is important to remember.
root=tk.Tk()

# Sets background color to black
root.configure(bg="black")

# Removes the window bar at the top creating a truely fullscreen
root.wm_attributes('-fullscreen','true')
tk.Button(root, text="Quit", bg="black", fg="black", command=lambda root=root:quit(root)).pack()

e = Label(root, text=phrase, fg="white", bg="black", font=("helvetica", 28))
e.pack()


root.mainloop()

The result of running this code is that the tkinter window never opens, as opposed to changing the displayed text. I know I must be over looking something simple but I can't seem to figure out what. Thanks for you help in advance!

Megastrik3
  • 95
  • 1
  • 1
  • 9

1 Answers1

2

This function never returns due to the while True loop:

def phraserefresh():
    while True:
        phrase_print = random.choice(phrases)
        time.sleep(1)
    return phrase_print # This line is never reached

You can use the after() method to set up a repeating delay and change the label text.

def phrase_refresh():
    new_phrase = random.choice(phrases)
    e.configure(text=new_phrase) # e is your label
    root.after(1000, phrase_refresh) # Delay measured in milliseconds
Chris
  • 4,009
  • 3
  • 21
  • 52
  • Thank you for your help! Though when a run that code I either don't get the phrase at all just numbers and the function name or an error saying that e is not defined. What did I do wrong? – Megastrik3 Aug 25 '19 at 01:54
  • @Megastrik3 Try putting the function declaration after the line where you declare `e` (just before `mainloop()`) – Chris Aug 25 '19 at 07:10
  • Ok, I got it to display a phrase by moving some code around, but now instead of changing the text that is one the screen it just adds another phrase below it. Is there a way to replace the existing text with the new text? – Megastrik3 Aug 25 '19 at 11:45
  • @Megastrik3 Try this instead: `e['text'] = new_phrase` and make sure you are not instantiating a new label each time. – Chris Aug 25 '19 at 11:50
  • e['text'] = new_phrase or e.['text']? The first one draws and error. – Megastrik3 Aug 25 '19 at 12:04
  • 1
    Actually never mind. I got it! The reason the code you posted wan't working was because my e = Label... and e.pack() lines were in the wrong order. I fixed it by adding phrase_refresh() between those two lines. So it works now! Thank you so much for your time and help! – Megastrik3 Aug 25 '19 at 12:18
  • @Megastrik3 Really glad you got it working! Nice one – Chris Aug 25 '19 at 12:34
  • @Megastrik3 I'm trying to do more or less exactly what you did here (but trying to show different ukulele chords to play lol). But I can't seem to get past the "e is not defined error" despite trying to follow your conversation here with Chris. Is there any way you can share your final code as another answer? – Kristen G. Jul 18 '20 at 21:57
  • 1
    @Megastrik3 On second thought, I merely had a misspelling in the word label ("lable") which my brain just would not let me see. *facepalm*. So now it's working. Thanks a lot for your question! – Kristen G. Jul 18 '20 at 22:15
  • 1
    Yeah that would do it @Kristen_G! Glad you were able to figure it out! – Megastrik3 Jul 18 '20 at 22:51
  • @KristenG. Glad you got it sorted! – Chris Jul 19 '20 at 08:50