How to get an input in python with a tkinter window

I decided to make, once for all, a function to get an input with tkinter instead that from the console, so that I do not have to rewrite it every time. Here is my example.

P.S.: I have fixed an error. Now it should work fine.

def tkinput(text) -> str:
	import tkinter as tk

	root = tk.Tk()
	question = tk.StringVar()
	tk.Label(root, text=text).pack()
	e = tk.Entry(root, textvariable=question)
	e.pack()
	e.focus()
	# question.set("prova")
	e.bind("<Return>", lambda event: root.destroy())
	root.mainloop()
	return question.get()



if __name__ == '__main__':
	name = tkinput("What is your name?")
	print(f"{name=}")
	

Subscribe to the newsletter for updates
Tkinter templates
My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts