Using Textbox And Text Capturing Function In Python GUI Application

Intro 

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  
  3. import ttk  
  4. win = tk.Tk()  
  5. # Application Name  
  6. win.title("Python GUI App")  
  7. # Label  
  8. lbl = ttk.Label(win, text = "Enter the name:").grid(column = 0, row = 0)  
  9. # Click event  
  10. def click(): print("Hi," + name.get())  
  11. # Textbox widget  
  12. name = tk.StringVar()  
  13. nameEntered = ttk.Entry(win, width = 12, textvariable = name).grid(column = 0, row = 1)  
  14. # Button widget  
  15. button = ttk.Button(win, text = "submit", command = click).grid(column = 1, row = 1)  
  16. win.mainloop()  

About the code

 

  • First, I am importing the tkinter module.
  • Next, I assign a class and label variables and give 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.

Output



 

 
Ebook Download
View all
Learn
View all