Unsolved reference 'Tk'? [duplicate] - python

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.

Related

How to detect text change of an entry in tkinter Python [duplicate]

This question already has answers here:
How do I get an event callback when a Tkinter Entry widget is modified?
(9 answers)
Closed 5 months ago.
How can detect that a user entering characters in tkinter entry ?
I want to calculate the total cost from 2 different entry. here is my code but does not work!
from tkinter import *
root=Tk()
def calculate_total_cost(event):
if count_ent.get().isdigit() and unit_cost_ent.get().isdigit():
total_cost=int(count_ent.get())*int(unit_cost_ent.get())
print(total_cost)
count_ent=Entry(root).pack()
unit_cost_ent=Entry(root).pack()
unit_cost_ent.bind("<key>",calculate_total_cost)
Please check this, insert value in both entry and press enter, you will get the results. Although your questions is also not cleared, but from your statement I decided that you are facing issue of "AttributeError: 'NoneType' object has no attribute 'bind'"...
By executing the below code, I hope you will get your answer. First execute this simple program, you will get the desired output in terminal.
from tkinter import *
root=Tk()
def calculate_total_cost(event):
if count_ent.get().isdigit() and unit_cost_ent.get().isdigit():
total_cost=int(count_ent.get())*int(unit_cost_ent.get())
print(total_cost)
count_ent=Entry(root)
count_ent.pack()
# count_ent.insert(0, value1)
unit_cost_ent=Entry(root)
unit_cost_ent.pack()
# unit_cost_ent.insert(0, value2)
unit_cost_ent.bind("<Return>",calculate_total_cost)
root.mainloop()

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.)

Tkinter cannot find filedialog? [duplicate]

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.

Tkinter module error on attributes like Tk or Frame

i am trying to use Tkinter on python 3.6
However it gives me an error when i try to run the following code:
import tkinter
top = tkinter.Tk()
top.mainloop()
The error says:
AttributeError: module 'tkinter' has no attribute 'Tk'
I receive the same error if i try to call the attribute Frame.
Any idea?
Thanks
Make sure you didn't name the file tkinter.py; It prevents import of tkinter module because the file your wrote searched first and imported.
Rename the file, and also make sure there's no tkinter.pyc remained.

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