I have this code, but why isn’t .ico file defined when setting window’s icon?
from tkinter import *
from PIL import ImageTk,Image
root = Tk()
root.title("Tkinter App")
root.iconbitmap('C:\Users\User\Desktop\Main\yazilimfoto\Ataturk1.jpg')
root.mainloop()
there are some problems in your code
the icon you are setting is in .jpg format.
then use
ico = Image.open('test.jpg')
photo = ImageTk.PhotoImage(ico)
root.wm_iconphoto(False, photo)
and then
root.iconbitmap(photo)
and it will work. remember that you don't need to insert the entire file path (C:\Users\User\Desktop\Main\yazilimfoto\Ataturk1.jpg) but only the file path in your .py file directory
Related
I was just learning python gui and this error showed up.
_tkinter.TclError: couldn't open "download.gif": no such file or directory
this the code
from tkinter import *
window = Tk()
icon = PhotoImage(file= 'download.gif')
window.iconphoto(True , icon)
window.mainloop()
You can access your file by specifying the file path.
like "/cd:/yourp-path/../../images/dowload.gif"
for some reason, Tkinter can't open my image. If I don't add from tkinter import * it shows error message as:
Error Message without from tkinter import *:
C:\Users\NG>python e:/PythonTkinter/app.py
Traceback (most recent call last):
File "e:/PythonTkinter/app.py", line 12, in <module>
logo = Image.open('logo.png')
File "C:\Users\NG\anaconda3\lib\site-packages\PIL\Image.py", line 2891, in open
fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'logo.png'
and if I add from tkinter import * as shown below, it shows error message as shown below.
Code:
import tkinter as tk
import PyPDF2
from PIL import Image, ImageTk
from tkinter import *
# begaining of our UI window
root = tk.Tk()
canvas = tk.Canvas(root, width=600, height=300)
canvas.grid(columnspan=3)
# Adding logo
# logo = ImageTk.PhotoImage(Image.open("logo.png"))
logo = Image.open("logo.png")
logo = ImageTk.PhotoImage(logo)
logo_label = tk.Label(image=logo)
loogo_label.image = logo
logo_label.grid(column=1, row=0)
# ending of our UI window
root.mainloop()
Error Message with from tkinter import *:
C:\Users\NG>python e:/PythonTkinter/app.py
Traceback (most recent call last):
File "e:/PythonTkinter/app.py", line 14, in <module>
logo = Image.open("logo.png")
AttributeError: type object 'Image' has no attribute 'open'
Image is right there in same folder where this python file is.
WHAT AM I DOING WRONG ? HELP!
Explanation:-
You should probably understand what relative path is. When relative path is used, it is not relative the location of the python file, but instead the location from where you are running the python file. Here you are running the file from:
C:\Users\NG
But your python file and the image you are using is inside:
e:/PythonTkinter/app.py
Solution:-
So here either you can change the location from where you're running the code to the location with image file OR you can copy the image to the location you are running the py file from(i.e., 'C:\Users\NG').
And as far as the second error is concerned, it's never a good idea to say from x import *. When you import '*' from tkinter, it replaces the PIL.Image with tkinter.Image. And hence the error. So either remove that line or move it to the top most. Recommended import is:
import tkinter as tk
import PyPDF2
from PIL import Image, ImageTk
You are just using the first import here, so I don't see a point in using from tkinter import *, so just remove it.
You can simply do as follows
from tkinter import *
window = Tk() # instantiate an instance of a window for us
window.geometry("500x500")
#creating a photo image from png photo
icon = PhotoImage(file='GUI using Python\\flower.png')
window.iconphoto(True, icon)
window.mainloop()
starting from the comment (creating a photo image from png photo), you give a value in the file parameter the relative path not the full path, also take care of the backslash, you should write two backslashes and no need for all of these libraries that you are using unless you are counting on them for other tasks.
Your PIL import should be after 'from tkinter import *'. Also make sure to be in the same directory as the image
I'm trying to load an image in the tkinter window but it only shows errors like: "no such file or directory", is this for another version of python? If so what could i use for python 3.6?
from tkinter import *
# pip install pillow
from PIL import Image, ImageTk
load = Image.open("hello.jpg")
render = ImageTk.PhotoImage(load)
img = Label(self, image=render)
img.image = render
img.place(x=0, y=0)
I would check which directory the hello.jpg file is in. The error you're getting means that it couldn't find that file where it was looking. It currently is looking in the same folder that the python file is in
Solution
Make sure that the directory which this python script is in also has the image
Your file should look something like this:
You should have some folder that this python file is in, and in that same folder that the python file is in you should have the TKinter images
On repl.it you should press this button:
Then add the hello.jpg file
You also need to change your code to remove the random indents as well as adding root = Tk() to "activate" TkInter
Your new code should look like this:
from tkinter import *
root = Tk()
# pip install pillow
from PIL import Image, ImageTk
load = Image.open("hello.jpg")
render = ImageTk.PhotoImage(load)
img = Label(self, image=render)
img.image = render
img.place(x=0, y=0)
In other words if I pass off the folder to someone containing a .txt and .py, and the script is run on their machine from this folder, how can I ensure the file dialog will open with this folder to select the .txt without knowing the absolute path? Referring to initialdir=??
from tkinter import filedialog
from tkinter import *
root = Tk()
root.withdraw()
root.filename = filedialog.askopenfilename(initialdir='/python', title="Select file",
filetypes=[("Text Files", "*.txt")])
print(root.filename)
You should use the os module and os.getcwd() to find the current working directory of the .py file
import os
from tkinter import filedialog
import tkinter as tk
root = tk.Tk()
root.withdraw()
root.filename = filedialog.askopenfilename(initialdir=os.getcwd(), title="Select file",
filetypes=[("Text Files", "*.txt")])
print(root.filename)
I would also suggest doing import tkinter as tk instead as importing everything may lead to naming conflicts if you are not careful, plus it's much easier to determine that what you are referring to came from the tkinter module when you prefix it with tk
If I have:
from Tkinter import *
app = Tk()
...
app.mainloop()
Would I use app.iconbitmap(...)?
And if so, how would I go about using this as the file, and should I import urllib?
You can use this too replace the Tkinter default icon.
import base64, PIL, urllib, Tkinter
from Tkinter import *
from PIL import ImageTk
from urllib import *
root = Tk()
raw_data = urllib.urlopen("http://dl.dropboxusercontent.com/s/qtlincxkbbiz1qv/stat.gif").read()
b64_data = base64.encodestring(raw_data)
image = PhotoImage(data=b64_data)
root.tk.call('wm', 'iconphoto', root._w, image)
root.mainloop()
And then change the .py file extension to .pyw to change the taskbar icon.
The .pyw extension tells it to run with pythonw.exe instead of python.exe, but running with pythonw.exe also makes it run without the console.
So, you'll either have to run without the icon, or without the console.
This is the call that worked for me on both Windows and Linux. I found that I cannot use ico files on Linux, so only using gif files which works on both platforms.
class Editor(tk.Tk):
. . .
. . .
self.tk.call('wm', 'iconphoto', self._w, tk.PhotoImage(file = "my_icon.gif"))