- 23rd Nov 2023
- 13:54 pm
Python Assignment Question
Develop a basic GUI application using Tkinter or PyQt that interacts with user input and displays output based on certain actions.Develop a basic GUI application using Tkinter or PyQt that interacts with user input and displays output based on certain actions.
Python Assignment Solution
import tkinter as tk
def greet():
name = entry.get() # Get user input from the entry widget
if name:
greeting = f"Hello, {name}!"
output_label.config(text=greeting) # Update the label with the greeting
# Create the main window
root = tk.Tk()
root.title("Personalized Greeting")
# Create a label for instructions
instructions_label = tk.Label(root, text="Enter your name:")
instructions_label.pack()
# Create an entry widget for user input
entry = tk.Entry(root)
entry.pack()
# Create a button to trigger the greeting
greet_button = tk.Button(root, text="Greet", command=greet)
greet_button.pack()
# Create a label to display the greeting
output_label = tk.Label(root, text="")
output_label.pack()
# Start the GUI event loop
root.mainloop()
This script creates a basic Tkinter GUI window with a label prompting the user to enter their name, an entry widget to input the name, a button to trigger the greeting, and a label to display the personalized greeting.
When the user enters their name in the entry field and clicks the "Greet" button, the greet()
function is called. It retrieves the name entered in the entry widget, generates a personalized greeting, and updates the label (output_label
) with the greeting.
To run this script, save it in a file with a .py
extension and execute it using a Python interpreter. You'll see a window where you can enter a name, click the "Greet" button, and it will display a greeting based on the provided name.
About The Author - Jessica Turner
Jessica Turner, a skilled software developer with a focus on user interface design, introduces the GUI application guide. With a background in creating intuitive and interactive interfaces, Jessica shares expertise in "GUI Application Interacting with User Input." Her commitment to simplifying complex concepts and making user-friendly applications is evident as she guides readers through the intricacies of building responsive GUIs, ensuring an enriching experience for both beginners and seasoned developers.