Sunday, February 7, 2021

Menu Widget

menu_frame2.py


#!/usr/bin/python3


from tkinter import *

#from tkinter import ttk


root = Tk()

root.title("Controls")

root.geometry("400x400+10+100")


my_menu = Menu(root)

root.config(menu=my_menu)


def our_command(my_text):

    hide_all_frames()

    my_label.config(text=my_text)

    my_label.pack()


#file_new function

def option_red():

    hide_all_frames()

    option_red_frame.pack(fill="both", expand=1)

    my_label=Label(option_red_frame, text="Option->Red")

    my_label.pack()


#edit_cut function

def edit_blue():

    hide_all_frames()

    edit_blue_frame.pack(fill="both", expand=1)

    my_label=Label(edit_blue_frame, text="Edit->Blue")

    my_label.pack()


#hide all frames function

def hide_all_frames():

    my_label.forget()


    #loop thru all chilern in frame and delete them

    for widget in edit_blue_frame.winfo_children():

        widget.destroy()

    for widget in option_red_frame.winfo_children():

        widget.destroy()


    edit_blue_frame.pack_forget()

    option_red_frame.pack_forget()


#create a new item

file_menu = Menu(my_menu)

my_menu.add_cascade(label="File", menu=file_menu)

file_menu.add_command(label="New...", command=lambda: our_command("New"))

file_menu.add_separator()

file_menu.add_command(label="Exit", command=root.quit)


#create Edit Menu item

edit_menu = Menu(my_menu)

my_menu.add_cascade(label="Edit", menu=edit_menu)

edit_menu.add_command(label="Cut", command=lambda: our_command("Cut"))

edit_menu.add_command(label="Copy", command=lambda: our_command("Copy"))

edit_menu.add_separator()

edit_menu.add_command(label="Blue", command=edit_blue)


#create Optiion Menu item

option_menu = Menu(my_menu)

my_menu.add_cascade(label="Option", menu=option_menu)

option_menu.add_command(label="Find", command=lambda: our_command("Find"))

option_menu.add_command(label="Find Next", command=lambda: our_command("Find Next"))

option_menu.add_separator()

option_menu.add_command(label="Red", command=option_red)


#create frames

edit_blue_frame = Frame(root, width=400, height=400, bg="blue")

option_red_frame = Frame(root, width=400, height=400, bg="red")


my_label=Label(root)

my_label.pack()


root.mainloop()

 

No comments:

Post a Comment