Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7024055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:47:24+00:00 2026-05-27T23:47:24+00:00

Every tkinter tutorial I have seen claims that tkinter.mainloop must be called for windows

  • 0

Every tkinter tutorial I have seen claims that tkinter.mainloop must be called for windows to be drawn and events to be processed, and they always call this function, even in hello world programs. However, when I try these out in the interactive shell, windows are drawn correctly without having to call mainloop. This example of embedding matplotlib graphics in tkinter produces a relatively complex application, with buttons for panning, zooming and resizing a plot within a tkinter window, and again, this all works if you remove the call to mainloop and run the code in the interactive shell. Of course, if I run the script (with mainloop removed) outside the interactive shell, the program ends too quickly to see what happens, but if I add a call to input to hold the program open everything works correctly (I’m running python 3.2.2 on linux).

So what exactly does mainloop do, and when is it necessary to call it?

EDIT:
To clarify, if I open up the GNOME terminal and type

$python3
>>> import tkinter
>>> root = tkinter.Tk()

a window immediately appears without having to call mainloop, and more complex tkinter functionality seems to work as well (for example, adding buttons to the window). In IDLE, a call to mainloop is necessary. It was my understanding that nothing should be drawn, and no events should be processed, until mainloop is called.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:47:25+00:00Added an answer on May 27, 2026 at 11:47 pm

    The answer to your main question is, you must call mainloop once and only once, when you are ready for your application to run.

    mainloop is not much more than an infinite loop that looks roughly like this (those aren’t the actual names of the methods, the names merely serve to illustrate the point):

    while True:
        event=wait_for_event()
        event.process()
        if main_window_has_been_destroyed(): 
            break
    

    In this context, “event” means both the user interactions (mouse clicks, key presses, etc) and requests from the toolkit or the OS/window manager to draw or redraw a widget. If that loop isn’t running, the events don’t get processed. If the events don’t get processed, nothing will appear on the screen and your program will likely exit unless you have your own infinite loop running.

    So, why don’t you need to call this interactively? That’s just a convenience, because otherwise it would be impossible to enter any commands once you call mainloop since mainloop runs until the main window is destroyed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Every Java circular byte buffer implementation I have seen referenced on SO and elsewhere
I have a function that will graph a 3D sphere with matplotlib in tkinter.
Every time I turn on my company-owned development machine, I have to kill 10+
Every time I create an object that has a collection property I go back
Every time I have to estimate time for a project (or review someone else's
I have a Python script which uses Tkinter for the GUI. My little script
Every single site says to use the settings in viewport but they flat out,
Every time I try compile a VB project that uses a Delphi (2007) activex,
Every time I test my program, I have to input a bunch of the
Every time that I want to do a Layout, I'm getting a black layout

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.