Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot start Tkinter window in Visual Studio with Python Tools

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:

tkinter in idle

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

tkinter in vs

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>
like image 732
Jake Avatar asked Dec 02 '14 15:12

Jake


People also ask

How do I enable Tkinter in Python?

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.

Is Tkinter already installed in Python?

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.

Is Tkinter a default Python library?

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.

How do I prevent CMD from appearing when using Tkinter?

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.

How do I open a Python interpreter in Visual Studio?

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.

How do I run Python in Visual Studio Code?

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.

What is the Python environments window in Visual Studio?

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.


1 Answers

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.

like image 88
Bryan Oakley Avatar answered Nov 15 '22 08:11

Bryan Oakley