0

My problem is when I write import tkinter(with the small t) it shows import error. And when I make it capital 'T' : import Tkinter it works but it pops file dialog error! Please help! I am running python 2.7.8 on windows 7 64 bit

My error ( when I use capital T for Tkinter)

Traceback (most recent call last):
  File "C:\Users\Ashim\Desktop\plotlib.py", line 5, in <module>
    from Tkinter.filedialog import askopenfilename
ImportError: No module named filedialog
nbro
  • 15,395
  • 32
  • 113
  • 196
ap2051
  • 149
  • 1
  • 8
  • whilst wildcard imports are allowed, for clarity it is best to avoid then: [wildcard imports](https://youtu.be/EueSSAyO3K0) – D.L Aug 10 '23 at 08:07

3 Answers3

2

On python 2.x tkinter is called Tkinter, so one has to import it using import Tkinter. On python 3.x the lower case is adopted.

Projects targeting the 2.x series will therefore have Tkinter, while those targeting 3.x series will use lower case, and projects targetting both python versions will check the version and import the appropriate library.

edit: tkinter.filedialog is specific to python3

On python 2.x the import should read import tkFileDialog

Haleemur Ali
  • 26,718
  • 5
  • 61
  • 85
  • Thank you for the excellent clarification! You surely deserve the green check! and i have updated my problem! – ap2051 May 16 '15 at 02:23
  • @AshimPaudel, thank's for posting your error msg, please see the updated answer for clarification on your issue. *you might be better off just using python 3.x* for this project because it seems the code you have is specific to the 3 series, and not designed to work on both. – Haleemur Ali May 16 '15 at 02:27
  • So, If I am planning to make a drag drop for the file input instead of raw_input, what will be my alternative on python 2.7.8? can you give me link to tutorial or previously answered thread! – ap2051 May 16 '15 at 02:31
1

I don't know what dialog you're seeing, perhaps you can give more information on that.

The reason the capitalization matters is because python is case-sensitive. That means that upper and lower case letters are different from each other. So 'tkinter' is not the same as 'Tkinter' If the strings don't match exactly, they are not the same.

Eric Renouf
  • 13,950
  • 3
  • 45
  • 67
  • Thank you very much for the clarification; I thought of that but was unsure.....the error that pops out is.... Traceback (most recent call last): File "C:\Users\Ashim\Desktop\plotlib.py", line 5, in from Tkinter.filedialog import askopenfilename ImportError: No module named filedialog – ap2051 May 16 '15 at 02:19
  • I think filedialog is a Python 3 thing, you may want to use Python 3 if you're trying to use tkinter and filedialog. – Eric Renouf May 16 '15 at 02:22
  • Thank you so much for the clarification! That helps a lot! So, If I am planning to make a drag drop for the file input instead of raw_input, what will be my alternative on python 2.7.8? can you give me link to tutorial or previously answered thread! – ap2051 May 16 '15 at 02:24
  • @AshimPaudel sorry, I actually don't do much GUI stuff, so I don't actually have any advice for you I'm betting on Haleemur coming through for you. If you accepted his answer, you probably want to upvote him too! – Eric Renouf May 16 '15 at 02:44
1

In python2 the module is called Tkinter. In python3 it is called tkinter. Don't ask me why it's just how it is.

Also there are certain classes that don't get imported when you try to import with *. When working with tkinter, I usually do a from Tkinter import *

Anatzum
  • 79
  • 8
  • Thanks for the information! I was confused because of the python 2 and 3 thing! Dont worry i wont ask you WHY???? – ap2051 May 16 '15 at 02:27