Cannot start Tkinter window in Visual Studio with

2020-04-05 06:51发布

问题:

I am developing using Python Tools for Visual Studio in Visual Studio 2013 community edition on Windows 8.1. My problem is that I am unable to get a Tkinter window to start. I have tried using this code:

 from tkinter import * 
 Tk()

When I launch this code from IDLE and such, I am able to get a tkinter window, as shown:

However, when I start this in Visual Studio, no Tkinter window appears, only the console window. No error is thrown. Example:

How do I get the Tkinter window to appear when I launch the program in Visual Studio with Python tools?

Edit: Also, when I try to do this from the Python interactive window in VS, this is what I get, with no window appearing:

>>> from tkinter import *
>>> Tk()
<tkinter.Tk object at 0x02D81FD0>

回答1:

Most likely the problem is that you aren't starting the event loop. Without the event loop the program will exit immediately. Try altering your program to look like this:

import tkinter as tk
root = tk.Tk()
root.mainloop()

The reason you don't need to call mainloop in IDLE is because IDLE does that for you. In all other cases you must call mainloop.