0

I used to be on CentOS, but had to change to Ubuntu.

I recently installed Python 3.5 and followed instructions from here: https://passingcuriosity.com/2015/installing-python-from-source/

I'm trying to write a simple GUI, yet it is saying that module Tkinter is not loaded. Here are bits from my terminal:

adminJerry@admin:~/Desktop$ python -m tkinter

/usr/local/bin/python3: Error while finding spec for 'tkinter.main' (: No module named '_tkinter'); 'tkinter' is a package and cannot be directly executed

-

adminJerry@admin:~/Desktop$ from tkinter import *

from: can't read /var/mail/tkinter

My code in the Python script mainly consists of this:

import Tkinter

*** Various labels and buttons made only to test it out ***

When I run my script:

adminJerry@admin:~/Desktop$ python test.py

Traceback (most recent call last): File "test.py", line 3, in import Tkinter ImportError: No module named 'Tkinter'

EDIT: Reinstalling Ubuntu fixed my issue.

James
  • 1,928
  • 3
  • 13
  • 30
John Doe
  • 25
  • 1
  • 9
  • 1
    Why have you installed it from source? It's pre-installed on Ubuntu, and even if not -- it's available as .deb packages. Installing from source is not a good idea, cause it breaks dependencies, and seems like some dependencies for tk lib have been broken in your case. Anyway, package is called `Tkinter` in python 2.x and `tkinter` in 3.x, try replacing in import – thodnev Aug 22 '16 at 18:10
  • @thodnev I installed from source because at first, I didn't even know Ubuntu had Python pre-installed. I see Python 2.7 and 3.5, but i think only was already installed, not 3.5. Do you have an idea for a fix? Do i have to simply reinstall Ubuntu, or just delete all files for Python 3.5? Thanks – John Doe Aug 22 '16 at 18:15
  • Reinstalling Ubuntu would be the cleanest way to go. – albert Aug 22 '16 at 18:23
  • @JohnDoe depends on how much time you spent customizing OS. I think reinstalling python from packages would be enough, however, it may leave some trash in your system. The 100% method is to reinstall Ubuntu, as albert said – thodnev Aug 22 '16 at 19:43
  • Possible duplicate of [Which tkinter modules were renamed in Python 3?](https://stackoverflow.com/questions/673174/which-tkinter-modules-were-renamed-in-python-3) – Stevoisiak Mar 30 '18 at 19:28

5 Answers5

2

There are many solutions to your problem that you could try.

1.Use:

import tkinter

Since Python 3.0, Tkinter has been renamed to tkinter. In Python 2.7, it was imported with a capital. When using this, you may want to make tkinter another namespace as it will save time. For example:

import tkinter as tk

test = tk.Button(label="Hello World!")

Instead of:

import tkinter

test = tkinter.Button(label="Hello World!")

2.Use:

from tkinter import *

This will import everything in tkinter. It was not specified of what to import. When using this, bear in mind that when you instantiate widgets, you use:

[variable name] = [widget name]([widget args])

An example would be:

test = Button(label="Hello World!")

3.Install Python again

Explore the idea of reinstalling Python using the installer (on Windows anyway) or using the package manager. You seemed to have installed the source which resulted in cutting some modules out.

You also seemed to have installed core Ubuntu. This may have excluded Python which had resulted you installing the source. If you can, try installing the full Ubuntu. It can be downloaded here.

Please take into consideration that I have tried my best to explain everything but some information could be incorrect. Please feel free to contact me if you find such errors.

  • Point number two is valid, but not recommended because of namespace pollution / name collisions. And no need to go full if just missing some packages. – progmatico Mar 24 '18 at 17:10
2

The error is that you are using a capital 'T' in tkinter, instead try :

from tkinter import *
Button(gui, text='HELLO WORLD', fg='#F08080', font = ("Courier", 15, 'bold'))

This should work, hope you find this useful.

Quirtz
  • 21
  • 1
0

Try typing import tkinter or from tkinter import * instead of import Tkinter

Sᴀᴍ Onᴇᴌᴀ
  • 8,218
  • 8
  • 36
  • 58
0

It's python3 that you are using. so,

import tkinter

If you were using python2, then:

import Tkinter
Eshita Shukla
  • 791
  • 1
  • 8
  • 30
0

In python 3, it was renamed to tkinter. It's no longer named Tkinter.

  • Thanks for trying to help, but if you look at the bottom of the question, he said in bold that he has already solved it, and what he did that worked :/ – Lionel Foxcroft Jan 30 '21 at 21:05