Tkinter code Works in Windows but not in Mac - python

Why does the following test not display the menu on my Mac running Python 3.7 on Mojave?
Tried a shebang but that didn't help.
import tkinter as tk
def quit_app():
my_window.destroy()
my_window = tk.Tk()
my_menu = tk.Menu(my_window)
my_menu.add_command(label='Quit',command=quit_app)
my_window.config(menu=my_menu)
my_window.mainloop()
The menu displays in Windows 10 but not on the Mac. The tkinter window is blank on the Mac.

The menu displays in Windows 10 but not on the Mac
Yes, that is because MAC has a design philosophy that all devs for their platform have to conform with.
You cannot have a add_command as a top level menu item on mac, rather:
import tkinter as tk
def quit_app():
my_window.destroy()
my_window = tk.Tk()
my_menu = tk.Menu(my_window)
quit_menu= tk.Menu(my_menu, tearoff=0)
quit_menu.add_command(label='Quit App',command=quit_app)
my_menu.add_cascade(label="Quit", menu=quit_menu, underline=0)
my_window.config(menu=my_menu)
my_window.mainloop()

Related

How will I make a tk window which cannot be minimized in Python Tkinter?

I tried win.overrideredirect(True) it didn't work...
My Code goes like this:
from tkinter import *
win = Tk()
win.resizable(0,0)
win.wm_protocol("WM_SAVE_YOURSELF", lambda: print("On exit"))
Label(win, text="Tk Window").pack()
win.mainloop()
Specs:
Python 3.9.6 [Latest],
Pip 21.1.3,
OS: Windows 10 Home
I want to make the minimize button to be disabled...
Please help
The thing #N1CK145 posted didn't work for me but I am guessing that he/she tried to do this:
import Tkinter as tk
root = tk.Tk()
root.attributes("-toolwindow", True)
root.mainloop()
If you want to know more attribute options look here
try resizeable function like this :
win= Tk()
win.geometry("750x250")
win.resizable(False, False)
Found this here:
import Tkinter as tk
root= tk.Tk()
root.title("wm min/max")
# this removes the maximize button
root.resizable(0,0)
# # if on MS Windows, this might do the trick,
# # but I wouldn't know:
# root.attributes(toolwindow=1)
# # for no window manager decorations at all:
# root.overrideredirect(1)
# # useful for something like a splash screen
root.mainloop()

tkinter using default Mac theme

i am working on a tkinter app and I just updated to the latest version of python(python 3.9.5) and tkinter is using my mac's default theme, which doesn't go with my design.
i want this(the light theme):
but what am getting is(the dark theme):
is there a way I can change the theme to light or to check the current mac theme?
my code:
from tkinter import *
class Snipper:
def __init__(self):
self.root = Tk()
self.root.title("snipper")
self.build()
# bring app to front after start
self.root.lift()
self.root.attributes('-topmost',True)
self.root.after_idle(self.root.attributes,'-topmost',False)
self.root.minsize(1000, 600)
self.root.mainloop()
s = Snipper()

Why always have to force quit python launcher after running Tkinter on Mac?

I'm using jupyter notebook on mac, recently I need to write interactive dialog box, so after google, I use Tkinter to make an interactive window.
But I was bothered by this problem couples day ,and still can't find a solution way.
Fisrt example:
from Tkinter import *
from tkFileDialog import askopenfilename
import sys
import os,time
def callback():
name= askopenfilename()
print name
errmsg = 'Error!'
Button(text='File Open', command=callback).pack(fill=X)
mainloop()
Second example:
from Tkinter import *
import sys,os
class YourApp(Tk):
def quit_and_close(self):
app.quit()
#os._exit(0)
#sys.exit(1)
#exit(0)
app = YourApp()
app.title('example')
app.geometry('400x300+200+200')
b = Button(app, text = "quit", command = app.quit_and_close)
b.pack()
app.mainloop()
And the third one:
import Tkinter as tk
import tkMessageBox
def ask_quit():
if tkMessageBox.askokcancel("Quit", "You want to quit now? *sniff*"):
root.destroy()
root = tk.Tk()
root.protocol("WM_DELETE_WINDOW", ask_quit)
root.mainloop()
After running those above code, always need have to force quit python launcher.
It is very weird, and annoying because after forcing quit, I will got the error:
Is it necessary to use python launcher as default window?
Is there possible to set another window to open ?
or is there proper way to close the launcher without causing programming crash?
p.s Even I try to use wxpython, it still open python launcher and got the same problem.

MacOSX Tkinter no response when Tk()

I have a TkButton.py like this
from Tkinter import *
root = Tk()
button = Button(root, text = 'Python/Tkinter')
button.pack()
root.mainloop()
But when I execute python2.7 TkButton.py, it just stuck.
I installed Python2.7 and py27-tkinter using MacPorts.
My OS is OS X 10.11.6
Can somebody help me?
Addition
Tkinter.Tk() not working. I don't know why.
And I can't write any command after root = tk.Tk(), so I have to Ctrl-C to quit.

Tkinter right-click popup unresponsive on OSX

I have been looking for a way to display a right-click popup menu on OSX.
So far all of my attempts have been unsuccessful. The same code will work fine on a Linux VM(Ubuntu).
For arguments sake I copied the code written in these two pages and tried to run them on my machine.
tkinter app adding a right click context menu?
http://effbot.org/zone/tkinter-popup-menu.htm
Neither have worked in the way I expect them to on OSX but they do when I run them on an Ubuntu VM.
The machine I am using is a Mac Mini4,1 running OSX 10.6.8.
Has anyone else experienced this and is there a viable workaround?
For odd historical reasons, the right button is button 2 on the Mac, but 3 on unix and windows.
Here is an example that works on my OSX box:
try:
# python 2.x
import Tkinter as tk
except ImportError:
# python 3.x
import tkinter as tk
class Example(tk.Frame):
def __init__(self, *args, **kwargs):
tk.Frame.__init__(self, *args, **kwargs)
self.popupMenu = tk.Menu(self, tearoff=0)
self.popupMenu.add_command(label="One", command=self.menu_one)
self.popupMenu.add_command(label="Two", command=self.menu_two)
self.popupMenu.add_command(label="Three", command=self.menu_three)
self.bind("<Button-2>", self.popup)
def menu_one(self):
print "one..."
def menu_two(self):
print "two..."
def menu_three(self):
print "three..."
def popup(self, event):
self.popupMenu.post(event.x_root, event.y_root)
if __name__ == "__main__":
root =tk.Tk()
frame = Example(root, width=200, height=200)
frame.pack(fill="both", expand=True)
root.mainloop()
Because of the MacOS's mouse button index is different from Windows or Linux .You can try this in your code:
MAC_OS = False
if sys.platform == 'darwin':
MAC_OS = True
if MAC_OS:
self.bind('<Button-2>', self.context_popup)
else:
self.bind('<Button-3>', self.context_popup)

Categories

Resources