Python Tkinter menu bars don't display
tkinter menu bar color
tkinter menu checkbutton
tkinter menu is not defined
tkinter popup menu
option menu tkinter
tkinter menu command arguments
I'm trying to make a GUI using Tkinter and have come to implementing a menu bar. I've looked at a few tutorials and written some code for it, but a menu bar never seems to appear - just a blank frame with a white background. This doesn't just happen for my code though; on copying and pasting the code of one of the aforementioned tutorials into a new script, the same behaviour is exhibited.
I'd appreciate it if anyone could shed any light on what's causing this. My system is OS X 10.5, Python 2.7, Tk 8.4. Here's the code from the tutorial that doesn't appear to work:
#!/usr/local/bin/python2.7 from Tkinter import * from ttk import * class App(Frame): def __init__(self): Frame.__init__(self) self.master.geometry('400x300') self.master.title(__file__) self.pack() self.menu = Menu(tearoff=False) self.master.config(menu = self.menu) fm = self.file_menu = None fm = Menu(self.menu, tearoff=False) self.menu.add_cascade(label='File', menu = fm) fm.add_command(label='Say Hello', command = self.say_hello) fm.add_separator() fm.add_command(label='Quit', command = self.quit) self.mainloop() def say_hello(self, *e): self.label = Label(self.master, text='Hello there!') self.label.pack(anchor=CENTER, fill=NONE, expand=YES, side=LEFT) if __name__ == '__main__': App()
and my code is here:
from Tkinter import * class App(Frame): def __init__(self, parent): Frame.__init__(self, parent) parent.title("Cluedo Solver 1.0") menubar = Menu(root) menubar.add_command(label="File") menubar.add_command(label="Quit", command=root.quit()) root.config(menu=menubar) root=Tk() root.geometry("300x250+300+300") app=App(root) root.mainloop()
Code with Explanation
From personal experience, I have found that it is usually easier to manage all widgets in a widgets method. That is what I did here, and it worked. Also, instead of parent, I used master. I will now walk you through the code step-by-step.
from Tkinter import *
We import Tkinter (GUI stuff)
We create a class called
App, which is the
Frame where widgets are held.
def __init__(self, master): Frame.__init__(self, master) self.grid() self.widgets()
We create a method called
__init__. This initializes the class, and runs another method called
def widgets(self): menubar = Menu(root) menubar.add_command(label="File") menubar.add_command(label="Quit", command=root.quit()) root.config(menu=menubar)
We create the
widgets method. This is where the widget,
menubar is added. If we were to create anymore widgets, they would also be here.
root=Tk() root.title("Menubar") app=App(root) root.mainloop()
Lastly, we give the entire window some properties. We give it a title,
Menubar, and run the
App class. lastly, we start the GUI's mainloop with
Menu Bars With tKinter - Python Tkinter GUI Tutorial #46, I'm trying to make a GUI using Tkinter and have come to implementing a menu bar. I've looked at a few tutorials and written some code for it, but a menu bar Tkinter menu bar Now that we've got the basic window, buttons, and event handling down, we're ready to tackle the idea of a menu bar. The way tkinter works, along with quite a few graphics/windows operations work, is with a main window, then you sort of build things on top of it, then display everything all at once, which gives the appearance
Based on some comments you made to one of the answers, you are apparently running this on a Macintosh. The code works fine, but the menu appears in the mac menubar rather than on the window like it does on Windows and Linux. So, there's nothing wrong with your code as far as the menubar is concerned.
The Tkinter Menu Widget, Menu Bars With tKinter - Python Tkinter GUI Tutorial #46 but i don't already know what to Duration: 11:02 Posted: Mar 9, 2020 Almost every app has a main menu. As expected, Tkinter supports adding a main menu to your application window. The screenshot below demonstrates a Tkinter based menu: Tkinter menu. Related course. Python Desktop Apps with Tkinter; Tkinter menubar You can create a simle menu with Tkinter using the code below. Every option (new, open, save..
Check your mac menu bar if you are doing any GUI that involves menubar which you will like to view or test. Its subtle and you may think you code is not to working. Click on the app(in this case python window), it will show a drop down menubar.
How to add Menus to Tkinter applications, root = Tk() def hello(): print "hello!" menubar = Menu(root) # create a pulldown menu, and add it to the menu bar filemenu = Menu(menubar, tearoff=0) In this video I'll show you how to create Menu bars at the top of your tKinter programs. Menus in tKinter are incredibly easy. You just define them, configure them, then cascade them onto the screen.
I am trying the code as above, but all I get is "Python" on the macOS menubar and it's usual pulldown. tkinter just doesn't seem to work menus on macOS 10.14.1
I think what is happening is that there are mac specific fixups that are changing the event codes so certain menu items will end up under the Python menu item instead of where expected, I saw some of this in my own experiments. When I expanded my code and used some of the reserved FILE event codes instead of the standard ones, things worked better.
#!/Library/Frameworks/Python.framework/Versions/3.7/bin/python3.7 # -*- coding: utf-8 -*-# -*- coding: utf-8 -*- from tkinter import * class App(Frame): def __init__(self, master): Frame.__init__(self, master) self.grid() self.widgets() def widgets(self): menubar = Menu(root) menubar.add_command(label = 'File') menubar.add_command(label = 'quit', command = root.quit()) root.config(menu = menubar) root = Tk() root.title('Menubar') app = App(root) root.mainloop()
Tk Tutorial - Menus, Tkinter's Menu widgets are completely independent units, and don't Menu widget for each submenu, and one more for the menu bar itself. This is an advanced menu buttons on python using Tkinter. Menu is a GUI tool where you will get some buttons and menu bars which has their own functions. As you can see that menu is usually for making form or any kind of list where you can get multiple functions. So, it is mostly made by the developers for any kind of software or program where users can easily interact. Menu is mostly for user interaction.
tkinter menu bars, assigning children to parents , Menus: Part of a Modern Tk Tutorial for Tcl, Ruby, Python and Perl. This chapter describes how to handle menubars and popup menus in Tk. For a polished If you don't specify a menubar for a particular window, Tk will use the menubar Menus in Tkinter (GUI Programming) The tkinter menu is a top-level pulldown menu. They are shown just under the title bar, as you’d expect from traditional gui apps. The menu can have multiple sub menus and each sub menu can contain items. Menu items can be associated with callback methods, meaning when you click them a Python method is called.
Mac OS bug with Menu_Demo · Issue #2068 · PySimpleGUI , tkinter menu bars, assigning children to parents, and grid/pack managers I think the problem is that I don't know how to assign a widget as a Menus in Tkinter applications are handled entirely by the Menu widget. As shown in the recipe, you use Menu both for the top-level menu bar (which you add to a top-level window as its menu configuration setting) and for cascading menus (which you add to the menu bar, or to other menus, with the add_cascade method).
Section 8.2. Menus, root.title('Test Tk menu on mac Catalina Python 3.7.4 tkinter 8.6') of software releases (that are not PySimpleGUI) to see which work and which don't. The demo that is failing for you uses a real "Menu Bar" style menu. Creates a radio button menu item. 3: add_checkbutton( options ) Creates a check button menu item. 4: add_cascade(options) Creates a new hierarchical menu by associating a given menu to a parent menu. 5: add_separator() Adds a separator line to the menu. 6: add( type, options ) Adds a specific type of menu item to the menu. 7: delete( startindex
- Your example never creates self.master, is this your actual code?
- Thanks for the reply! This isn't actually my code, it's someone else's from one of the tutorials I was using, but they both suffer from the same problem so I figured it'd be better to paste this one. I'll edit my post to include mine.
- Also you're right, I hadn't noticed that about self.master - guess it was written in a hurry! If I put a root=Tk(), put root as an argument of App's init (and feed it into the last line), put a self.master=root immediately into init and put root.mainloop() at the end of the script, I still find the same result...
- @user2163043 Can you please post the tutorial? I am assuming that it is online and it would greatly aid us in helping you out.
- @user2163043 Solved. Fun problem - where can I find out more about
Menu's? Also, if my answer was helpful please click the green checkmark next to it. This will help others recognize this as potentially helpful.
- @user2163043 If my answer was at all helpful to you, please give me the green check mark. It will help others see that this is a good answer. If not, please tell me the issue with my answer.
- Wow, hi thanks for your answer! Strangely though when I run it, I don't actually get a pop-up gui at all - python launcher runs, but brings up no window. If I place in a root.geometry("250x300+300+300") after root.title however, it does the same thing as before - bring up an empty white frame (this line was in my original code too - sorry forgot to mention it, have edited my question now). Very strange. The tutorial is here: skriticos.blogspot.co.uk/2009/07/…. Thanks for your help!
- @user2163043 I have a mac also - I will run the program there as well. So... Does the menubar show up or not? It worked well for me... What could have happened is that the Mac Menubar shows up in the upper bar, the area next to the apple sign and your program name. That, is actually almost certainly what happened.
- Ahhhhh ok I feel quite stupid now, yes that is what happened! The menu was there all along but after 4 years of owning a Mac I still didn't think to look for it in the OS X menu bar. Great. Thanks so much for your help I definitely wouldn't have found it otherwise!
- @user2163043 Yea - SO is great. I have learned so much since I joined. Try answering some questions - oftentimes you learn more from answering than asking. Anyways, good luck on your project.
- Correct, but this was also mentioned in the comments of the other answer before you posted this.
- still +1, because THIS is the answer (had the same problem)
- OMG! I feel so foolish right now. I may never get used to using a Mac! Thank you so much for pointing out the obvious.