My code stopped working quite literally overnight, due to an issue with Tkinter.
I'm using PySimpleGui for my project, which was working the previous day.
When I run my program it showed the bellow error:
File "E:/Projekty Python/dcs_assistant/gui_and_video.py", line 1, in <module>
import PySimpleGUI as sg
File "C:\Users\pawni\Miniconda3\envs\dcs_assistant\lib\site-packages\PySimpleGUI\__init__.py", line 2, in <module>
from .PySimpleGUI import *
File "C:\Users\pawni\Miniconda3\envs\dcs_assistant\lib\site-packages\PySimpleGUI\PySimpleGUI.py", line 125, in <module>
tclversion_detailed = tkinter.Tcl().eval('info patchlevel')
File "C:\Users\pawni\Miniconda3\envs\dcs_assistant\lib\tkinter\__init__.py", line 2354, in __getattr__
return getattr(self.tk, attr)
AttributeError: module '_tkinter' has no attribute 'eval'
Process finished with exit code 1
Line 125 is this:
tclversion_detailed = tkinter.Tcl().eval('info patchlevel')
This, as per Tkinter documentation, should simply return the version of Tkinter library (https://tkdocs.com/tutorial/install.html).
When I ran a dummy program using just Tkinter to test if Tkinter alone works. I got another error:
Traceback (most recent call last):
File "E:/Projekty Python/dcs_assistant/test.py", line 3, in <module>
tk = tkinter.Tk()
File "C:\Users\pawni\Miniconda3\envs\dcs_assistant\lib\tkinter\__init__.py", line 2272, in __init__
self._loadtk()
File "C:\Users\pawni\Miniconda3\envs\dcs_assistant\lib\tkinter\__init__.py", line 2286, in _loadtk
tk_version = self.tk.getvar('tk_version')
AttributeError: module '_tkinter' has no attribute 'getvar'
Process finished with exit code 1
The code for the dummy program was:
import tkinter
from tkinter.constants import *
tk = tkinter.Tk()
frame = tkinter.Frame(tk, relief=RIDGE, borderwidth=2)
frame.pack(fill=BOTH,expand=1)
label = tkinter.Label(frame, text="Hello, World")
label.pack(fill=X, expand=1)
button = tkinter.Button(frame, text="Exit", command=tk.destroy)
button.pack(side=BOTTOM)
tk.mainloop()
I tried to uninstall and re-install Tkinter again using both pip and conda. No joy.
Related
I am trying to make an exit button with the code
I do import tkinter* in here
from tkinter import*
from tkinter import ttk
from PIL import Image,ImageTk
import os
def iExit(self):
self.iExit=tkinter.askyesno("Face Recognition","Are you sure you want to exit?")
if self.iExit >0:
self.root.destroy()
else:
return
and I got the error
I know that I cant put tkinter inside the code but i don't know how to solve it. The error that I am getting is here
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\tkinter\__init__.py", line 1948, in __call__
return self.func(*args)
^^^^^^^^^^^^^^^^
File "c:\Users\user\Desktop\FYP\Facial_Recognition system\Main.py", line 148, in iExit
self.iExit=tkinter.askyesno("Face Recognition","Are you sure you want to exit?")
^^^^^^^
NameError: name 'tkinter' is not defined
askyesno is defined in the tkinter.messagebox module. Import it from there:
from tkinter.messagebox import askyesno
When I try to add an image and set it as my GUI window logo it gives me these errors
Traceback (most recent call last):
File "C:\Users\Meina Jia\PycharmProjects\guwindow\main.py", line 7, in <module>
icon = PhotoImage(file='logo.jpg')
File "C:\Users\Meina Jia\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 4093, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\Meina Jia\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 "logo.jpg"
Process finished with exit code 1
I already changed the file type using code.
from tkinter import *
window = Tk()
window.geometry("420x420")
window.title("Backrooms in A Nutshell")
icon = PhotoImage(file='logo.jpg')
window.iconphoto(True, icon)
window.mainloop()
For this, you will need the Pillow Library.
import tkinter as tk
from PIL import ImageTk, Image
window = tk.Tk()
window.geometry("420x420")
window.title("Backrooms in A Nutshell")
icon = ImageTk.PhotoImage(Image.open('logo.jpg'))
window.iconphoto(True, icon)
window.mainloop()
Note: It is good practice to import a library directly:
Run
import tkinter
instead of
from tkinter import *
The program:
from tkinter import *
root = Tk()
canvas = Canvas(root)
canvas.pack()
root.mainloop()
error for this program:
c:/Users/ADMIN/Desktop/python/tkinter.py
Traceback (most recent call last):
File "c:\Users\ADMIN\Desktop\python\tkinter.py", line 1, in <module>
from tkinter import *
File "c:\Users\ADMIN\Desktop\python\tkinter.py", line 3, in <module>
root = Tk()
NameError: name 'Tk' is not defined
is the problem I am still facing in my computer. please help me to rectify this error
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.
We implemented a distributed chat, which is using a Tkinter GUI. As I updated my system to Fedora18 Im getting exceptions when calling a Tkinter event, almost the same as described here:
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib64/python2.7/threading.py", line 551, in bootstrap_inner self.run()
File "/usr/lib64/python2.7/threading.py", line 504, in run self.target(*self.__args, **self.__kwargs)
File "/hachat/peer.py", line 156, in startRecvLoop
self.processMessage(msg, addr)
File "/hachat/peer.py", line 222, in processMessage
self.gui.receive(msg)
File "/hachat/gui.py", line 74, in receive
self.textfenster.insert(END,msg.name+": "+msg.text+'\n')
File "/usr/lib64/python2.7/lib-tk/Tkinter.py", line 2986, in insert
self.tk.call((self._w, 'insert', index, chars) + args)
TclError: out of stack space (infinite loop?)
Here is a snipped out of the gui-class:
import Tkinter
import ScrolledText
import tkMessageBox
import tkSimpleDialog
import threading
class gui(object):
def __init__(self, parent):
self.root = Tkinter.Tk()
self.textfenster = ScrolledText.ScrolledText(self.fpopup,width=90,height=24,background='white')
self.textfenster.pack(fill=Tkinter.BOTH, expand=Tkinter.YES)
def run(self):
self.guiRunThread = threading.Thread(target=self.root.mainloop())
self.guiRunThread.daemon = True
self.guiRunThread.start()
def receive(self,msg):
self.textfenster.insert(Tkinter.END,msg.name+": "+msg.text+'\n')
self.textfenster.see(Tkinter.END)
The Exception appears only on my system, the reason seems to be that tk was not compiled with support for threads. I have to get rid of this Exeption - as the program is distributed it needs to run on different systems. So Im asking for how to get rid of this exception as well as an hint to get tk for supporting threads.
Im using Python version is 2.7.3, Tcl/Tk version 8.5. import Tkinter; Tkinter.Tk().tk.eval("puts $tcl_platform(threaded)") is giving back a Exception as well.
I solved the problem with Queues to communicate with Tk. See Mutli-threading python with Tkinter for an example!
This one works:
from Tkinter import *
from ScrolledText import ScrolledText
from threading import Thread
scrolled = None
def start():
global scrolled
root = Tk()
scrolled = ScrolledText(root)
scrolled.pack(fill=BOTH, expand=YES)
return root
Thread(target=start().mainloop).start()
print scrolled.get(0.0, END)