0

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()
AD WAN
  • 1,414
  • 2
  • 15
  • 28
  • 3
    Why are you calling the `input` function in a GUI program? You should be using an Entry widget to get a line of user input. There are a few typos in that code. Also, you shouldn't try to create Tkinter objects like StringVar before you call `Tk()`. – PM 2Ring Aug 18 '17 at 10:54
  • @DAZWAN Once you have found an answer which works for you, click the tick next to the answer to accept it. This lets other people with your problem know which solution worked for you. – Adi219 Aug 18 '17 at 10:56
  • **IF YOU KEEP ABUSING FORMATTING YOU'RE GOING TO HAVE A BAD TIME** –  Aug 18 '17 at 19:27

2 Answers2

1

It doesn't print the journalist's name because you haven't called journalist()!

Change print(journalist) to print(journalist())

That should work!

Adi219
  • 4,712
  • 2
  • 20
  • 43
  • Boss thanks, when you you change it to print(journalist()) it asks you to enter the name in my terminal but i want the name I entered in my entry widget in my GUI to be printed, without asking me to enter it in my terminal. – AD WAN Aug 18 '17 at 11:00
  • For this, you need an input box in your GUI instead of input(). Then, print the input from the input box – Adi219 Aug 18 '17 at 11:04
  • 1
    i tried the input box procedure but it was not what i was looking for but all the same you've taught me new thing now many thanks for this selfless support. – AD WAN Aug 18 '17 at 11:50
1

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?

PM 2Ring
  • 54,345
  • 6
  • 82
  • 182