Input box in Python tkinter

Tommy 74 Published: 12/07/2024

Input box in Python tkinter

Here is a simple example of how you can create an input box using Python's Tkinter library:

import tkinter as tk

class Application(tk.Frame):

def init(self, master=None):

super().init()

self.master = master

self.pack()

self.create_widgets()

def create_widgets(self):

self.hi_label = tk.Label(self, text="Hi!")

self.hi_label.pack(side="top")

self.query_entry = tk.Entry(self, width=50)

self.query_entry.pack()

self.response_text = tk.Text(self, height=10, width=100)

self.response_text.pack()

def submit_query(self):

query = self.query_entry.get()

if query:

You can add your logic here to generate a response based on the input query

response = "You asked: " + query

self.response_text.delete(1.0, tk.END)

self.response_text.insert(tk.INSERT, response)

root = tk.Tk()

app = Application(master=root)

app.mainloop()

In this code:

We first import Tkinter and then create a simple application with two main widgets: an entry box for the user to input their query, and a text area where the response will be displayed. The create_widgets method is used to set up these widgets in the GUI. The submit_query method can be called whenever the user clicks a button or submits the form. In this case, it simply retrieves the query from the entry box and displays the query along with some placeholder response.

Now let's enhance the functionality by responding based on the input query:

import tkinter as tk

class Application(tk.Frame):

def init(self, master=None):

super().init()

self.master = master

self.pack()

self.create_widgets()

def create_widgets(self):

self.hi_label = tk.Label(self, text="Ask me anything!")

self.hi_label.pack(side="top")

self.query_entry = tk.Entry(self, width=50)

self.query_entry.pack()

self.response_text = tk.Text(self, height=10, width=100)

self.response_text.pack()

def submit_query(self):

query = self.query_entry.get()

if query:

response_dict = {

"hello": "Hello there!",

"hi": "Hi back at you!",

"goodbye": "Goodbye for now.",

"thanks": "You're welcome!",

"who are you?": "I'm just an AI, here to help."

}

if query in response_dict:

self.response_text.delete(1.0, tk.END)

self.response_text.insert(tk.INSERT, response_dict[query])

else:

self.response_text.delete(1.0, tk.END)

self.response_text.insert(tk.INSERT, "Sorry, I didn't understand that.")

root = tk.Tk()

app = Application(master=root)

app.mainloop()

In this code:

We have a dictionary response_dict where the keys are some common queries and the values are the responses. In the submit_query method, we check if the input query is in the response_dict. If it is, we display that response. If not, we show a message saying "Sorry, I didn't understand that."

Python input box GUI

Here is a comprehensive overview of creating a simple GUI for taking user input using the tkinter library in Python:

What is tkinter?

Tkinter is Python's de-facto standard GUI (Graphical User Interface) package. It provides a powerful object-oriented windowing toolkit to create fully featured windows with scalable graphics, menus, modality, and more.

Creating a Simple GUI for Taking User Input

To create a simple GUI that takes user input, we'll use the tkinter library in Python. Here's an example code:

import tkinter as tk

class Application(tk.Frame):

def init(self, master=None):

super().init(master)

self.master = master

self.pack()

self.create_widgets()

def create_widgets(self):

self.input_field = tk.Entry(self)

self.input_field.pack(side="top")

self.label = tk.Label(self)

self.label["text"] = "Enter your input:"

self.label.pack(side="top")

self.submit_button = tk.Button(self)

self.submit_button["text"] = "Submit"

self.submit_button["command"] = self.process_input

self.submit_button.pack(side="top")

def process_input(self):

user_input = self.input_field.get()

print(f"User input: {user_input}")

self.input_field.delete(0, "end")

root = tk.Tk()

app = Application(master=root)

app.mainloop()

Breaking it Down

Let's break down the code:

We import the tkinter library and assign it to the alias tk. We create a class Application that inherits from tk.Frame. This is the main container for our GUI. In the constructor (__init__ method), we set up some basic attributes: self.master: The main window (or master) of our application, which we'll pass to the Frame class. self.pack(): Packs our frame into the window. We define a method create_widgets() that creates the GUI components: An input field (Entry) where users can enter their input. A label (Label) that displays a message. A submit button (Button) that, when clicked, calls the process_input method. In the process_input method, we get the user's input from the input field and print it to the console. We create an instance of our Application class, passing the main window (root) as the master. Finally, we start the GUI event loop using the mainloop() method.

Running the Code

To run this code, simply execute it in your Python interpreter or save it to a file and run that file using Python. When you run the code, a simple GUI will appear with an input field, label, and submit button. Enter some text in the input field and click the submit button to see the output printed to the console.

This is just a basic example of what you can do with tkinter and Python. With more advanced features like menus, dialog boxes, and graphics, tkinter provides endless possibilities for creating complex GUIs!