Running Tkinter on Mac

2024/10/13 22:25:16

I am an absolute newbie. I'm trying to make Python GUI for my school project so I decided to use Tkinter. When I try to import Tkinter it throws this message:

>>> import tkinter
Traceback (most recent call last):File "<stdin>", line 1, in <module>File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 36, in <module>import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'

I tried to find a solution online but I couldn't figure it out (mostly didn't understand it).

I read about some problem with directory in setup.py but I don't understand how to fix it. I have tkinter folder in my python3.7 folder.

I don't really understand these steps that I found:

If it fails with "No module named _tkinter", your Python configuration needs to be modified to include this module (which is an extension module implemented in C). Do not edit Modules/Setup (it is out of date). You may have to install Tcl and Tk (when using RPM, install the -devel RPMs as well) and/or edit the setup.py script to point to the right locations where Tcl/Tk is installed. If you install Tcl/Tk in the default locations, simply rerunning "make" should build the _tkinter extension.

I'm using Mac OS and use Visual Studio Code.

Answer

To check your python version, run this on terminal:

$ python --version

Check what python your machine is using. Run:

$ which python

If it's using python from Homebrew, it's probably using Python 2. If you installed python 3 manually, just install tKinter manually.

$ brew install python-tk

To run python 2 manually, run on terminal:

$ python <FILENAME>

If python 3, run:

$ python3 <FILENAME>
https://en.xdnf.cn/q/69484.html

Related Q&A

Object-based default value in SQLAlchemy declarative

With SQLAlchemy, it is possible to add a default value to every function. As I understand it, this may also be a callable (either without any arguments or with an optional ExecutionContext argument).No…

High Resolution Image of a Graph using NetworkX and Matplotlib

I have a python code to generate a random graph of 300 nodes and 200 edges and display itimport networkx as nx import matplotlib.pyplot as pltG = nx.gnm_random_graph(300,200) graph_pos = nx.spring_layo…

how to read an outputted fortran binary NxNxN matrix into Python

I wrote out a matrix in Fortran as follows:real(kind=kind(0.0d0)), dimension(256,256,256) :: dense[...CALCULATION...]inquire(iolength=reclen)dense open(unit=8,file=fname,& form=unformatted,access=d…

python argparse subcommand with dependency and conflict

I want to use argparse to build a tool with subcommand. The possible syntax could be/tool.py download --from 1234 --interval 60 /tool.py download --build 1432 /tool.py clean --numbers 10So I want to us…

Running django project without django installation

I have developed a project with Django framework(python and mysql DB) in Linux OS(Ubuntu 12.04), I want to run this project in localhost in another machine with Linux(Ubuntu 12.04) without installing D…

redefine __and__ operator

Why I cant redefine the __and__ operator?class Cut(object):def __init__(self, cut):self.cut = cutdef __and__(self, other):return Cut("(" + self.cut + ") && (" + other.cut +…

How to find class of bound method during class construction in Python 3.1?

i want to write a decorator that enables methods of classes to become visible to other parties; the problem i am describing is, however, independent of that detail. the code will look roughly like this…

Multi-Threaded NLP with Spacy pipe

Im trying to apply Spacy NLP (Natural Language Processing) pipline to a big text file like Wikipedia Dump. Here is my code based on Spacys documentation example:from spacy.en import Englishinput = open…

Django Tastypie throws a maximum recursion depth exceeded when full=True on reverse relation.

I get a maximum recursion depth exceeded if a run the code below: from tastypie import fields, utils from tastypie.resources import ModelResource from core.models import Project, Clientclass ClientReso…

Adding a colorbar to two subplots with equal aspect ratios

Im trying to add a colorbar to a plot consisting of two subplots with equal aspect ratios, i.e. with set_aspect(equal):The code used to create this plot can be found in this IPython notebook.The image …