I am not familiar with the ways of python, i saw few other questions here with similar description, but could not fix this.
Error:
Traceback (most recent call last):
File "C:/Users/UT/PycharmProjects/tkinter/python_PET/main.py", line 16, in <module>
m = menu_bar_class(root)
File "C:/Users/UT/PycharmProjects/tkinter/python_PET/main.py", line 14, in __init__
self.master.config(self.menu)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1326, in configure
return self._configure('configure', cnf, kw)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1312, in _configure
cnf = _cnfmerge(cnf)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 114, in _cnfmerge
for c in _flatten(cnfs):
AttributeError: Menu instance has no attribute '__len__'
Program:
from Tkinter import *
from tkFileDialog import *
import tkMessageBox
import ttk
root = Tk()
class menu_bar_class:
def __init__(self,master):
self.master = master
print("menu bar")
self.menu = Menu(self.master)
self.master.config(self.menu)
m = menu_bar_class(root)
root.mainloop()
You need to pass in the menu as a keyword argument:
self.master.config(menu=self.menu)
When you pass in a positional argument (so without the menu= part), then Tkinter expects to receive either a dictionary with configuration (so {'menu': self.menu}) or a sequence containing more sequences or dictionaries. Because self.menu is neither, you get the error you see.
Related
I am trying to display an image in a GUI, and don't understand what is wrong. I keep getting this error:
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
What should my code (especially the line with my_img=...) look like?
My Code:
from tkinter import *
from PIL import ImageTk,Image
my_img = ImageTk.PhotoImage(Image.open("iu.jpeg"))
my_label = Label(image=my_img)
my_label.pack()
root = Tk()
root.title("ICON PRACTICE")
root.iconbitmap('iu.ico')
button_quit = Button(root, text = "EXIT", command=root.quit)
button_quit.pack()
root.mainloop()
The full error
Traceback (most recent call last):
File "main.py", line 4, in <module>
my_img = ImageTk.PhotoImage(Image.open("test.png"))
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/PIL/ImageTk.py", line 112, in __init__
self.__photo = tkinter.PhotoImage(**kw)
File "/usr/lib/python3.8/tkinter/__init__.py", line 4064, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "/usr/lib/python3.8/tkinter/__init__.py", line 3997, in __init__
raise RuntimeError('Too early to create image')
RuntimeError: Too early to create image
Exception ignored in: <function PhotoImage.__del__ at 0x7f7148fadc10>
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/PIL/ImageTk.py", line 118, in __del__
name = self.__photo.name
AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
Try doing this, its probably because you create the root object after opening the image and creating the photo object.
import os
from tkinter import *
from PIL import ImageTk,Image
root= Tk()
i = Image.open("C:/path/to/the/image/directory/image.png")
photo = ImageTk.PhotoImage(i)
root.mainloop()
The Label widget will only work after root = Tk() (Tk() starts the underlying Tcl interpreter) is declared. Then, all child widgets must have root as their first parameter (e.g., Label(root, text='hi')). You started the interpreter after you tried to use it, so Python raised an exception.
I have declared an image file within a class and I'm able to show it in the class body. But when I am calling it within a function to show it within the function body it throws an error.
Here is my code:
import tkinter
from tkinter import *
from PIL import Image,ImageTk
body=Tk()
ftpim = Image.open('textfile_big.png')
ftpfileimage = ImageTk.PhotoImage(ftpim)
lbl=Label(body,image=ftpfileimage).pack()
def nextimg():
cbody=Tk()
lbl=Label(cbody,image=ftpfileimage).pack()
but=Button(body,command=nextimg,text="show").pack()
body.mainloop()
When I'm executing it, however, I have the error "pyimage1" doesn't exist :
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\My Love\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
File "C:\Users\My Love\Desktop\New Folder\Unknown_Project\Unknown_package\sssss.py", line 10, in nextimg
lbl=Label(cbody,image=ftpfileimage).pack()
File "C:\Users\My Love\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2763, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "C:\Users\My Love\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2296, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "pyimage1" doesn't exist
hey guys i stuck on simple python GUI program
import Tkinter as tk
window = tk.Tk
text_box = tk.Entry(window)
def save_text():
str1 = text_box.get()
fx = open("file1.txt", "w")
fx.write(str1)
fx.close()
btn1 = tk.Button(window, text="Save", command="save_text")
text_box.pack()
btn1.pack()
window.mainloop()
i this error:
Traceback (most recent call last):
File "C:/Users/Saket/PycharmProjects/guiform1/firstform.py", line 5, in <module>
text_box = tk.Entry(window)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 2385, in __init__
Widget.__init__(self, master, 'entry', cnf, kw)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1965, in __init__
BaseWidget._setup(self, master, cnf)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1943, in _setup
self.tk = master.tk
AttributeError: class Tk has no attribute 'tk'
Anybody having idea what am i doing wrong please help me??
You have to call tk.Tk to create an instance:
window = tk.Tk()
^^
You will then have a tk.Tk-type object to interact with.
Here I'm starting to create a TK window with a text field but when ever I run this I get the error
Exception in Tkinter callback
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1470, in __call__
return self.func(*args)
File "School.py", line 31, in begin
emulatorI=emulator()
File "School.py", line 20, in __init__
code.pack(self.root)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1868, in pack_configure
+ self._options(cnf, kw))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1105, in _options
cnf = _cnfmerge(cnf)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 114, in _cnfmerge
for c in _flatten(cnfs):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1826, in __getattr__
return getattr(self.tk, attr)
AttributeError: __len__
from within the module. What am I doing wrong? I'm realitively new to classes so I may have done something wrong in the class setup.
I'm running python 2.7 on OS 10.9
class emulator:
def __init__(self):
self.root=Tk()
self.root.geometry("500x500")
self.root.title("Python")
code=Text(self.root)
code.pack(self.root)
self.root.mainloop()
emulatorI=emulator()
The problem is with the line
code.pack(self.root)
The pack function normally takes no arguments except keyword arguments, and so should just be called as
code.pack()
The reason for the very odd error is that pack can take a positional argument, which is expected to be a dictionary of options. When trying to treat the Tk instance as a dictionary, it failed due to the lack of a __len__ method.
What is the purpose of code.pack(self.root)?
The arguments for pack are placement directions, such as side, padding, fill, anchor, etc
(see http://effbot.org/tkinterbook/pack.htm ).
(Python version: 3.1.1)
I am having a strange problem with StringVar in tkinter. While attempting to continuously keep a Message widget updated in a project, I kept getting an error while trying to create the variable. I jumped out to an interactive python shell to investigate and this is what I got:
>>> StringVar
<class 'tkinter.StringVar'>
>>> StringVar()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python31\lib\tkinter\__init__.py", line 243, in __init__
Variable.__init__(self, master, value, name)
File "C:\Python31\lib\tkinter\__init__.py", line 174, in __init__
self._tk = master.tk
AttributeError: 'NoneType' object has no attribute 'tk'
>>>
Any ideas? Every example I have seen on tkinter usage shows initializing the variable with nothing sent to the constructor so I am at a loss if I am missing something...
StringVar needs a master:
>>> StringVar(Tk())
<Tkinter.StringVar instance at 0x0000000004435208>
>>>
or more commonly:
>>> root = Tk()
>>> StringVar()
<Tkinter.StringVar instance at 0x0000000004435508>
When you instantiate Tk a new interpreter is created. Before that nothing works:
>>> from Tkinter import *
>>> StringVar()
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Python26\lib\lib-tk\Tkinter.py", line 251, in __init__
Variable.__init__(self, master, value, name)
File "C:\Python26\lib\lib-tk\Tkinter.py", line 182, in __init__
self._tk = master.tk
AttributeError: 'NoneType' object has no attribute 'tk'
>>> root = Tk()
>>> StringVar()
<Tkinter.StringVar instance at 0x00000000044C4408>
The problem with the examples you found is that probably in the literature they show only partial snippets that are supposed to be inside a class or in a longer program so that imports and other code are not explicitly indicated.