Python Tkinter Error - python

I have tried to work with the Tkinter library, however, I keep getting this message, and I don't know how to solve it.. I looked over the net but found nothing to this specific error - I call the library like this:
from Tkinter import *
and I get this error -
TclError = Tkinter.TclError
AttributeError: 'module' object has no attribute 'TclError'
I have no clue what can I do now..
Thank you
full traceback:
Traceback (most recent call last):
File "C:/Users/Shoham/Desktop/MathSolvingProject/Solver.py", line 3, in <module>
from Tkinter import *
File "C:\Heights\PortableApps\PortablePython2.7.6.1\App\lib\lib- tk\Tkinter.py", line 41, in <module>
TclError = Tkinter.TclError
AttributeError: 'module' object has no attribute 'TclError'

You imported (mostly) everything from the module with from Tkinter import *. That means that (mostly) everything in that module is now included in the global namespace, and you no longer have to include the module name when you refer to things from it. Thus, refer to Tkinter's TclError object as simply TclError instead of Tkinter.TclError.

The problem seems to be in "C:\Heights\PortableApps\PortablePython2.7.6.1\App\lib\lib-tk\Tkinter.py:
The regular python install imports in lib-tk\Tkinter.py are different to what is in PortablePython2.7.6.1\App\lib\lib-tk\Tkinter.py:
try:
import _tkinter
except ImportError, msg:
raise ImportError, str(msg) + ', please install the python-tk package'
tkinter = _tkinter # b/w compat for export
TclError = _tkinter.TclError
Then where Tkinter is used in PortablePython _tkinter is used instead. It seems like a bug in PortablePython.
The full contents of the file are here. Replacing the file in C:\Heights\PortableApps\PortablePython2.7.6.1\App\lib\lib-tk\Tkinter.py as per the comments fixes the issue.

Like #ErezProductions said. You either have to import everything and access it directly or import only the module.
from Tkinter import *
TclError
or
import Tkinter
Tkinter.TclError

See the difference:
>>> import tkinter
>>> TclError = tkinter.TclError
>>>
No error. But, with your method:
>>> from tkinter import *
>>> TclError = tkinter.TclError
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'tkinter' is not defined
The difference is that the first method imports the module tkinter into the name space. You can address its properties using the dot notation tinter.property. However, the from tkinter import * imports the module's properties into the name space, not the module itself.
Either try the first method given above, or adjust your approach (NB: importing all properties is a bad idea) like so:
>>> from tkinter import *
>>> my_TclError = TclError # renamed because TclError defined in tkinter
>>>

Related

NameError: name 'regc' is not defined in Tkinter

I got a name error when i am using this code.Can anyone fix this problem?
from tkinter import *
import mysql.connector
home=Tk()
home.geometry("700x700")
home.title("Home")
reg=Button(home,text="Register",bg='brown',fg='white',width=20,command=regc)
reg.place(x=350,y=200)
mainloop()
I got an error like this:
Traceback (most recent call last):
File "C:/Users/Softech/Desktop/tkinterproject.py", line 29, in <module>
reg=Button(home,text="Register",bg='brown',fg='white',width=20,command=regc)
NameError: name 'regc' is not defined
Maybe you forgot to define the function regc, anyway in the code its not there. So start off by defining it. Keep in mind, you have to define it before the declaration of the button.
from tkinter import *
import mysql.connector
def regc():
new=Toplevel()
new.geometry("500x500")
new.title("Registration")
Label_reg=Label(new,text="REGISTRATION FORM",width=20,font=("bold",20))
Label_reg.place(x=90,y=53)
lname=Label(new,text="Name",width=20,font=("bold",10))
lname.place(x=80,y=130)
home=Tk()
home.geometry("700x700")
home.title("Home")
reg=Button(home,text="Register",bg='brown',fg='white',width=20,command=regc)
reg.place(x=350,y=200)
home.mainloop()
To understand more on how to define a function, take a look here
Hope it helped you solve your error.
Cheers

Cannot import a function from a module in python

I'm using this code.
from azlyrics import artists
print(artists("O"))
In the module named 'azlyrics' the function 'artists' is well defined. But I get this error.
Traceback (most recent call last):
File "C:\Users\user\Desktop\python\eminem\New folder\azlyrics-master\examples\get_artists.py", line 1, in <module>
from azlyrics import artists
ImportError: cannot import name 'artists' from 'azlyrics' (C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\azlyrics\__init__.py)
What could be the problem?
There must be a bug in the azlyrics documentation or packaging.
This works:
>>> from azlyrics.azlyrics import artists
>>> artists("O")
'["Oakenfold, Paul", "Oakes, Ryan", "Oak Ridge Boys,
The", "Oak, Winona", "O.A.R. (Of A Revolution)", "Oasis", "Obel, Agnes", "Oberst, ...]'
There is a mistake in azlyrics v1.3.2, relative import should be used in azlyrics/__init__.py:
instead of:
from azlyrics import *
we should have:
from .azlyrics import *
This is fixed but a release is not done.

