1

The following is a small python3 script from a web tutorial that demonstrates how to make pulldown menubars using Tkinter.

It is copied directly from https://www.tutorialspoint.com/python3/tk_menu.htm with no changes!

  • It works fine when I run it on a raspberry pi (using 3 different IDEs)
  • BUT when I run it on my Mac (in 2 different IDEs) it just creates the white window and places no menu elements into it.

  • NOTE that this Mac is able to run other simple Tkinter scripts that I have been making as I try to learn Tkinter... making buttons, popups, no problem.

New Edit: When I go to the macs that this is failing on, and I do:

    >>> import tkinter
    >>> tkinter._test()

...the test works fine with its little buttons etc, and I get the little window telling me it's v8.5.

What is it about this script that is making the mac choke (and the other computer is not)? Huge thanks!

Here is the tkinter script that fails on the macs:

# !/usr/bin/python3
from tkinter import *
def donothing():
   filewin = Toplevel(root)
   button = Button(filewin, text="Do nothing button")
   button.pack()

root = Tk()
menubar = Menu(root)
filemenu = Menu(menubar, tearoff = 0)
filemenu.add_command(label="New", command = donothing)
filemenu.add_command(label = "Open", command = donothing)
filemenu.add_command(label = "Save", command = donothing)
filemenu.add_command(label = "Save as...", command = donothing)
filemenu.add_command(label = "Close", command = donothing)

filemenu.add_separator()

filemenu.add_command(label = "Exit", command = root.quit)
menubar.add_cascade(label = "File", menu = filemenu)
editmenu = Menu(menubar, tearoff=0)
editmenu.add_command(label = "Undo", command = donothing)

editmenu.add_separator()

editmenu.add_command(label = "Cut", command = donothing)
editmenu.add_command(label = "Copy", command = donothing)
editmenu.add_command(label = "Paste", command = donothing)
editmenu.add_command(label = "Delete", command = donothing)
editmenu.add_command(label = "Select All", command = donothing)

menubar.add_cascade(label = "Edit", menu = editmenu)
helpmenu = Menu(menubar, tearoff=0)
helpmenu.add_command(label = "Help Index", command = donothing)
helpmenu.add_command(label = "About...", command = donothing)
menubar.add_cascade(label = "Help", menu = helpmenu)

root.config(menu = menubar)
root.mainloop()
13
  • What versions of tkinter do you have in raspberrypi and macos? Commented Jun 9, 2018 at 2:26
  • Hashbang! If this is copied directly from the script, the hashbang identifier is incorrect and will run in the system installation of python, which IIRC for MacOS is Python2, not Python3. Commented Jun 9, 2018 at 2:40
  • Hi @Alan -- you know, i thought that also, but when I totally delete the hashbang file when on the mac, the file still will not run right, same "blank white window that still appears to be running its mainloop as it announces it's done when I close it" issue. (and all those other Tkinter scripts that run OK on the mac don't have a hashbang.) Commented Jun 9, 2018 at 3:03
  • @eyllanesc -- I will go and try to learn the versions of tkinter. Embarrassed to say I'm not confident of my way to find out those version numbers in either setting -- I will try now. Commented Jun 9, 2018 at 3:04
  • Yes, because the hashbang has a space in it ... so it is being ignored. Try correcting it to #! /usr/bin/python3 Commented Jun 9, 2018 at 3:04

1 Answer 1

1

BUT when I run it on my Mac (in 2 different IDEs) it just creates the white window and places no menu elements into it.

Great, it's working perfectly!

The window itself is not where menus go on the Mac - look at the top of the screen, just like every other Mac application.

Sign up to request clarification or add additional context in comments.

1 Comment

HAhahahahahaha!!! OK, you win! :) Thanks much. Not so often that an answer is right in front of me quite like that. Thanks... this had me going to about 10 different sources... :) And I got distracted by various reported issues about 8.5 having incompatibility on the mac... and how the mac guidelines rule out certain TK menu functions... when in reality, it was right there. Thanks! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.