python - How to let your entry receive function you've written and print it in tkinter -
i want entry check length of name type in entry widget , return in lowercase. when click on print button prints name entered in lowercase.
challenge:
it doesn't print name entered when click on print button prints id of journalist
function.
import sys tkinter import messagebox tkinter import * def journalist(): name=input("please enter journalist name:") if len (name)>15: # checks length of input messagebox.showerror("error,message=name more length of '15' ") sys.exit() return (name.lower()) # converts name string entered lowercase def print_journalist_name(): print(journalist) # print name entered news=stringvar root=tk() root.geometry("300x300") 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(
in comment, said: "it asks enter name in terminal want name entered in entry widget". well, asks user input in terminal because told input()
call.
here's re-organized version of code want.
we user input entry widget's textvariable news
, test it's not big, convert lowercase, , print in terminal. if long, display error message in messagebox , set string in entry empty string.
import tkinter tk tkinter import messagebox def print_journalist_name(): # name entry widget name = news.get() if len(name) > 15: msg = "length of\n{}\nis greater 15".format(name) messagebox.showerror("error", message=msg) # clear 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 from tkinter import *
because doing import tkinter tk
cleaner, , makes code easier read, since it's obvious names coming tkinter.
when do
from tkinter import *
it puts 135 tkinter names namespace; in python 2 175 names. creates needless clutter in namespace , can cause name collisions: if accidentally name 1 of variables 1 of imported names can lead mysterious bugs. it's worse when star imports multiple modules since can stomp on each others' names. also, star imports make code harder read since have remember names defined locally , imported, , multiple star imports, they're imported from.
for more info on important topic, please see why “import *” bad?
Comments
Post a Comment