Using Textbox And Text Capturing Function In Python GUI Application


In this blog, I am going to create a textbox and text capturing function in Python GUI application. When a user enters the text in the textbox and clicks the Submit button, the button action will capture the text and display the Hi text in Python console.
Software requirement
Python 3.5 and IDLE (Python 3.5)
Programming code
  1. import tkinter as tk  
  2. from tkinter import ttk
  4. win = tk.Tk()# Application Name  
  5. win.title("Python GUI App")# Label  
  6. lbl = ttk.Label(win, text = "Enter the name:").grid(column = 0, row = 0)# Click event  
  7. def click():   
  8.     print("Hi," + name.get())# Textbox widget  
  9. name = tk.StringVar()  
  10. nameEntered = ttk.Entry(win, width = 12, textvariable = name).grid(column = 0, row = 1)# Button widget  
  11. button = ttk.Button(win, text = "submit", command = click).grid(column = 1, row = 1)  
  12. win.mainloop()   
About the code
  • First, I am importing the tkinter module.
  • Next, I assign a class and label variables and give the application title.
  • After label creation, next, I define click function then create textbox and button in the code.
  • Finally, I have started the Windows event loop by calling the main loop method and executed the code.
The Python GUI application will appear on the screen.