Search code examples
pythonmacosvisual-studio-codetkinter

Tkinter window is not opening on Mac


I've just set up VSCode, installed Brew and Python.

I've just written some code to open a Tkinter window:

import tkinter

window = tkinter.Tk()
window.title("Ice calculator")

frame = tkinter.Frame(window)
frame.pack()

ice = tkinter.LabelFrame(frame)
ice.grid(row=0, column=0)

window.mainloop()

All it does is show a little rocket in my dock, but no window is opened.

I've tried installing Tkinter manually with Brew and but it didn't change anything, still throws this:

 DEPRECATION WARNING: The system version of Tk is deprecated and may be removed in a future release. Please don't rely on it. Set TK_SILENCE_DEPRECATION=1 to suppress this warning.

Solution

  • The window technically does appear, but it is very small because nothing is set to be displayed inside the window, and the window size is not set.

    Before the window.mainloop() line, you can add the following:

    window.geometry("300x200")  # Sets the window size to 300x200 pixels
    

    The above will make the size of the window be 300 by 200 pixels.

    Alternatively, you can simply add content to the window. For example, you could add the following:

    ice_label = tkinter.Label(ice, text="Welcome to Ice Calculator")
    ice_label.pack(padx=20, pady=20)
    

    Here's the full code

    import tkinter
    
    window = tkinter.Tk()
    window.title("Ice calculator")
    
    frame = tkinter.Frame(window)
    frame.pack()
    
    ice = tkinter.LabelFrame(frame)
    ice.grid(row=0, column=0)
    
    ice_label = tkinter.Label(ice, text="Welcome to Ice Calculator")
    ice_label.pack(padx=20, pady=20)
    
    # window.geometry("300x200")  # Sets the window size to 300x200 pixels
    
    window.mainloop()