Python Tkinter – I can't replicate terminal output in Tkinters Label (GUI)

Question:

Within my class ‘App’, I wish to take the user input in the Tkinter EntryBox, and use it to get the definition using pythons PyDictionary module. Once done, I wish to display the output in a Tkinter Label. The specific output which is printing to the python terminal, is not going to the Tkinter Label.

Questions similar to this are all over, but not for my specific problem, from what I can find.

My Code:

# Python Dictionary App

from tkinter import * # Main Tkinter module.
import customtkinter as ctk # Module for modernising Tkinter.
from pydictionary import Dictionary # The dictionary module.

ctk.set_appearance_mode("dark")
ctk.set_default_color_theme("blue")

# Main Class
class App(ctk.CTk):

    def __init__(self):
        super().__init__() # Initiates vairiables from parent class in ctk.

        # Sets the title of the interface and the size.
        self.title("Python Dictionary Application by Leon Hill")
        self.geometry("750x500")

        # Entry box for input
        self.entry_box = ctk.CTkEntry(self, width=200)
        self.entry_box.place(x=10, y=50)

        #Label above the entry
        self.entry_label = ctk.CTkLabel(self, width=200, text="Enter a word", 
                                        anchor=W)
        self.entry_label.place(x=10, y=20)

        # Label to display output
        self.output_label = ctk.CTkLabel(self, width=730, height=390, bg_color="#D5D8DC", padx=10, pady=10, 
                                    text=" ", text_color="black", anchor=NW, justify=LEFT, wraplength=600)
        self.output_label.place(x=10, y = 100)

        # The three main buttons 
        self.definition_button = ctk.CTkButton(self, text="Definition", command=self.word_definiton).place(x=250, y=50)
        self.synonym_button = ctk.CTkButton(self, text="Synonyms").place(x=400, y=50)
        self.antonym_button = ctk.CTkButton(self, text="Antonyms").place(x=550, y=50)

    def word_definiton(self):
        dict = Dictionary(self.entry_box.get()) # Take the input from EntryBox to use with Dictionary.
        self.meaning = dict.meanings() # request the meaning of the word, rather than synonyms or antonyms.
        self.output_label.configure(text = dict.print_meanings()) # print the meaning into the Tkinter Label.


if __name__ == "__main__":
    app = App()
    app.mainloop() #Run the app

When inputting a word into the entrybox, for example the word "Hello", this appears in the terminal:

Meanings of the word 'Hello':
1. (used to express a greeting, answer a telephone, or attract attention.)
2. (an exclamation of surprise, wonder, elation, etc.)
3. (used derisively to question the comprehension, intelligence, or common sense of the person being addressed): You're gonna go out with him? Hello!
4. the call “hello” (used as an expression of greeting): She gave me a warm hello.
5. to say “hello”; to cry or shout: I helloed, but no one answered.

My goal is to project the terminal output to the Label in my Tkinter app, but nothing happens.

App Screenshot

Asked By: Leon

||

Answers:

dict.print_meanings() returns None, so the label text is set to None.

You can get a list using dict.meanings() instead of dict.print_meanings() and then format the data however you want.

Example:

def word_definiton(self):

    word = self.entry_box.get()
    dict = Dictionary(word) # Take the input from EntryBox to use with Dictionary.
    self.meaning = dict.meanings() # request the meaning of the word, rather than synonyms or antonyms.

    text = f"Meanings of the word '{word}':n"
    for i, meaning in enumerate(self.meaning):
        text += f"{i+1}. {meaning}n"
    self.output_label.configure(text=text)

screenshot


I recommend using a Text widget rather than a ctk.CTkLabel. The Text widget will allow you to add colors and other types of formatting that are not possible with a ctk.CTkLabel. Plus, a Text widget is scrollable whereas a ctk.CTkLabel is not.

Answered By: Bryan Oakley