Tkinter cannot find filedialog? [duplicate] - python

This question already has answers here:
Tkinter import filedialog error
(4 answers)
Closed 5 years ago.
I'm having some issues using tkinter. To start, I'm using Python 2, and I've seen people on SO use a mixture of import tkinter and import Tkinter; only the latter works for me, although there is another module names _tkinter. Which am I supposed to use here?
The real issue is that tkinter cannot find filedialog. I'm trying to do path = Tkinter.filedialog.askopenfilename() but I'm getting an error AttributeError: 'module' object has no attribute 'filedialog'. What can I do about this?

Python 2's tkinter and Python 3's tkinter libraries have some differences.
Python 2:
import Tkinter
Python 3:
import tkinter
Python 2:
import tkFileDialog # ( `as filedialog` ) to import the filedialog module
Python 3:
from tkinter import filedialog
You have to use the Python 2 code samples. Every method call to tkinter should be the same in both Python versions. However you have to import different modules. For any further questions just search the wiki for your Python version (Python 2) or feel free to ask a new question here on Stack Overflow.

Related

Unsolved reference 'Tk'? [duplicate]

This question already has an answer here:
Unresolved reference 'tkinter' [duplicate]
(1 answer)
Closed 5 months ago.
I am trying to learn tkinter and how to make GUIs with it, but PyCharm is giving me an error as "Unresolved reference 'Tk' "
My code:
from tkinter import *
root = Tk()
It sounds like you have a file in your project called tkinter.py that's shadowing the tkinter module. (The path to the "real" tkinter would be something like Python/Python310/lib/tkinter/__init__.py, whereas the path tkinter.py in the error message indicates a file in your project's root path.)
To fix the problem, rename or move your tkinter.py file.

Error in GoogleColab (Python)- TclError: no display name and no $DISPLAY environment variable [duplicate]

This question already has answers here:
TclError: no display name and no $DISPLAY environment variable in Google Colab
(2 answers)
Closed 1 year ago.
Does anybody know how to fix the below error in Python in GoogleColab Notebook? I am running the below code:-
import matplotlib.pyplot as plt
from matplotlib import style
style.use('fivethirtyeight')
import tkinter as tk
from tkinter import filedialog
from tkinter import *
from PIL import ImageTk, Image
import numpy
import keras
from keras.models import load_model
from sklearn.metrics import accuracy_score
model = load_model('training_model.h5')
classes = { 1: 'Speed Limit 20 km/h',
2: 'Speed Limit 30 km/h',
3: 'Speed Limit 50 km/h' }
#initialize GUI
top = tk.Tk()
top.geometry('800x600')
top.title('Traffic Sign Recognition Project Report')
top.configure(background='#EEE')
label=Label(top,background='#CDCDCD', font=('arial',15,'bold'))
sign_image = Label(top)
I get the error on the line -> top = tk.Tk()
It is not possible to use tkinter on Google Colaboratory for alot of reasons. Firstly, even though if you manage to get it working by following here, as it mentions:
However, if you want to interact with the GUI, that's going to be hard, 'cuz Colab doesn't support interactive screens out of the box.
also from other answers there:
Servers generally don't even have a display. And even if they had, you wouldn't see it. You will have to run Python on your desktop or laptop to use tkinter.
I cannot close this question as a duplicate because the other question does not have a correct answer chosen.

Why import duplicates from a module [duplicate]

This question already has an answer here:
Why use Python code "from tkinter import ttk"? [duplicate]
(1 answer)
Closed 4 years ago.
I am learning tkinter and noticed that people import multiple things sometimes.
from tkinter import *
from tkinter import ttk
I was wondering why people do this for many modules, not just tkinter. I always thought that import * meant that you were importing everything from a module. So why do people import more items?
tkinter.ttk is a submodule of tkinter. Submodules aren't guaranteed to be loaded by import *; if you want them, it's safest to import them yourself. (Whether submodules are loaded by import * depends on the presence and contents of an __all__ list, as well as what imports __init__.py performs and what imports have already been performed.)

Why use Python code "from tkinter import ttk"? [duplicate]

This question already has answers here:
Python: `from x import *` not importing everything
(3 answers)
Closed 6 years ago.
Python applications using TK GUIs usually call the following import statements:
from tkinter import *
from tkinter import ttk
Isn't calling from tkinter import ttk redundant? I thought ttk would already be imported by calling from tkinter import *
Please explain why ttk needs to be imported separately?
(I already understand that it has "improved widgets". I want to understand why the improved widgets aren't accessible from the from tkinter import * call.)
If you go to your python installed location, you will find that in the python library, tkinter is a folder instead of a .py file. So when you use from tkinter import *, you actually only imported things in your-python-location/lib/tkinter/__init__.py. Things like ttk are actually separate files in tkinter folder(e.g. lib/tkinter/ttk.py, lib/tkinter/scrolledtext.py etc.) Therefore, from tkinter import * and from tkinter import tkk are different commands that import things from different module.

Python Tkinter - global name Tk() is not defined

I've been following a tutorial for the tkinter interface in python and it uses the following piece of code to declare a root widget for the program which then has children widgets:
root = Tk()
I'm getting the following error when trying to interpret this piece of code:
Global name Tk() is not defined
I'm fairly sure this is because tkinter has changed since the tutorial; I cannot find any other tutorials that do not use snippets of code like this so they wouldn't work either.
The question I have is in context simple, but searching, I cannot find the answer;
How can I bypass this: what has changed to the syntax of tkinter and what is the new method of sorts to declare a root widget? additionally it would be brilliant if anybody has the knowledge of tkinter to warn me whether the way in which you can add children widgets to the root has changed as well.
Thank you for any and all replies ~ Michael
You probably forgot from Tkinter import * at the top.
Alternatively, there's
import Tkinter
or
import Tkinter as tk
Edit: Generally, you want to use the idiom from <library> import <module>, so for your specific example from Tkinter import Tk would work.
which just allows you type tk.Button, for example, rather than Tkinter.Button throughout your code. And if you're using Python 3.x, the library is lowercase.
import tkinter
For general importing questions, I've seen the Importing Python Modules link referenced a lot on SO.
You seem to have named the file tkinter.py. You cannot name a file with the module you are importing. Python will try to import from your existing file instead of tkinter module. There will be module name collison. Try to rename your file name.
from Tkinter import *
root = Tk()

Categories