[PYTHON] Environment variables when using Tkinter

What to do if you get an error like this because the Tcl / Tk library is not found when you try to use Tkinter in a Windows environment.

    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: Can't find a usable tk.tcl in the following directories: 
    {C:\Python27\tcl\tk8.5} C:/Python27/tcl/tcl8.5/tk8.5 C:/lib/tk8.5 C:/library

If you specify TCL_LIBRARY and TK_LIBRARY environment variables, it will work. If you have multiple versions of Python or Tcl / Tk and you don't want to mess with environment variables, you can mess with os.environ directly at the beginning of the program.

Sample program

# -*- coding: utf-8 -*-
__author__ = 'Natsutani'

import os
from matplotlib.pyplot import *;

def main():
    #Setting environment variables
    os.environ['TCL_LIBRARY'] = 'C:/Python32/tcl/tcl8.5'
    os.environ['TK_LIBRARY'] = 'C:/Python32/tcl/tk8.5'

    x = (5,11,3,5)
    y = (3,5,3,5)
    scatter(x,y)
    show()

if __name__ == "__main__":
    main()

Recommended Posts

Environment variables when using Tkinter
Try using Tkinter
Periodic execution processing when using tkinter [Python3]
Shell variables, environment variables
Use pydantic when reading environment variables in Python
[Python] Get environment variables
Summary when using Fabric
What are environment variables?
Try using Python's Tkinter
Precautions when using Chainer
(Personal) points when using ctypes
When using optparse with iPython
Handle Go environment variables (viper)
What are environment variables? (Linux)
Python notes using perl-special variables
Handle environment variables in Python
A story that stumbled when using pip in a proxy environment
HTTP environment variables in Flask
# 1 [python3] Simple calculation using variables
DEBUG settings when using Django
When using if and when using while
File structure when using serverless-python-requirements
Use configparser when using API
Test discovery fails when using tensorflow in vscode + pytest environment
Set environment variables with lambda-uploader
Small speedup when using pytorch
PATH when using ANACONDA virtual environment with Pycharm (as of Mac 2020/10/03)
Debug settings in virtual environment when using Pipenv with VS Code
Initializing global variables using Python decorators
A memorandum when using beautiful soup
Variable scope when using internal functions
Proxy measures when using WEB API
Precautions when using pit in Python
Precautions when using TextBlob trait analysis
When pip fails under proxy environment
Error when building mac python environment
Create a python GUI using tkinter
Build a go environment using Docker
Precautions when using codecs and pandas
Precautions when using the urllib.parse.quote function
Using global variables in python functions
Try giving AWS Lambda environment variables?
[Python] Be careful when using print
Using Chainer with CentOS7 [Environment construction]
Precautions when using phantomjs from python
ResourceWarning when using requests: unclosed workaround
When using MeCab with virtualenv python
Precautions when using six with Python 2.5
Read system environment variables with python-Part 1
[VS Code] ~ Tips when using python ~
When using regular expressions in Python
Python3.6 environment construction (using Win environment Anaconda)
Read system environment variables with python-Part 2
GUI creation in python using tkinter 2
Notify using Notification Center when the execution environment is macOS in Python