How to add Menu bar in custom tkinter
Asked Answered
C

2

7

How can I add the Manu bar in my custom TKinter window?

I want to add a menu bar in my custom TKinter window.

Catullus answered 18/12, 2022 at 14:40 Comment(4)
You can follow this. pythontutorial.net/tkinter/tkinter-menuSquirrel
What it's your window code and why do you think it's not adding menu barsNorthnortheast
I won't custom tkinter not tkinter because I want black background in that and more customised title bar and allCatullus
Please, when asking a question, explain what you tried and where you're stuck. This looks like "please write my code for me".Adder
S
3

CustomTkinter currently does not change the appearance of the menubar, its still pretty basic. Many GUI toolkits are eschewing tradition menus for a fully customisable UI.

enter image description here

Your best option is likely to build your own using CTk widgets, such as CTkOptionMenu or CTkComboBox for example.

enter image description here

From a usability and accessibility perspective there are advantages to using a traditional menu system though.

import customtkinter

customtkinter.set_appearance_mode("light")  
customtkinter.set_default_color_theme("dark-blue")

app = customtkinter.CTk()
app.title("CustomTkinter Sample Menu Bar.py")

def button_callback():
    print("Button click", combobox_1.get())

def change_appearance_mode_event(new_appearance_mode: str):
    customtkinter.set_appearance_mode(new_appearance_mode)

def change_scaling_event(new_scaling: str):
    new_scaling_float = int(new_scaling.replace("%", "")) / 100
    customtkinter.set_widget_scaling(new_scaling_float)


frame_1 = customtkinter.CTkFrame(master=app)
frame_1.grid(row=0, pady=10, padx=0)

optionmenu_1 = customtkinter.CTkOptionMenu(frame_1, values=["Option 1", "Option 2", "Option 42 long long long..."])
optionmenu_1.grid(row=0, column=0, pady=10, padx=10)
optionmenu_1.set("CTk Option Menu")

combobox_1 = customtkinter.CTkComboBox(frame_1, values=["Option 1", "Option 2", "Option 42 long long long..."])
combobox_1.grid(row=0, column=1, pady=10, padx=0)
combobox_1.set("CTk Combo Box")

appearance_mode_optionemenu = customtkinter.CTkOptionMenu(frame_1, values=["Light", "Dark", "System"], command=change_appearance_mode_event)
appearance_mode_optionemenu.grid(row=0, column=2, padx=0, pady=10)

scaling_optionemenu = customtkinter.CTkOptionMenu(frame_1, values=["80%", "90%", "100%", "110%", "120%"], command=change_scaling_event)
scaling_optionemenu.grid(row=0, column=3, padx=0, pady=10)

text_1 = customtkinter.CTkTextbox(master=frame_1, width=600, height=70)
text_1.grid(row=1, column=0, columnspan=4, pady=10, padx=10)
text_1.insert("0.0", "Lorem Ipsum adalah contoh teks atau dummy dalam industri percetakan dan penataan huruf atau typesetting. Lorem Ipsum telah menjadi standar contoh teks sejak tahun 1500an, saat seorang tukang cetak yang tidak dikenal mengambil sebuah kumpulan teks dan mengacaknya untuk menjadi sebuah buku contoh huruf. Ia tidak hanya bertahan selama 5 abad, tapi juga telah beralih ke penataan huruf elektronik, tanpa ada perubahan apapun. Ia mulai dipopulerkan pada tahun 1960 dengan diluncurkannya lembaran-lembaran Letraset yang menggunakan kalimat-kalimat dari Lorem Ipsum, dan seiring munculnya perangkat lunak Desktop Publishing seperti Aldus PageMaker juga memiliki versi Lorem Ipsum.")

app.mainloop()
Sarcenet answered 9/2, 2023 at 19:55 Comment(0)
A
0

I am not sure there's a built-in Menu bar option in CTK you can use the Tkinter menu bar within it and customize it according to your design. for example: enter image description here

# Menu Bar
        menu_bar = tk.Menu(self)
        m1 = tk.Menu(menu_bar, tearoff=0)
        m1.add_command(label="Open File",command=self.open_file)
        m1.add_separator()
        m1.add_command(label="Save File",command=lambda : self.save_final(0))
        self.config(menu=menu_bar)

        menu_bar.add_cascade(label="File",menu=m1)
        m2 = tk.Menu(menu_bar, tearoff=0)
        m2.add_command(label="Light theme",command=lambda : self.theme_selection(0))
        m2.add_command(label="Dark theme",command=lambda : self.theme_selection(1))
        m2.add_separator()
        m2.add_command(label="System theme",command=lambda : self.theme_selection(2))
        self.config(menu=menu_bar)
        menu_bar.add_cascade(label="Setting",menu=m2)
        
        m3 = tk.Menu(menu_bar, tearoff=0)
        m3.add_command(label="help!",command=lambda : self.help_us())

        self.config(menu=menu_bar)
        menu_bar.add_cascade(label="Help",menu=m3)
        
        
        # configure grid layout (4x4)
        # self.grid_columnconfigure(0, weight=1)
        self.grid_columnconfigure(1, weight=1)
        self.grid_columnconfigure((2, 3), weight=1)
        self.grid_rowconfigure((1, 2), weight=1)
        
        # Creating Side Bar Button here
        self.Left_Side_Bar_Button()
        
        # search Bar with Button 
        self.search_frame = customtkinter.CTkFrame(self, corner_radius=0, fg_color="transparent")
        self.search_frame.grid(row=0, column=1, sticky="nw")
        self.search_frame.grid_rowconfigure(2, weight=1)
        self.search_bar_entry = customtkinter.CTkEntry(self.search_frame, placeholder_text="Search", width=200, corner_radius=0,fg_color="#fafafa", text_color="#000000")
        self.search_bar_entry.grid(row=0, column=0, sticky="nw", pady=15)
        self.bind('<Return>', self.update_row)
        search_img = customtkinter.CTkImage(Image.open(os.path.join(image_path, "search.png")), size=(18, 18))
        self.search_btn =  customtkinter.CTkButton(self.search_frame, text="", image=search_img , command=self.update_row_for_btn, height=28, width=28,fg_color="#fafafa", corner_radius=0, border_color="#565b5e", border_width=2 )
        self.search_btn.grid(row=0, column=1, sticky="nw", pady=15, padx=(0,20))
        
        # Add form Button
        add_img = customtkinter.CTkImage(Image.open(os.path.join(image_path, "add.png")), size=(20, 20))
        self.search_btn =  customtkinter.CTkButton(self.search_frame, text="", image=add_img , command=self.Add_Data, height=30, width=30, corner_radius=5,fg_color="#fafafa")
        self.search_btn.grid(row=0, column=2, sticky="nw", pady=15)

I hope it may help you thanks

Allophone answered 10/5, 2023 at 13:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.