I'm currently trying to create a Python3 Tkinter program which I can launch by double clicking or opening from other python scripts, instead of going through idle.
So far I've had little luck, as when I attempt to launch the script the console opens for a moment and then crashes.
EDIT: Removing the logo segment of code allows the program to run. Any ideas why and how to fix it? Also I have not had to run a program via the console before so little luck there.
from tkinter import *
from tkinter import ttk
root = Tk()
root.title("Kinematics")
Logo_frame = LabelFrame(root, bg = "white")
Logo_frame.grid(row=0, column=12, sticky = "NSEW")
#Logo
Logo = Image(file="Logo-S.gif")
image_label = ttk.Label(Logo_frame, image=Logo)
image_label.grid(row=0, column=0, rowspan = 3)
root.mainloop()
The error from your present code is:
Exception ignored in: <bound method Image.__del__ of <tkinter.Image object at 0x7f1aa91df2e8>>
Traceback (most recent call last):
File "/usr/lib/python3.5/tkinter/__init__.py", line 3357, in __del__
if self.name:
AttributeError: 'Image' object has no attribute 'name'
Traceback (most recent call last):
File "/home/sunbear/Coding/python35/tkinter/stackoverflow_questions/test52.py", line 22, in <module>
Logo = Image(file="Logo-S.gif")
TypeError: __init__() missing 1 required positional argument: 'imgtype'
There is a typo in your command in line 22(your original code before edit). You need the following correction to overcome this error, i.e.:
#Logo
#Logo = Image(file="Logo-S.gif")
Logo = PhotoImage(file="Logo-S.gif") #you should change to this.
You can read more about when to use PhotoImage and Image using this link I gave you.
I just attempted to run the program with a random gif I found online, all seemed to work to plan. I'm running windows 10 pro... maybe try re-saving/downloading the gif.
Related
My full code
from tkinter import *
i=0
for i in range(10) :
window = Tk()
window.title('add image')
window = Canvas(window,width= 600, height= 600)
window.pack()
image=PhotoImage(file=r"C:\\Users\\Konstantinos\\New folder\\hello.png")
window.create_image(0,0, anchor = NW, image=image)
window.mainloop()
The error when i run the program
File "C:\Programms\Lib\tkinter\__init__.py", line 2832, in _create
return self.tk.getint(self.tk.call(
^^^^^^^^^^^^^
_tkinter.TclError: image "pyimage2" doesn't exist
The error when i debug the program
Exception has occurred: TclError
image "pyimage2" doesn't exist
File "C:\Users\Konstantinos\New folder\demo.py", line 9, in <module>
window.create_image(0,0, anchor = NW, image=image)
So basically, the program opens an image multiple times. When th program is not in a loop it works but when i put it in a loop it gives me the error. Because i recently started programming i dont really know how to solve the problem and I have looked in other threads with the similar problem but none apply to me. I will appreciate any answer
The error probably comes from multiple Tk instances. Try removing the for-loop and then it will work. But if your intention was for multiple windows, then you can look into this answer: https://stackoverflow.com/a/36316105/9983213. Feel free to tinker around with the example.
A smaller example is:
import tkinter as tk
root = tk.Tk()
for i in range(5):
top = tk.Toplevel(root)
root.mainloop()
When I'm trying to run a script to see if I can use tkinter on VsCode it throws a NameError saying name 'Tk' is not defined. Furthermore I can run it on IDLE and it runs just fine. I've been searching around to see if I can fix it but I still can't get it working. Do you have any idea what I'm doing wrong?
Here is the code:
from tkinter import *
root = Tk()
myLabel = Label(root, text = 'Hello World!')
myLabel.pack()
Do NOT name your file tkinter.py because the tkinter module you are trying to import is actually importing the file itself. And since there's no function called Tk in your file, you are getting that error. Rename the file to something else.
For example, rename it to gui.py.
Also, it's better to be explicit rather than implicit in python. So instead of
# Pollutes your namespace
# May clash with the functions you define or functions of other libraries that you import
from tkinter import *
root = Tk()
...
you should use
import tkinter as tk
root = tk.Tk()
...
Here's an example of how it can clash with other namespaces:
from tkinter import *
root = Tk()
Label = "hello"
Label1 = Label(gui, text=Label)
This results in an error:
Traceback (most recent call last):
File "stackoverflow.py", line 98, in <module>
Label1 = Label(gui, text=Label)
TypeError: 'str' object is not callable
I am fairly new to python and very new to GUI's so please let me know if I have made an obvious mistake.
This is also my first post so please let me know if I need to edit my question in some way.
I don't understand why I am getting errors when trying to set an icon to a tkinter GUI.
I have already tried the following posts:
tkinter TclError: error reading bitmap file
https://www.delftstack.com/howto/python-tkinter/how-to-set-window-icon-in-tkinter/
There are other stack exhange posts on this topic but they are for windows and/or their OP's normally can get the icon to apear once then it doesn't work. This isn't my situation the programs fail to run. Though I have tried some of their suggestions as well.
I have tried three different ways of coding this:
Method 1:
from tkinter import *
import os
class MainWindow:
def __init__(self, master):
self.master = master
master.title('TD')
#self.iconbitmap(default = 'fist.ico')
root = Tk()
root.iconphoto(True, PhotoImage(os.path.join(os.getcwd(),'favicon-3.png')))
my_gui = MainWindow(root)
root.mainloop()
Result:
runfile('~./envs/thermTD_project/thermTD/untitled2.py', wdir='~./envs/thermTD_project/thermTD')
Traceback (most recent call last):
File "~./envs/thermTD_project/thermTD/untitled2.py", line 23, in <module>
root.iconphoto(True, PhotoImage(os.path.join(os.getcwd(),'favicon-3.png')))
File "/usr/lib/python3.8/tkinter/__init__.py", line 2116, in wm_iconphoto
self.tk.call('wm', 'iconphoto', self._w, "-default", *args)
TclError: can't use "~./envs/thermTD_project/thermTD/favicon-3.png" as iconphoto: not a photo image
File existence check:
In [42]: os.path.exists(os.path.join(os.getcwd(),'favicon-3.png'))
Out[42]: True
Method 2:
from tkinter import *
import os
class MainWindow:
def __init__(self, master):
self.master = master
master.title('TD')
#self.iconbitmap(default = 'fist.ico')
root = Tk()
root.iconbitmap(os.path.join(os.getcwd(),'favicon-3.ico'))
my_gui = MainWindow(root)
root.mainloop()
Result:
In [43]: runfile('~./envs/thermTD_project/thermTD/untitled2.py', wdir='~./envs/thermTD_project/thermTD')
Traceback (most recent call last):
File "~./envs/thermTD_project/thermTD/untitled2.py", line 22, in <module>
root.iconbitmap(os.path.join(os.getcwd(),'favicon-3.ico'))
File "/usr/lib/python3.8/tkinter/__init__.py", line 2071, in wm_iconbitmap
return self.tk.call('wm', 'iconbitmap', self._w, bitmap)
TclError: bitmap "~./envs/thermTD_project/thermTD/favicon-3.ico" not defined
File existence check:
In [44]: os.path.exists(os.path.join(os.getcwd(),'favicon-3.ico'))
Out[44]: True
Method 3:
from tkinter import *
import os
class MainWindow:
def __init__(self, master):
self.master = master
master.title('TD')
#self.iconbitmap(default = 'fist.ico')
root = Tk()
img = PhotoImage(os.path.join(os.getcwd(),'favicon-3.ico'))
root.tk.call('wm','iconphoto', root._w, img)
my_gui = MainWindow(root)
root.mainloop()
Result:
In [45]: runfile('~./envs/thermTD_project/thermTD/untitled2.py', wdir='~./envs/thermTD_project/thermTD')
Traceback (most recent call last):
File "~./envs/thermTD_project/thermTD/untitled2.py", line 21, in <module>
root.tk.call('wm','iconphoto', root._w, img)
TclError: can't use "~./envs/thermTD_project/thermTD/favicon-3.ico" as iconphoto: not a photo image
File existence check:
See method 1
The code without any of the icon setting lines works as expected.
I have tried to gif, png and ico on each method. I am trying other file types now.
NB: While the filename is favicon actually they are ico, gif, png files of 64x64 pixles. Even the favicon ico only has 1 type of icon in it (if thats the right phrase). Its just I got them from a favicon which I converted to different formats using mogrify.
OS: Ubuntu 20.04.1 LTS
python 3.8.2
IDE: spyder 4.1.5
P.S. I would be gratefull to know what the syntax for including this in my init function. I am trying to do object orientated GUI's I am very much still learning.
While trying the below code:
import tkinter
print(tkinter.TkVersion)
print(tkinter.TclVersion)
mainWindow = tkinter.Tk()
mainWindow.title("Hello Python")
mainWindow.geometry("1200X1024")
mainWindow.mainloop()
I am getting an error:
Traceback (most recent call last):
File "C:\Users\Satya\IdeaProjects\GUO_python\tkinter1.py", line 11, in <module>
mainWindow.geometry("1200X1024")
File "C:\Users\Satya\Anaconda3\lib\tkinter\__init__.py", line 1835, in wm_geometry
return self.tk.call('wm', 'geometry', self._w, newGeometry)
_tkinter.TclError: bad geometry specifier "1200X1024"
https://i.stack.imgur.com/EJyn6.jpg
The arguments you passed to geometry aren't quite right.
You have written 1200X1024 - but that X needs to be lowercased.
Try mainWindow.geometry("1200x1024").
You can use also add "+" to specify where you want your window to position on your screen
("1200x1024+100+10")
import tkinter
print(tkinter.TkVersion)
print(tkinter.TclVersion)
mainWindow = tkinter.Tk()
mainWindow.title("Hello Python")
mainWindow.geometry("1200x1024") # use "x" instead of " X"
mainWindow.mainloop()
Also, if you set 1200x1024 (and not 'X') to make your window taking your whole screen you can use mainWindow.state('zoomed')
So I am running python 2.6 on a macbook pro and trying to write the code in python to display an image from a file in a label on a tkinter gui. The image is called image.png. The program runs without errors when I use this code
i = Image.open("image.png")
but when I do this code (I add one line):
i = Image.open("image.png")
photo = ImageTk.PhotoImage(i)
The program will crash and say "Bus error" in the command line. I don't even know what that means. I would think that PIL is installed correctly, since Image works, but the fact that ImageTk does not work puzzles me. Can anybody tell me what might be causing this Bus error?
EDIT:
Well I made a new program to test the error further. Here is the exact script I ran:
import Image
import ImageTk
i = Image.open("image.png")
photo = ImageTk.PhotoImage(i)
Now instead of getting "Bus error", this is my traceback.
Traceback (most recent call last):
File "imageTest.py", line 5, in <module>
photo = ImageTk.PhotoImage(i)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/PIL/ImageTk.py", line 113, in __init__
self.__photo = apply(Tkinter.PhotoImage, (), kw)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/Tkinter.py", line 3285, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-tk/Tkinter.py", line 3226, in __init__
raise RuntimeError, 'Too early to create image'
RuntimeError: Too early to create image
Exception AttributeError: "PhotoImage instance has no attribute '_PhotoImage__photo'" in <bound method PhotoImage.__del__ of <ImageTk.PhotoImage instance at 0x3c7a30>> ignored
I don't know about the Bus Error, but you need to create a Tk window before you can call PhotoImage. This script works for me-
import Image
import ImageTk
from Tkinter import Tk
window = Tk()
i = Image.open("image.png")
photo = ImageTk.PhotoImage(i)
ImageTk.PhotoImage has a garbage collection (ref count) bug in it. You must place a reference to the PhotoImage object in either a global variable of a class instance variable (e.g., self.myphoto = ImageTk.PhotoImage(i)).
See this warning:
http://infohost.nmt.edu/tcc/help/pubs/pil/image-tk.html
Even thought you do need to call a Tk window you also need to set the directory so that it can find the image.png.
import os
import Image
import ImageTk
from Tkinter import Tk
os.chdir('C:/../../') # put file path for the image.
window = Tk()
i = Image.open("image.png")
photo = ImageTk.PhotoImage(i)
window.mainloop()