Please am creating an app view and in python and trying to show some images but am getting error. bellow is the code
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
root.title("Developing Software")
root.iconbitmap("icon.ico")
image = Image.open("tree_50b.JPG").convert("RGB")
my_img = ImageTk.PhotoImage(image)
my_label = Label(my_img)
my_label.pack()
button_quit = Button(root, text='Exit Program', fg='red', command=root.quit)
button_quit.pack()
root.mainloop()
This is the error message
Traceback (most recent call last):
File "<ipython-input-1-c9a970a4c796>", line 16, in <module>
my_label = Label(my_img)
File "C:\anconda\lib\tkinter\__init__.py", line 2766, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "C:\anconda\lib\tkinter\__init__.py", line 2292, in __init__
BaseWidget._setup(self, master, cnf)
File "C:\anconda\lib\tkinter\__init__.py", line 2269, in _setup
if master._last_child_ids is None:
AttributeError: 'PhotoImage' object has no attribute '_last_child_ids'
I need help please
The first argument to Label(...) needs to be a widget, not an image, and the image needs to be a keyword argument.
Label(root, image=my_img)
Related
_str__ returned non-string (type JpegImageFile) while trying to use image as button in Tkinter. I tried file= instead of Image.open but that didn't work too. Can someone tell me how to fix this
Here is the simplified version of the code:
from tkinter import *
from PIL import ImageTk, Image
# Window setup
mainwindow = Tk()
mainwindow.geometry('420x420')
mainwindow.title('Root Screen')
# Button image source
temp_img = PhotoImage(Image.open('D:\\Coding\\Python_stuff\\Watch_T800\\temp_img.jpg'))
temp_img_lbl = Label(image=temp_img)
temp_img_lbl.pack()
# Button function
def menufun():
menuwindow = Tk()
menuwindow.geometry('420x420')
menuwindow.title('Menu')
temp = Button(menuwindow, text="Temperature",height=5, width=10, image=temp_img)
temp.grid(row=0, column=0)
# Home Buttons
menu = Button(mainwindow, text="Menu", command=menufun).pack()
# BG Image
bg=Image.open('D:\\Coding\\Python_stuff\\Watch_T800\\background.png')
blah1=ImageTk.PhotoImage(bg)
lbl=Label(mainwindow, image=blah1)
lbl.pack()
mainloop()
And here is the error:
Traceback (most recent call last):
File "d:\Coding\Python_stuff\Watch_T800\test.py", line 14, in <module>
temp_img_lbl = Label(image=temp_img)
^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\yashw\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 3204, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "C:\Users\yashw\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 2628, in __init__
self.tk.call(
TypeError: __str__ returned non-string (type JpegImageFile)
I already explained what I tried in the , But I'm telling it again
I have tried using file= instead of image.open but it says
Pyimage1 Not found
I am noticing an error since I started to import files on VS code. But on other IDEs like python IDLE it works.
Code:
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
root_icon = PhotoImage(file="start_point1.png")
root.iconphoto(False, root_icon)
root.title("Canvas")
root.geometry("600x600")
color_label = Label(root, text="Pen-down Color: ", font=("comic sans ms", 12))
color_label.place(relx=0.6, rely=0.9, anchor=CENTER)
input_box = Entry(root)
input_box.insert(0, "black")
input_box.place(relx=0.8, rely=0.9, anchor=CENTER)
canvas1 = Canvas(root, width=590, height=510, bg="white", highlightbackground="lightgray")
canvas1.pack()
root.mainloop()
Error on vs code:
File "d:\New coding folder\Python projects\canvas\main.py", line 5, in <module>
root_icon = PhotoImage(file="start_point1.png")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.240.0_x64__qbz5n2kfra8p0\Lib\tkinter\__init__.py", line 4120, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.11_3.11.240.0_x64__qbz5n2kfra8p0\Lib\tkinter\__init__.py", line 4065, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't open "start_point1.png": no such file or directory
but works on python IDLE
Any fix for this?
NOTE: The code is not ready yet but it should work till here.
I was writing a python GUI program with Spleeter. And when it comes to the Separation function, the error occurs. Here's my code:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from spleeter.separator import Separator
from tkinter import *
from tkinter.ttk import *
from tkinter import messagebox
window = Tk()
screen_width,screen_height = window.maxsize()
window.title("Spleeter GUI Version")
w = int((screen_width-700)/2)
h = int((screen_height-400)/2)
window.geometry(f'700x400+{w}+{h}')
lbl = Label(window, text="File Path:")
lbl.grid(column=0, row=0)
txt = Entry(window, width=10)
txt.grid(column=1, row=0)
lbl2 = Label(window, text="Stems:")
lbl2.grid(column=0, row=1)
combo = Combobox(window)
combo['values'] = (2,4,5)
combo.current(0)
combo.grid(column=1, row=1)
def Separation():
File_name=txt.get();
stems='spleeter:'+combo.get()+'stems'
separator = Separator(stems)
separator.separate_to_file(File_name, 'out')
messagebox.showinfo("Notification", "Separation Finished!")
def clicked():
Separation()
btn = Button(window, text="Separate", command=clicked)
btn.grid(column=2, row=0)
def main():
window.mainloop()
if __name__=='__main__':
main()
And the error occurs every time I click on the Button, the message is like:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\MyUsrname\.conda\envs\music\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "d:\File\Code\python\helloworld.py", line 39, in clicked
Separation()
File "d:\File\Code\python\helloworld.py", line 33, in Separation
separator = Separator('spleeter:2stems')
File "C:\Users\MyUsrname\.conda\envs\music\lib\tkinter\ttk.py", line 1138, in __init__
Widget.__init__(self, master, "ttk::separator", kw)
File "C:\Users\MyUsrname\.conda\envs\music\lib\tkinter\ttk.py", line 559, in __init__
tkinter.Widget.__init__(self, master, widgetname, kw=kw)
File "C:\Users\MyUsrname\.conda\envs\music\lib\tkinter\__init__.py", line 2292, in __init__
BaseWidget._setup(self, master, cnf)
File "C:\Users\MyUsrname\.conda\envs\music\lib\tkinter\__init__.py", line 2262, in _setup
self.tk = master.tk
AttributeError: 'str' object has no attribute 'tk'
separator = Separator(stems)
I assume you meant spleeter.separator.Separator (from [GitHub]: deezer/spleeter - Spleeter).
But (according to traceback), you ended up using [Python.Docs]: tkinter.ttk.Separator. And, that's due to (order is important too):
from spleeter.separator import Separator
# ...
from tkinter.ttk import * # !!! THIS IS THE ONE !!!
Note that importing everything from a module is generally considered bad practice (with few exceptions, I personally consider that people using it kind of don't know what they are doing), so don't do it unless you know what it does behind the scenes.
Besides reusability and structure, packages and modules also act as (nested) namespaces, avoiding name clashes. So (most times), only import the package (or the module), and refer to classes, functions as (package.)module.function_or_class_or_variable_name
Example:
from spleeter import separator
sep = separator.Separator("a:b:c:d")
from tkinter import ttk
lbl = ttk.Label(window)
I changed from Python 3.4 to Python 2.7, and while debugging my script I found the interpreter unable to read a base64 graphic encoded in a string. I wanted to use the encoded image as a background for a tkinter canvas like so, but I am unable to do so now as I had been with Python 3.4:
background="""
iVBORw0KGgoAAA #....continues
"""
photo = tk.PhotoImage(data=background)
width, height = photo.width(), photo.height()
canvas = tk.Canvas(root, width=width, height=height, bd=-2)
canvas.pack()
canvas.create_image(0, 0, image=photo, anchor="nw")
When I run the script, I get this error:
>Traceback (most recent call last):
File "main.py", line 31, in <module>
photo = tk.PhotoImage(data=background)
File "c:\Python27\lib\lib-tk\Tkinter.py", line 3323, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "c:\Python27\lib\lib-tk\Tkinter.py", line 3279, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize image data
I have been creating an Email program using Tkinter, in Python 3.3.
On various sites I have been seeing that the Frame widget can get a different background using Frame.config(background="color").
However, when I use this in my Frames it gives the following error:
_tkinter.TclError: unknown option "-Background"
It does not work when doing the following:
frame = Frame(root, background="white")
Or:
frame = Frame(root)
frame.config(bg="white")
I can't figure it out.
I would post my whole source code but I dont want it exposed on the internet, but the frame creation goes something like this:
mail1 = Frame(self, relief=SUNKEN)
mail1.pack()
mail1.place(height=70, width=400, x=803, y=109)
mail1.config(Background="white")
I have tried multiple options trying to modify the background. The frame is like a wrap around an email preview for an inbox.
In case it's needed, this the way I am importing my modules:
import tkinter, time, base64, imaplib, smtplib
from imaplib import *
from tkinter import *
from tkinter.ttk import *
The following is the full traceback:
Traceback (most recent call last):
File "C:\Users\Wessel\Dropbox\Python\Main\Class Ginomail.py", line 457, in <module>
main()
File "C:\Users\Wessel\Dropbox\Python\Main\Class Ginomail.py", line 453, in main
app = Application(root) #start the application with root as the parent
File "C:\Users\Wessel\Dropbox\Python\Main\Class Ginomail.py", line 60, in __init__
self.initINBOX()
File "C:\Users\Wessel\Dropbox\Python\Main\Class Ginomail.py", line 317, in initINBOX
mail1.config(bg="white")
File "C:\Python33\lib\tkinter\__init__.py", line 1263, in configure
return self._configure('configure', cnf, kw)
File "C:\Python33\lib\tkinter\__init__.py", line 1254, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: unknown option "-bg"
Gives the following error with the code from the answer:
File "C:\Users\Wessel\Dropbox\Python\Main\Class Ginomail.py", line 317, in initINBOX
mail1 = Frame(self, relief=SUNKEN, style='myframe')
File "C:\Python33\lib\tkinter\ttk.py", line 733, in __init__
Widget.__init__(self, master, "ttk::frame", kw)
File "C:\Python33\lib\tkinter\ttk.py", line 553, in __init__
tkinter.Widget.__init__(self, master, widgetname, kw=kw)
File "C:\Python33\lib\tkinter\__init__.py", line 2075, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: Layout myframe not found
Solved! Thanks. Its the inbox bar to the right, background needed to be white.
The root of the problem is that you are unknowingly using the Frame class from the ttk package rather than from the tkinter package. The one from ttk does not support the background option.
This is the main reason why you shouldn't do wildcard imports -- you can overwrite the definition of classes and commands.
I recommend doing imports like this:
import tkinter as tk
import ttk
Then you prefix the widgets with either tk or ttk :
f1 = tk.Frame(..., bg=..., fg=...)
f2 = ttk.Frame(..., style=...)
It then becomes instantly obvious which widget you are using, at the expense of just a tiny bit more typing. If you had done this, this error in your code would never have happened.
You use ttk.Frame, bg option does not work for it. You should create style and apply it to the frame.
from tkinter import *
from tkinter.ttk import *
root = Tk()
s = Style()
s.configure('My.TFrame', background='red')
mail1 = Frame(root, style='My.TFrame')
mail1.place(height=70, width=400, x=83, y=109)
mail1.config()
root.mainloop()