Python tkinter 8.5 import messagebox

前端 未结 2 1634
别跟我提以往
别跟我提以往 2020-12-20 03:02

The following code runs fine within IDLE, but otherwise I get \"NameError: global name \'messagebox\' is not defined\". However, if I explicitly state from tkinter imp

相关标签:
2条回答
  • 2020-12-20 03:17

    the messagebox is a separate submodule of tkinter, so simply doing a complete import from tkinter:

    from tkinter import *
    

    doesn't import messagebox

    it has to be explicitly imported like so:

    from tkinter import messagebox
    

    in the same way that ttk has to be imported explicitly

    the reason it works in idle is because idle imports messagebox for its own purposes, and because of the way idle works, its imports are accessible while working in idle

    0 讨论(0)
  • 2020-12-20 03:24

    IDLE is written in Python and uses Tkinter for the GUI, so it looks like your program is using the import statements that IDLE itself is using. However, you should explicitly include the import statement for the messagebox if you want to execute your program outside the IDLE process.

    0 讨论(0)
提交回复
热议问题