无法使用Python工具在Visual Studio中启动Tkinter窗口 [英] Cannot start Tkinter window in Visual Studio with Python Tools
问题描述
我正在使用Windows 8.1上Visual Studio 2013社区版中的Visual Studio Python工具进行开发.我的问题是我无法启动Tkinter窗口.我尝试使用此代码:
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()
当我从IDLE等启动此代码时,我可以得到一个tkinter窗口,如下所示:
When I launch this code from IDLE and such, I am able to get a tkinter window, as shown:
但是,当我在Visual Studio中启动它时,没有Tkinter窗口出现,只有控制台窗口出现.没有引发任何错误.示例:
However, when I start this in Visual Studio, no Tkinter window appears, only the console window. No error is thrown. Example:
当我使用Python工具在Visual Studio中启动程序时,如何显示Tkinter窗口?
How do I get the Tkinter window to appear when I launch the program in Visual Studio with Python tools?
另外,当我尝试从VS中的Python交互式窗口执行此操作时,这就是我得到的,没有出现窗口:
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>
推荐答案
最有可能的问题是,您没有启动事件循环.没有事件循环,程序将立即退出.尝试更改您的程序,使其看起来像这样:
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()
您不需要在IDLE中调用mainloop的原因是因为IDLE为您执行了此操作.在所有其他情况下,您必须调用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.
这篇关于无法使用Python工具在Visual Studio中启动Tkinter窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!