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>
Python Distributions with Tkinter The simplest method to install Tkinter in a Windows environment is to download and install either ActivePython 3.8 or 3.7 from here. Alternatively, you can create and activate a Conda environment with Python 3.7 or greater that is integrated with the latest version of Tkinter.
Tkinter is widely available for all operating systems. It is pre-install in the Python. To work with the Tkinter, we must install the Python.
What is Tkinter used for and how to install it? Tkinter is the de facto way in Python to create Graphical User interfaces (GUIs) and is included in all standard Python Distributions. In fact, it's the only framework built into the Python standard library.
On ms-windows, python programs using tkinter should have the extension .pyw. And this extension should be associated with pythonw.exe rather than python.exe. Using pythonw.exe will prevent the cmd.exe window from appearing when your python script has a GUI.
For every Python environment known to Visual Studio, you can easily open the same interactive (REPL) environment for a Python interpreter directly within Visual Studio, rather than using a separate command prompt. You can easily switch between environments as well.
Use the Open interactive window command to run Python interactively within the context of Visual Studio. Use the Open in PowerShell command to open a separate command window in the folder of the selected environment.
Visual Studio's Python Environments window (shown below in a wide, expanded view) gives you a single place to manage all of your global Python environments, conda environments, and virtual environments.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With