Simd
Simd

Reputation: 21244

How to display word in a window on demand

I am new to tkinter. I have code that runs a loop and sometimes finds a word I care about. It then prints it to the screen. I would like instead to print the word to the centre of a blank window for more dramatic effect. What is the simplest way to do this? I am looking at the tutorial code:

from tkinter import *
import time

class App(Frame):
    def __init__(self,master=None):
        Frame.__init__(self, master)
        self.master = master
        self.label = Label(text="", fg="Red", font=("Helvetica", 18))
        self.label.place(x=50,y=80)
        self.update_clock()

    def update_clock(self):
        now = time.strftime("%H:%M:%S")
        self.label.configure(text=now)
        self.after(1000, self.update_clock)



root = Tk()
app=App(root)
root.wm_title("Tkinter clock")
root.geometry("200x200")
root.after(1000, app.update_clock)
root.mainloop()

This seems to be based around an event loop. I would like instead to just be able to print a new word on demand. For example if I have a function that returns a word

listofwordstodisplay = ['apple', 'banana', 'kiwi', 'pear']
def word(idx):
    return listofwordstodisplay[idx]

I would like to be able to do something like display(word(2)) if possible.

Upvotes: 0

Views: 80

Answers (1)

Ritesh Khanna
Ritesh Khanna

Reputation: 146

The simplest way is to create a label and then modify its text by using configure function.

from tkinter import *

listofwordstodisplay = ['apple', 'banana', 'kiwi', 'pear']
def word(idx):
    return listofwordstodisplay[idx]

root=Tk()
label=Label(root)
label.pack()

label.configure(text=word(2))

root.mainloop()

Upvotes: 1

Related Questions