Menu bar does not show up on macOS - python

I wrote this code on Pycharm (macOS) but the menu bar does not show up. Can anyone tell me why?
from tkinter import *
root = Tk()
def hello():
print("hello!")
# create a toplevel menu
menubar = Menu(root)
menubar.add_command(label="Hello!", command=hello)
menubar.add_command(label="Quit!", command=root.destroy)
# display the menu
root.config(menu=menubar)
root.mainloop()

You cannot put commands at the very top of a menubar in OSX. Also, the menu will not appear at the top of the window, it appears at the top of the screen just like with native OSX apps.
Other than the fact that your menubar has no dropdown menus, it's working as designed. Add a dropdown menu, and that menu will appear on the menubar.

Related

How call a function in a Tkinter menu

I would like to call a function clicking on fileMenu 'Action', I was able to call the function (below is just one example) only by entering it in the submenu, is there a way for make it?
import tkinter as tk
from tkinter import Menu
def stateMenu():
fileMenu.entryconfig("Delete", state="disabled")
fileMenu.entryconfig("Transfer", state="disabled")
root = tk.Tk()
menubar = Menu()
fileMenu = Menu(menubar, tearoff=0)
menubar.add_cascade(label="Action", menu=fileMenu) // I would like call function here
fileMenu.add_command(label="Delete", command=stateMenu, state="normal") // no here
fileMenu.add_command(label="Transfer", command=stateMenu, state="normal") // no here
root.config(menu=menubar)
root.mainloop()
The only way to call a command from a menubar is by adding the command with add_command. Some operating systems allow you to add commands to the root menubar, some don't.
From a usability perspective it's generally considered a bad idea to add commands directly on the menubar. Users expect to see a menu when they click on something on the menubar, and would be surprised if some action immediately happened.

How can I change the color of the menu in Tkinter?

How can I change the color of the menu in Tkinter? That is, I need to change the color of the menu, which is attached to the window through root.config (menu = menu). The usual use of menu.config (bg = 'black') does not help.
Code
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
menu = tk.Menu(root)
#menu.config(background='black')
root.config(menu = menu)
root.config(bg='black')
file = tk.Menu(root, tearoff=0)
file.add_command(label='GYG')
menu.add_cascade(label='Hello', menu=file)
root.mainloop()
PS Sorry if I have awkward English (I speak Russian, but the Russian version of the site did not help me)
Reply to comment
I commented out a line to show how I tried to change the background (from the screenshot this is clearly visible)
This depends on your platform. You can't change the color on OSX, and I'm pretty sure you can't change it on Windows, either. When you associate a menu with the window using the menu option of the window, tk has very little control over the menu. The menus are largely managed by the underlying OS.
The way to do it is how you're doing it. If it's not working, then it is a restriction on the platform you're working on.

How to disable a tkinter OptionMenu

I can't figure out or find how to disable a tkinter OptionsMenu. I have 3 optionsmenu's in my GUI and want to disable them when a button is clicked
self.menu = OptionMenu(self, var, *items)
btn = Button(self, text="disable", command = self.disable)
btn,pack()
self.disable(self):
//Disable menu here...
Is there a way to just call a built in function for OptionMenu and disable it? Or do I have to disable every option in the menu? (Which i also can't figure out)
BTW: I used the menu.pack() for a separate Topleve() window that pops up, but I started off with the grid() system in my main Tk window, used by menu.grid(row=0,column=0)
EDIT:
So I forgot to mention that I have multiple OptionMenus being generated by a constructor method. This is what I tried doing and didn't work:
makeMenu():
menu = OptionMenu(self, var, *items)
....//whole bunch of menu settings
return menu
menu1 = makeMenu()
all_menus.append(menu)
Now the reason this didn't work is because I had to append it after creation. I don't know why the settings don't carry over, but what I had to do is this:
makeMenu():
menu = OptionMenu(self, var, *items)
....//whole bunch of menu settings
return menu
makeMenu():
menu = OptionMenu(self, var, *items)
....//whole bunch of menu settings
all_menus.append(menu)
makeMenu()
And with this change, I can use this to disable menus later on:
for menu in all_menus:
menu.config(state=DISABLED)
Like with any other widget, you use the configure method to set the state to "disabled":
self.menu.configure(state="disabled")
The above will work for both the tkinter and ttk OptionMenu widgets.

Tkinter menu copy/paste/cut option

I have coded in that when you right click you get a menu With copy/cut/paste after some googeling, but i havent found anything about a dropdown menu With copy/cut/paste...
What i got:
from Tkinter import *
master = Tk()
Edit.add_command(label="Copy")
Edit.add_command(label="Paste")
Edit.add_command(label="Cut")
bar.add_cascade(label="Edit", menu=Edit)
mainloop()
Notes:
I am coding in Python 2.7
You need to create a menubar,
mymenu = Menu(master)
create the Edit menu,
editmenu = Menu(mymenu, tearoff=0) # editmenu is now a child of mymenu
add your menu options with labels and commands,
editmenu.add_command(label='Cut', command=cut) # 'cut' is a cut function you wrote
editmenu.add_command(label='Copy', command=copy) # need a copy function too
editmenu.add_command(label='Paste', command=paste) # paste function
then add that Edit menu to the menubar,
mymenu.add_cascade(label='Edit', menu=editmenu)
then add the menubar to the master tk object:
master.config(menu=mymenu)
Then a menubar will appear at the top of the window when you run the program. Make sure you define the cut, copy, and paste functions, or you'll get an error. You can use print as a placeholder if you want.

How to change the colour of menu in Tkinter under windows?

I'm using windows xp. I want to change menubar and labels foreground and background in TKinter. But, I'm unable to change. Can I change it in windows xp or I have to upgrade it to windows 7.
from Tkinter import *
root = Tk()
menubar = Menu(root)
menubar.add_command(label = 'Label1', command = log, background = 'Black', foreground = 'Red')
root.config(menu=menubar)
root.mainloop()
I'm able to display what I want and my code is working perfectly in Linux. But, it's not changing the color in window. Do I need to use any additional commands to make it work?
There is nothing you can do. Tkinter uses a native menu object for the menus, which means they will have exactly the same look and feel of other windows menus.
from Tkinter import *
def log():
print 'in log fun'
root = Tk()
menubar = Menu(root)
menubar.add_command(label = 'Label1', command = log)
root.config(bg='red',menu=menubar)
root.mainloop()
you can config the background color, not possible to menu background color.
enter image description here

Categories