TypeError: 'module' object is not subscriptable

I'm using python version 3.6.
mystuff.py includes:
mystuff = {'donut': "SHE LOVES DONUTS!"}
mystuffTest.py includes this
import mystuff
print (mystuff['donut'])
The error that I receive when I run mystuffTest.py is as follows:
$ python3.6 mystuffTrythis.py
Traceback (most recent call last):
File "mystuffTrythis.py", line 3, in <module>
print (mystuff['donut'])
TypeError: 'module' object is not subscriptable
So far I haven't seen this exact error here on stackoverflow. Can anyone explain why I am getting this error?
import mystuff is importing the module mystuff, not the variable mystuff. To access the variable you'd need to use:
import mystuff
print(mystuff.mystuff['donut'])
EDIT: It's also possible to import the variable directly, using:
from mystuff import mystuff
print(mystuff['donut'])
I got this error because a later from __ import * statement imported a module which bound my variable to something else:
from stuff_a import d
from stuff_b import *
d['key']
In stuff_b.py, d was bound to a module, hence the error. Lesson learned: avoid importing * from modules.

Module has no attribute error in python3

contents of io.py
class IO:
def __init__(self):
self.ParsingFile = '../list'
def Parser(self):
f = open(ParsingFile, 'r')
print(f.read())
contents of main.py
import sys
sys.path.insert(0, "lib/")
try:
import io
except Exception:
print("Could not import one or more libraries.")
exit(1)
print("Libraries imported")
_io_ = io.IO()
When I run python3 main.py I get the following error:
Libraries imported
Traceback (most recent call last):
File "main.py", line 11, in <module>
_io_ = io.IO()
AttributeError: module 'io' has no attribute 'IO'
Any idea what's going wrong?
My file was called io. It seems that there already exists a package called io which caused the confusion.
Your package name (io) conflicts with the Python library's package with the same name, so you actually import a system package.
You can check this by printing io.__all__.
Changing io.py to something else is probably the best way to go to avoid similar problems. Otherwise, you can use an absolute path.
try
from io import IO
That worked for me when trying to import classes from another file
this has more information:
Python module import - why are components only available when explicitly imported?

Fixed: Python NameError, fixed AttributeError and got this?

FIXED: turns out there is a module already called parser. Renamed it and its working fine! Thanks all.
I got a python NameError I can't figure out, got it after AttributeError. I've tried what I know, can't come up with anything.
main.py:
from random import *
from xml.dom import minidom
import parser
from parser import *
print("+---+ Roleplay Stat Reader +---+")
print("Load previous DAT file, or create new one (new/load file)")
IN=input()
splt = IN.split(' ')
if splt[0]=="new":
xmlwrite(splt[1])
else:
if len(splt[1])<2:
print("err")
else:
xmlread(splt[1])
ex=input("Press ENTER to Exit...")
parser.py:
from xml.dom import minidom
from random import *
def xmlread(doc):
xmldoc = minidom.parse(doc)
itemlist = xmldoc.getElementsByTagName('item')
for s in itemlist:
print(s.attributes['name'].value,":",s.attributes['value'].value)
def xmlwrite(doc):
print("no")
And no matter what I get the error:
Traceback (most recent call last):
File "K:\Python Programs\Stat Reader\main.py", line 10, in <module>
xmlwrite.xmlwrite(splt[1])
NameError: name 'xmlread' is not defined
The same error occurs when trying to access xmlwrite.
When I change xmlread and xmlwrite to parser.xmlread and parser.xmlwrite I get:
Traceback (most recent call last):
File "K:\Python Programs\Stat Reader\main.py", line 15, in <module>
parser.xmlread(splt[1])
AttributeError: 'module' object has no attribute 'xmlread'
The drive is K:\ because it's my personal drive at my school.
If your file is really called parser.xml, that's your problem. It needs to be parser.py in order to work.
EDIT: Okay, since that wasn't your issue, it looks like you have a namespacing issue. You import your parser module twice when you use import parser and then from parser import *. The first form of it makes "parser" the namespace and the second form directly imports it, so in theory, you should have both parser.xmlwrite and xmlwrite in scope. It's also clearly not useful to import minidom in main.py since you don't use any minidom functionality in there.
If you clear up those and still have the issue, I would suggest looking at __ init __.py. If that still does nothing, it could just plain be a conflict with Python's parser module, you could substitute a name like myxmlparser.

Categories

Resources