Ttk style layout not found how to fix? - python-3.x

I am making my own custom Menubar.My work is in process but i recently came across one confusion.
Here is my code:-
from tkinter import *
from tkinter.ttk import Style,Frame as fp
class menu():
def __init__(self,parent,bg="#ffffcc"):
self.parent = parent
self.bg = bg
#this is a image in base 64 encoded format
borderImageData='''
iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAIAAAAlC+aJAAAABmJLR0QA/wD/
AP+gvaeTAAACZ0lEQVRoge2aPU/TURSHn/MvpbYYKa9qQQnlTSOJRBzoYBDi
YEwcdPFD+PIFHHRxcCXxO7gxmBgXSTcxMYQEE4spoEWIvBcDBfp2HVqlxRhK
TDwtuc947vJ7cnPvGc6R/v5+KhlHO8C/YgW0sQLaWAFtqg4WxKEjJMGQaQyK
txbHpZGqmGzG7GzKyoyZfcfsOCZbeChFjaz+vAw/oq6V1K75HiERJ5v+33H/
xKnC55czF3CfYD1mxkbY+Pb7sECguUtuPQbMh5d8eksmpZL2r7jccvEGV++B
Ma+fsRzNlwOBAID3lNx+gjHm1VNiEweuqSwwWZajxCak85oEQ3wOk0my/4j7
7uD1m7ERNuY1Ux7KeozwC3x+uXI3V3AAXFXSPcjCFAtTmuFKw8xPsviRrsHc
B+MANHVS7WPuvXK0kjGz43hqaOogL3CyASC+oJrqKMQXAWoayQmI2wuY1J5q
qKOQ2gXE4+MYdGIroI0V0MYKaGMFtLEC2lgBbayANlZAGyugjRXQxgpoYwW0
sQLaWAFtrIA2DmCSCQC3RzlL6VR7AZIJ8jewtQaIv0Uz05GoPQuYrVXyAqsz
JBO0D+imKh0JhtjbYmWGvEAmbabDtPTSelk5Wimc6yNwiekw2Qz7j3hylERc
hh9S36YZ7jCkoU2GHpjtdTM5mqv8mtSnkyxFpHtQeoZIJ1n7UnbDelc1vTfl
+n1EePOcH0u5cvGyR11r/hJSeyxFzPZGmSx7SE09p3twe1j7asZGCifCcnDt
UhzaBwgOSHMnZbNuw86mWY7K3Lg5ZN2mAjkWnbiisQLaWAFtKl7gJzIjtMOb
uqQwAAAAAElFTkSuQmCC
'''
self.borderImage = PhotoImage( data=borderImageData,master=self.parent)
self.TP_style=Style()
self.TP_style.element_create("RoundedFrame",
"image", self.borderImage,
border=14, sticky="nsew")
self.TP_style.layout("RoundedFrame",
[("RoundedFrame", {"sticky": "nsew"})])
self.frame_one = fp(parent, style="RoundedFrame", padding=10,width=100,height=100)
self.frame_one.pack()
l1=Label(parent,image=self.borderImage).pack()
def popup(self,x,y,width=110,height=120):
self.width=width
self.height=height
self.app = Tk()
self.app.config(bg=self.bg)
self.app.geometry(f"{self.width}x{self.height}+{x}+{y}")
self.app.wm_attributes("-topmost",True)
self.app.overrideredirect(True)
self.app.focus_force()
#self.frame_one = fp(self.app, style="RoundedFrame", padding=10,width=100,height=100)
#self.frame_one.pack()
#l1=Label(self.app,image=self.borderImage).pack()
#self.m.pack_propagate(0)
def destroy(event):
self.app.destroy()
self.app.bind("<FocusOut>",destroy)
if __name__ == "__main__":
root = Tk()
menu = menu(root)
def evt(evt):
menu.popup(evt.x_root,evt.y_root,width=200,height=400)
root.bind("<Button-3>",evt)
root.mainloop()
If i write style for frame like
self.frame_one = fp(parent, style="RoundedFrame", padding=10,width=100,height=100)
in the init() method every thing works fine. If same thing i write in popup methon(where i have just commented out),i got the following error.
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\mishra\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C:\Users\mishra\P_Menu.py", line 64, in evt
menu.popup(evt.x_root,evt.y_root,width=200,height=400)
File "C:\Users\mishra\P_Menu.py", line 51, in popup
self.frame_one = fp(self.app, style="RoundedFrame", padding=10,width=100,height=100)
File "C:\Users\mishra\AppData\Local\Programs\Python\Python38-32\lib\tkinter\ttk.py", line 740, in __init__
Widget.__init__(self, master, "ttk::frame", kw)
File "C:\Users\mishra\AppData\Local\Programs\Python\Python38-32\lib\tkinter\ttk.py", line 557, in __init__
tkinter.Widget.__init__(self, master, widgetname, kw=kw)
File "C:\Users\mishra\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 2567, in __init__
self.tk.call(
_tkinter.TclError: Layout RoundedFrame not found
Same thing for Lable
l1=Label(self.app,image=self.borderImage).pack()
In the init() section everything works fine but in popup method I got following error.
Traceback (most recent call last):
File "C:\Users\mishra\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
return self.func(*args)
File "C:\Users\mishra\P_Menu.py", line 64, in evt
menu.popup(evt.x_root,evt.y_root,width=200,height=400)
File "C:\Users\mishra\P_Menu.py", line 53, in popup
l1=Label(self.app,image=self.borderImage).pack()
File "C:\Users\mishra\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 3143, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "C:\Users\mishra\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 2567, in __init__
self.tk.call(
_tkinter.TclError: image "pyimage1" doesn't exist
I want to know how to fix it,cause i want to know logic behind it and use in popup() method.
Any help will be appriciated.
Thank You!

Maybe it's because popup creates a new Tk instance as self.app, but the style belongs to the first Tk instance created by root = Tk(). So a child of the second Tk instance cannot recognize it. Maybe you can try changing self.app = Tk() to self.app = Toplevel() to see if it works?

Related

Python 3.6 - _tkinter.TclError

Today I tried to make a text editor using Tkinter's Filedailog, Font and Text functions. But when I tried to run it, It gave me a tkinter.tclerror.
I am using Sublime Text Editor and Command Prompt with a Virtual Environment
Here is my Code:
from tkinter import *
from tkinter import filedialog
from tkinter import font
root = Tk()
root.title('CodePad Code Editor')
root,geometry("1200x660")
# Create Main Frame which will take text
my_frame = Frame(root)
my_frame.pack(pady=5)
# Create Text Box for text input
my_text = Text(my_frame, width=97, height=25, font=("Helvetica", 16), selectbakground="yellow", selectforeground="black", undo=True, yscrollcommand=text_scroll.set)
my_text.pack()
root.mainloop()
And this was the error:
Traceback (most recent call last):
File "C:\Users\Steve\AppData\Local\Programs\Python\Python36-32\lib\runpy.py",
line 193, in _run_module_as_main
"__main__", mod_spec)
File "C:\Users\Steve\AppData\Local\Programs\Python\Python36-32\lib\runpy.py",
line 85, in _run_code
exec(code, run_globals)
File "e:\Python-Projects\text_editor\TextEditor.py", line 5, in <module>
root = Tk()
File "C:\Users\Steve\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__
init__.py", line 2017, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, want
objects, useTk, sync, use)
_tkinter.TclError
Anybody please tell me what can I do to clear the error
Thanks
Steve

