Search code examples
pythontkintertkinter-entry

How to let your entry receive function you've written and print it in tkinter


I want my entry to check the length of the name I type in the entry widget and to return it in lowercase. So when you click on the print button it prints the name you entered in lowercase.

Challenge:

It doesn't print the name you entered when I click on print button but it prints the id of the journalist function.

from tkinter import messagebox
from tkinter import *


def journalist():
    name = input("please enter journalist name:")
    if len(name) > 15:         # checks the length of your input
        messagebox.showerror("error,message=name more than length of '15' ")
    return(name.lower())    # converts the name string entered to lowercase


def print_journalist_name():
        print(journalist)
        # To print name entered


root = Tk()
root.geometry("300x300")

news = StringVar()

label = Label(text="Name").place(x=5, y=100)
entry = Entry(root, width=40, textvariable=news).place(x=45, y=100)
button = Button(root, text="Print", command=print_journalist_name).place(x=90, y=200)

root.mainloop()

Solution

  • In a comment, you said: "it asks you to enter the name in my terminal but i want the name I entered in my entry widget". Well, it asks for user input in the terminal because you told it to do that with the input() call.

    Here's a re-organized version of your code that does what you want.

    We get the user input from the Entry widget's textvariable news, test that it's not too big, convert it to lowercase, and print it in the terminal. If it is too long, we display an error message in the messagebox and set the string in the Entry to an empty string.

    import tkinter as tk
    from tkinter import messagebox
    
    def print_journalist_name():
        # Get the name from the Entry widget
        name = news.get()
        if len(name) > 15:
            msg = "Length of\n{}\nis greater than 15".format(name)
            messagebox.showerror("error", message=msg)
            # Clear the name
            news.set('')
        else:
            print(name.lower())
    
    root = tk.Tk()
    root.geometry("300x300")
    
    label = tk.Label(text="Name").place(x=5,y=100)
    news = tk.StringVar()
    entry = tk.Entry(root, width=40, textvariable=news).place(x=45,y=100)
    button = tk.Button(root, text="Print", command=print_journalist_name).place(x=90,y=200)
    
    root.mainloop()
    

    I got rid of the from tkinter import * because doing import tkinter as tk is cleaner, and makes the code easier to read, since it's obvious which names are coming from Tkinter.

    When you do

    from tkinter import *
    

    it puts 135 Tkinter names into your namespace; in Python 2 you get 175 names. This creates needless clutter in the namespace and it can cause name collisions: if you accidentally name one of your variables with one of the imported names that can lead to mysterious bugs. It's even worse when you do star imports with multiple modules since they can stomp over each others' names. Also, star imports make the code harder to read since you have to remember which names are defined locally and which are imported, and with multiple star imports, where they're imported from.

    For more info on this important topic, please see Why is “import *” bad?