How can I set a tkinter app icon with a URL? - python

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"))

Related

Why isn’t .ico file defined when setting window’s icon tkinter

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

Removing the py.exe at the background

I am working on tkinter project. While double clicking the python file, the py.exe file is visible in the background which doesn't look good. so is there a way to make the py.exe invisible or resize it.Please help me with your ideas. Thank you
Sample code:
import tkinter.messagebox
from tkinter import ttk
class Demo1:
data = []
def __init__(self, master):
self.master = master
self.label=tkinter.Label(text="Add IP/Hostname")
self.label.pack()
self.t=tkinter.Text(self.master,height=20,width=50)
self.t.pack()
self.button = tkinter.Button(self.master,height=3,width=10, text="OK"
)
self.button.pack()
def main():
root = tkinter.Tk()
app = Demo1(root)
root.mainloop()
if __name__ == '__main__':
main()
Change the .py extension to .pyw. That should suppress the console window.
Note: the .pyw extension should be associated to be opened with pythonw.exe. Normally this is done by the Python installer. If not, select pythonw.exe the first time you click on a .pyw file. That will make the proper association.
Does that work?
import ctypes
ctypes.windll.user64.ShowWindow(ctypes.windll.kernel64.GetConsoleWindow(), 6 )
Otherwise please post an example code.

Compiling Python code with an image and sounds folder?

I am currently working on a project, and in the end I'd need to compile it. The issue that I am facing is that I am working with the .py file, but also 2 folders, one with all of the images, and one with all of the music...
I've looked around, but nothing answers the questions completely. I've seen that it was best to base64 encode my images, but that didn't seem to work for me... I tried UTF-8 characters and binary.
Any idea on how I could transform my 2 folders and my code file into a single .exe executable, that can be used on any computer?
Thanks
Angaros
Have you tried this kind of approach:
Let's assume you have some kind of layout like this
main.py
resources.py
data/img1.png
And in main.py:
import resources
import tkinter as tk
root = tk.Tk()
root.title("Test")
resources.load_images()
lbl = tk.Label(root, image=resources.IMG1)
lbl.place(relx=0.5, rely=0.5, anchor="c")
root.mainloop()
And in resources.py:
import tkinter as tk
IMG1 = None
def load_images():
global IMG1
IMG1 = tk.PhotoImage("data\\img1.png")
This works quite nicely, and can load your image.
Now let's make it work in one single exe file:
We don't need to change main.py, as it simply loads resources through our resources.py script.
We can load/save our image data using this script:
import base64
def load_data(filename):
with open(filename, mode="rb") as f:
return base64.b64encode(f.read())
print(str(load_data("data\\img1.png"), "utf-8"))
# R0lGOD ...
So we can now copy and paste this into our resources.py file, and change it to look like this:
import tkinter as tk
# here's our copied base64 stuff:
img1_data = '''
R0lGODlhEAAQALMAAAAAAP//AP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAA\nAAAAACH5BAEAAAIALAAAAAAQABAAQAQ3UMgpAKC4hm13uJnWgR
TgceZJllw4pd2Xpagq0WfeYrD7\n2i5Yb+aJyVhFHAmnazE/z4tlSq0KIgA7\n
'''
IMG1 = None
def load_images():
global IMG1
IMG1 = tk.PhotoImage(data=img1_data)
And now compile our main.py file using PyInstaller:
pyinstaller main.py --onefile
And we should see, once the task is complete, that there is a single executable file in dist, called main.exe.

how do i open a jpg image inside a tkinter window in python 3.6?

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)

How do I upload an image on Python using Tkinter?

I am doing GUI programming using Tkinter on Python. I am using the grid manager to make widgets. I have created several buttons and I want to upload an image on top of them. When I enter this code, it gives me an escape sequence error.
I heard using PIL is not a good idea? Is that true?
cookImage = PhotoImage(file = "image/C:\Users\terimaa\AppData\Local\Programs\Python\Python36-32\cook.gif")
Windows filenames must be entered as raw strings:
cookImage = PhotoImage(file=r"C:\Users\terimaa\AppData\Local\Programs\Python\Python36-32\cook.gif")
This applies to all of Python, not just PIL.
Use:
path = r"a string with the path of the photo"
Note the r prefix, it means a raw string.
...
img = ImageTk.PhotoImage(Image.open(file=path))
label = tk.Label(root, image = img)
label.something() #pack/grid/place
...
The path can be:
Absolute ("C:\Users\terimaa\AppData\Local\Programs\Python\Python36-32\cook.gif")
Relative ("\cook.gif", depends on where the Python code is)
If you have an image file that is exactly what you want, just open it with BitmapImage or PhotoImage. Note that Tcl/Tk 8.6, which you should have with 3.6 on Windows, also reads .png files. On Windows, prefix the filename with 'r' or use forward slashes: 'C:/User/...'.
The actual PIL package is no longer maintained and only works on 2.x. That is what a new user should not use. The compatible successor, pillow (installed for instance with python -m pip install pillow) is actively maintained and works with 3.x. The compatibility extends to the import statement: import PIL imports pillow. Pillows allows one to manipulate images and to convert many formats to tk format (the ImageTk class).
this is exact code which is most help for move image
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
import os, shutil
class Root(Tk):
def __init__(self):
super(Root,self).__init__()
self.title("thinter Dialog Widget")
self.minsize(640,400)
self.labelFrame = ttk.LabelFrame(self,text="Open A File")
self.labelFrame.grid(column=0,row=1,padx= 20, pady= 20)
self.btton()
def btton(self):
self.button = ttk.Button(self.labelFrame, text="Browse Afile", command=self.fileDailog)
self.button.grid(column=1,row=1)
def fileDailog(self):
self.fileName = filedialog.askopenfilename(initialdir = "/", title="Select A File",filetype=(("jpeg","*.jpg"),("png","*.png")))
self.label = ttk.Label(self.labelFrame, text="")
self.label.grid(column =1,row = 2)
self.label.configure(text = self.fileName)
os.chdir('e:\\')
os.system('mkdir BACKUP')
shutil.move(self.fileName,'e:\\')
if __name__ == '__main__':
root = Root()
root.mainloop()
you could not move image to c drive due to Permission denied: this code work successfully on python 3.8, 3,7

Categories

Resources