When using the Button object in Tkinter I get an error

When running this code:
import tkinter as tk
class MyApplication(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.title('Programma')
self.geometry('500x400')
self.button = tk.Button(self, text='Execute', command=self.fun)
self.button.grid(row=0, column=4, stick=tk.W)
def fun(self):
print('Hello')
app = MyApplication()
app.mainloop()
I get the following error:
Traceback (most recent call last): File "C:/Users/Andrea/Desktop/hfhfhg.py", line 17, in <module>
app = MyApplication() File "C:/Users/Andrea/Desktop/hfhfhg.py", line 10, in __init__
self.button = tk.Button(self, text='Execute', command=self.fun) File "C:\WPy64-3910\python-3.9.1.amd64\lib\tkinter\__init__.py", line 2346, in __getattr__
return getattr(self.tk, attr) AttributeError: '_tkinter.tkapp' object has no attribute 'fun'
I understand that the code is not optimal for a GUI but I would like to understand how Tkinter works without using a frame object. Please can you help me?

Python _tkinter.TclError: bad screen distance "0.5" using Turtle module

I'm a turtle's beginner and I wrote a little program (file.py) who draws something.
But when I call the file with an import statement from another file (theotherfile.py. Note that it use tkinter), I have this error :
Traceback (most recent call last):
File "C:\Users\jeandubois\Documents\GitHub\myproject\theotherfile.py", line 443, in <module>
import theotherfile
File "C:\Users\jeandubois\Documents\GitHub\myproject\file.py", line 10, in <module>
turtle1 = turtle.Turtle()
File "C:\Users\jeandubois\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 3813, in __init__
Turtle._screen = Screen()
File "C:\Users\jeandubois\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 3663, in Screen
Turtle._screen = _Screen()
File "C:\Users\jeandubois\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 3689, in __init__
self._root.setupcanvas(width, height, canvwidth, canvheight)
File "C:\Users\jeandubois\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 438, in setupcanvas
self._canvas = ScrolledCanvas(self, width, height, cwidth, cheight)
File "C:\Users\jeandubois\AppData\Local\Programs\Python\Python39\lib\turtle.py", line 343, in __init__
self._canvas = TK.Canvas(master, width=width, height=height,
File "C:\Users\jeandubois\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2685, in __init__
Widget.__init__(self, master, 'canvas', cnf, kw)
File "C:\Users\jeandubois\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 2569, in __init__
self.tk.call(
_tkinter.TclError: bad screen distance "0.5"
My code :
# file.py
import tkinter
root = tkinter.Tk()
[some awesome code]
root.mainloop()
# when closing root:
import theotherfile
# theotherfile.py
import turtle
turtle1 = turtle.Turtle()
turtle2 = turtle.Turtle()
turtle3 = turtle.Turtle()
turtle1.speed(100)
turtle2.speed(100)
turtle3.speed(100)
turtle1.width(5)
turtle2.width(5)
turtle3.width(5)
turtle1.color(choice(colors))
turtle2.color(choice(colors))
turtle3.color(choice(colors))
turtle1.up()
turtle2.up()
turtle3.up()
turtle1.goto(-100, -100)
turtle2.goto(0, -100)
turtle3.goto(-50, -13.4)
turtle1.down()
turtle2.down()
turtle3.down()
a = 100
for loop in range(2):
for loop2 in range(3):
turtle1.forward(a)
turtle1.left(120)
turtle2.forward(a)
turtle2.left(120)
turtle3.forward(a)
turtle3.left(120)
turtle1.color(choice(colors))
turtle2.color(choice(colors))
turtle3.color(choice(colors))
a = 50
turtle1.forward(50)
turtle2.forward(50)
turtle3.forward(50)
for loop in range(5):
turtle1.forward(a)
turtle1.left(120)
turtle2.forward(a)
turtle2.left(120)
turtle3.forward(a)
turtle3.left(120)
turtle1.color(choice(colors))
turtle2.color(choice(colors))
turtle3.color(choice(colors))
turtle1.right(60)
turtle2.right(60)
turtle3.right(60)
turtle1.forward(50)
turtle2.forward(50)
turtle3.forward(50)
turtle1.up()
turtle2.up()
turtle3.up()
turtle1.hideturtle()
turtle2.hideturtle()
turtle3.hideturtle()
turtle.mainloop()
I use Python 3.9.0 .
Can someone help me please ?

Tkinter PhotoImage Error: couldn't recognize data in image file

I am working on winows 7 and python 3.8.2. I have an error.
I wanted to upload an Image in a tkinter window. I tried importing a .gif file and I get an error as
"couldn't recognize data in image file". Though the file is in the correct path the error is happening.
Can you please help me?
Here is the Code
from tkinter import *
window = Tk()
window.title("try")
canvas = Canvas(window, height = 500, width = 500)
canvas.pack()
my_image = PhotoImage(file = "C:\\Users\\jeeva\\Pictures\\bulbon.gif")
canvas.create_image(0, 0, anchor = NW, image = my_image)
window.mainloop()
And here is the error:
Traceback (most recent call last):
File "C:\Users\jeeva\Desktop\Tanmay_new\python\Codes\Not Finished\tett.py", line 6, in <module>
my_image = PhotoImage(file = "C:\\Users\\jeeva\\Pictures\\bulbon.gif")
File "C:\Users\jeeva\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 4061, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\jeeva\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 4006, in __init__
self.tk.call(('image', 'create', imgtype, name,) + options)
_tkinter.TclError: couldn't recognize data in image file "C:\Users\jeeva\Pictures\bulbon.gif"
Thank you,
Regards,
Tanmay

I can't save a list

import tkinter as tk
list = []
def add():
element = entry.get()
entry.delete(0, 1000)
list.append(element)
def save():
with open("save.txt", "w") as f:
for element in list:
f.write(element + ",")
root = tk.Tk()
frameList = tk.Frame(root)
frameList.pack()
entry = tk.Entry(root)
entry.pack()
btnAdd = tk.Button(root, text="add", command=add)
btnAdd.pack(pady=5)
btnSave = tk.Button(root, text="save", command=save)
btnSave.pack()
root.mainloop()
this code give me this error when I save:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:/Users/HP/Desktop/python/github/write.py", line 11, in save
with open("save.txt", "w") as f:
FileNotFoundError: [Errno 2] No such file or directory: 'save.txt'
and if I create the file "save.txt" it says:
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\HP\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:/Users/HP/Desktop/python/github/write.py", line 13, in save
f.write(element + ",")
OSError: [Errno 9] Bad file descriptor
please, can you help me to solve this problem?
sorry for this last sentence but I have to add some "not code" to publish the question, so I will say some random stuff from now, sorry

Resources