How to Create Menu and Message Widget in GUI Using Python?
Share
Condition for Creating Menu and Message Widget in GUI Using Python
Menu Widget:
A menu in a GUI provides a way for users to select commands or options through a list, typically displayed as a dropdown or horizontal bar at the top of the window.
Message Widget:
A message widget is used to display a message or information on the GUI window.It typically shows static text, and it can be used for displaying notifications or instructions.
Step-by-Step Process
Menu Widget:
Create a main window using Tkinter.
Add a Menu widget and configure it by creating submenus and commands that can be triggered when selected.
Message Widget:
Use tk.Message() or tk.Label() to display a message on the window.
You can customize the text, font, and positioning.
Sample Code
import tkinter as tk
from tkinter import messagebox
def show_message():
messagebox.showinfo("Message", "Hi I am a Machine Learning Engineer")
def exit_app():
root.quit()
root = tk.Tk()
root.title("Menu and Message Widget Example")
menu_bar = tk.Menu(root)
file_menu = tk.Menu(menu_bar, tearoff=0)
file_menu.add_command(label="Show Message", command=show_message)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=exit_app)
menu_bar.add_cascade(label="File", menu=file_menu)
root.config(menu=menu_bar)
message = tk.Label(root, text="Welcome to the GUI! Select 'Show Message' from the menu.", font=("Arial", 14))
message.pack(pady=20)
root.mainloop()