I have a quick question, how would you make a drop down menu in Tkinter like the one below:
This menu has a drop down option, how would you add a drop down in tkinter here is my code:
# Menu Bar
MenuBar = Menu(root)
root.config(menu=MenuBar)
MenuBar.config(bg="White", fg="Black", activebackground="Whitesmoke", activeforeground="Black", activeborderwidth=1, font=('Monaco', 11))
# Settings Option
SettingsOption = Menu(MenuBar, tearoff=False)
MenuBar.add_cascade(label="Settings", menu=SettingsOption)
SettingsOption.add_command(label="Help", command=None)
SettingsOption.add_command(label="Documentation", command=None)
So whenever I click Settings I should get a menu called help. Then when I hover over help I should get another dropdown menu called documentation. How would you do this in Python Tkinter?
You can use add_cascade() to add sub-menu:
import tkinter as tk
root = tk.Tk()
menubar = tk.Menu(root)
menubar.config(bg="white", fg="black", activebackground="whitesmoke", activeforeground="black", activeborderwidth=1, font="Monaco 11")
settings_menu = tk.Menu(menubar, tearoff=False)
help_menu = tk.Menu(settings_menu, tearoff=False)
help_menu.add_command(label="Documentation")
settings_menu.add_cascade(label="Help", menu=help_menu)
menubar.add_cascade(label="Settings", menu=settings_menu)
root.config(menu=menubar)
root.mainloop()
Related
I'm trying to change the background color of the menubar that I made using the Tkinter widget, I changed the background color of the title-bar thanks to a solution that I came across on StackOverflow even tho it wasn't for windows 10 but it worked in my windows 10 however it's not able to change the color of the menubar.
Here's the code including the function to change title-bar.
from tkinter import *
import ctypes as ct
root = Tk()
root.title("MenuBar in GUI")
root.geometry("500x500")
root.config(bg="black")
# These attributes are not working, menubar still remains unchanged
menubar = Menu(root, bg='black', fg='cyan', activebackground="grey",activeforeground="cyan")
filemenu = Menu(menubar, bg="black", fg="purple",activebackground="grey", activeforeground="cyan", tearoff=False)
menubar.add_cascade(label="File", menu=filemenu)
root.config(bg="black", menu=menubar) # Defining the main menu
root.mainloop()
When i tried to customize the menubar it didn't worked as it's shown in this screenshot, Background and other colors are suppose to change but it didn't.
Screenshot
I'm trying to change the background color of the menubar that I made
using the Tkinter widget.
Try this.
import tkinter as tk
class MenuBar(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master, bd=1, relief='raised')
self.master=master
self.configure(background='orange', cursor='hand2')
file = tk.Menubutton(self, text='File',
background='black',
foreground='red',
activeforeground='black',
activebackground='white'
)
file_menu = tk.Menu(file,tearoff=0)
file_menu.add_command(label='save',
background='black',
foreground='white',
activeforeground='black',
activebackground='white'
)
file.config(menu=file_menu)
file.pack(side='left')
edit = tk.Menubutton(self, text='Edit',
background='blue',
foreground='white',
activeforeground='black',
activebackground='white'
)
edit_menu = tk.Menu(edit,tearoff=0)
edit_menu.add_command(label='add',
background='black',
foreground='white',
activeforeground='black',
activebackground='white'
)
edit.config(menu=edit_menu)
edit.pack(side='left')
root = tk.Tk()
menubar = MenuBar(root)
menubar.pack(side='top', fill='x')
root.mainloop()
Output before:
Output after:
I have been struggling with merging my code.
I am creating an arcade game on Python and have a main file where I have an image and clickable assets which link to a game I have imported.
Now I am working on creating constant features in the game, including a menu bar which displays reminders, can change the volume and brightness setting etcetura.
However, I don't know how to make this on my main project file.
Could you help me out?
I am using Python Pygame, Tkinter and Turtle.
It sounds like you need to think some more about what exactly you want. Your question is rather vague and tkinter has several options that can be presented in various ways to a user for making selections. Below is a quick example of some ideas you may want to explore.tkinter widgets
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry("400x400")
root.resizable(False, False)
root.title("Sample")
menu = tk.Menu(root)
root.config(menu=menu)
file_menu = tk.Menu(menu, tearoff=0)
menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="Quit", command=quit)
file_menu.add_command(label="Something", command=quit)
file_menu.add_command(label="New Option", command=quit)
label = tk.Label(root, text="Choose an option below", font="times 14")#label
label.pack()
items = ["Hi", "Something", "Bye"]
combo_box = ttk.Combobox(root, font=20, state="normal")#combo box
combo_box['values'] = items
combo_box.pack()
lblabel = tk.Label(root, text="Choose an item from the list", font="times 14")#label
lblabel.pack()
items_var = tk.StringVar(value=items)
list_box = tk.Listbox(root, listvariable=items_var, selectmode=tk.BROWSE)#list box
list_box.pack()
rblabel = tk.Label(root, text="Choose an option below", font="times 14")#label
rblabel.pack()
choices = tk.StringVar(value=items)
radio_button = ttk.Radiobutton(root, text="some_option")#radio button
radio_button.pack()
root.mainloop()
I'm try to create scroll bar in "notepad(Program name)" but when i run my program it don't show
scrollbar in window .......................................................................
.....................................................................
from tkinter import *
SIDE_R = RIGHT
windows = Tk()
windows.title("NotePad")
windows.minsize(width=60, height=80)
windows.config()
frame = Frame(windows)
# Menu bar
# # File
menubar = Menu(windows)
file = Menu(menubar, tearoff=0)
file.add_command(label="New")
file.add_command(label="Open")
file.add_command(label="Save")
file.add_command(label="close")
file.add_separator()
file.add_command(label="Exit", command=windows.quit)
menubar.add_cascade(label="File", menu=file)
# # Edit
edit = Menu(menubar, tearoff=0)
edit.add_separator()
edit.add_command(label="Copy")
edit.add_command(label="Paste")
edit.add_command(label="Cute")
edit.add_command(label="Delete")
edit.add_command(label="Select All")
menubar.add_cascade(label="Edit", menu=edit)
# # Help
helps = Menu(menubar, tearoff=0)
helps.add_command(label="About")
menubar.add_cascade(label="Help", menu=help)
# Create place to write a text
text = Text(windows)
text.focus()
text.grid(pady=0.5, padx=10)
# Creating Scale
scroll = Scrollbar(windows, orient=VERTICAL,)
scroll.grid(row=0, column=1, sticky=NS+SE)
text.config(yscrollcommand=scroll.set)
scroll.config(command=text.yview)
windows.config(menu=menubar)
windows.mainloop()
In the toolbar I press a button and a drop-down menu with buttons opens. The images are. gif for buttons. I want the button in the toolbar to open a drop-down menu with buttons (image - gif). How can I do this?
# Image Toolbar
img1 = PhotoImage(file = r\path\"img.gif")`
# Toolbar
toolbar = Frame(root, bd=1, relief=RAISED)
toolbar.pack(side=TOP, fill=X)
btn1 = Button(toolbar, relief=FLAT, compound= LEFT, text="",image=img1, command=submenu1)
btn1.pack(side=LEFT, padx=0, pady=0)
def submenu1():
# gif icon for submenu1:
imgvar1 = PhotoImage(file=r\path\.gif)
???
To make a button with a drop-down menu I suggest you use a Menubutton which was designed for that purpose — it's the part that stays on the screen all the time. After creating one you should create a Menu widget and configure the Menubutton to use it.
Once you have a Menu instance, you can populate it with different kinds of menu items, including ones made of images by using the Menu.insert_cascade() method. Also see this Menu item creation documentation which describes all the different kinds of menu items you can create and add to one.
Below is some sample code illustrating what I mean. For simplicity I used the same image twice — on the Menubutton and on one of the items on the drop-down menu that is displayed when it's clicked.
import tkinter as tk
from tkinter.constants import *
root = tk.Tk()
# Image Toolbar
img1 = tk.PhotoImage(file="8-ball.png")
# Toolbar
toolbar = tk.Frame(root, bd=1, relief=RAISED)
toolbar.pack(side=TOP, fill=X)
menubtn = tk.Menubutton(toolbar, relief=FLAT, compound=LEFT, text="", image=img1)
menubtn.pack(side=LEFT, padx=0, pady=0)
menu = tk.Menu(menubtn, tearoff=0)
menubtn.config(menu=menu)
menu.insert_command(0, label='Submit', command=lambda: print('Submit clicked'))
imgvar1 = tk.PhotoImage(file="8-ball.png")
menu.insert_cascade(1, image=imgvar1, command=lambda: print('Image pop out clicked'))
root.mainloop()
I'm currently a noob to tkinter as i made this code. so kindly ignore any foolishness if found.
import tkinter as tk
from tkinter import ttk
from tkinter import Menu
win=tk.Tk()
win.title('Tabs GUI')
tab_control=ttk.Notebook(win)
tab1=ttk.Frame(tab_control)
tab_control.add(tab1,text='Tab 1')
tab_control.grid()
tab2=ttk.Frame(win) #1
tab_control.add(tab2,text='Tab 2')
menuBar = Menu(tab1) #2
win.config(menu=menuBar)
# Add menu items
fileMenu = Menu(menuBar, tearoff=0)
fileMenu.add_command(label="New")
fileMenu.add_separator()
fileMenu.add_command(label="Exit")
menuBar.add_cascade(label="File", menu=fileMenu)
# Add another Menu to the Menu Bar and an item
helpMenu = Menu(menuBar, tearoff=0)
helpMenu.add_command(label="About")
menuBar.add_cascade(label="Help", menu=helpMenu)
ttk.Label(tab2, text="Choose a number:").grid(column=0, row=0)
numberChosen = ttk.Combobox(tab2, width=8)
numberChosen['values'] = (1, 2, 4, 42, 100)
numberChosen.grid(column=0, row=1)
numberChosen.current(0)
chVarDis = tk.IntVar()
check1 = tk.Checkbutton(tab2, text="Disabled", variable=chVarDis, state='disabled')
check1.select()
check1.grid(column=0, row=2, sticky=tk.W)
chVarUn = tk.IntVar()
check2 = tk.Checkbutton(tab2, text="UnChecked", variable=chVarUn)
check2.deselect()
check2.grid(column=1, row=2, sticky=tk.W )
win.mainloop()
now the first problem is that on #1 i gave 'tab2' master window i.e 'win', then added it to the notebook but it acts like i gave it notebook() as a master window because 'tab1' and 'tab2' are functioning same. And the second problem is why 'combobox' is auto-focused when i open 'tab2'. and the same goes for Menu() #2, i've given it parent tab1 yet it is displayed in top level window win