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
Related
from tkinter import *
window = Tk()
window.geometry("500x500")
icon = PhotoImage(file="X.png")
window.iconphoto(True,icon)
window.mainloop()
#my code
#error
Traceback (most recent call last):
File "C:\Users\priya\PycharmProjects\pythonProject2\main.py", line 5, in <module>
icon = PhotoImage(file="X.png")
File "C:\Users\priya\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 4093, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\priya\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 4038, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "X.png"
It seems that the file X.png is not a valid PNG image file, try viewing it in a image viewer and check its filetype via https://www.checkfiletype.com/ or using the file command via WSL.
I am attempting to make a tkinter project but as I tried to run for like the third time it gives me this error:
Traceback (most recent call last):
File "/Users/cool/Documents/STM Wisepay Service.py", line 63, in <module>
app = App(root)
File "/Users/cool/Documents/STM Wisepay Service.py", line 20, in __init__
self.create_buttons()
File "/Users/cool/Documents/STM Wisepay Service.py", line 30, in create_buttons
tk.Button(button_frame, text = "Add to Debt", commmand = self.debt).grid(column = 6, row = 5)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 2366, in __init__
Widget.__init__(self, master, 'button', cnf, kw
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 2296, in __init__
(widgetName, self._w) + extra + self._options(cnf)
_tkinter.TclError: unknown option "-commmand"
What can I do to fix this? What are the problems with my code?
Here is my code:
https://pastebin.com/mWPEFbpz
You are spelling command wrong (three m's)
You are passing it a variable called commmand and it doesn't know how to use that variable.
tk.Button(... commmand = ...
You can see it in the error message right here
_tkinter.TclError: unknown option "-commmand"
These python error messages are actually very helpful. I would encourage you to read them carefully to catch stuff like this.
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.
I have a label object that needs to display a variety of images. Based on my research, the following script (below) should successfully update the image displayed in my label object. However, when I call self.ImageViewer.configure(image=self.imgToDisp), I get a TypeError (seen below).
I'm not entirely sure what the error means, but it looks like .configure is expecting a string? Nobody else seems to encounter this problem so I must be doing something syntactically wrong with the script below. Any input is appreciated.
Script:
def getImgs(self):
folderPath = filedialog.askdirectory()
self.imgArray, self.imagePaths = batchImpt.importAllImgs(folderPath)
halThrThsnd.saveAllData(self.imgArray)
im = Image.open(self.imagePaths[0])
self.imgToDisp = PhotoImage(im)
self.imageViewer.configure(image = self.imgToDisp)
self.imageViewer.image = self.imgToDisp
Error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\tkinter\__init__.py", line 1549, in __call__
return self.func(*args)
File "//mspm1bnas50s/home58/biegad1/Python Scripts/GUI_0_1.py", line 40, in getImgs
self.imageViewer.configure(image = self.imgToDisp)
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\tkinter\__init__.py", line 1330, in configure
return self._configure('configure', cnf, kw)
File "C:\Users\biegad1\AppData\Local\Continuum\Anaconda3\lib\tkinter\__init__.py", line 1321, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
TypeError: __str__ returned non-string (type TiffImageFile)
You need to change self.imgToDisp = PhotoImage(im) to self.imgToDisp = ImageTk.PhotoImage(im)
Of course, you must add ImageTk to your import statements. I think you already did: from PIL import Image. If So, modify it to: from PIL import Image, ImageTk.
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 ).