I am using Python 3.5.0 on Windows 10 and want to replace this:
To change the icon you should use iconbitmap or wm_iconbitmap I'm under the impression that the file you wish to change it to must be an ico file.
import tkinter as tk
root = tk.Tk()
root.iconbitmap("myIcon.ico")
If you haven't an icon.ico file you can use an ImageTk.PhotoImage(ico) and wm_iconphoto.
import tkinter as tk
from PIL import Image, ImageTk
root = tk.Tk()
ico = Image.open('test.jpg')
photo = ImageTk.PhotoImage(ico)
root.wm_iconphoto(False, photo)
root.mainloop()
Note:
If default is True, this is applied to all future created toplevels as
well. The data in the images is taken as a snapshot at the time of
invocation.
Detailed implementations under different OS:
On Windows, the images are packed into a Windows icon structure. This
will override an ico specified to wm iconbitmap, and vice versa.
On X, the images are arranged into the _NET_WM_ICON X property, which
most modern window managers support. A wm iconbitmap may exist
simultaneously. It is recommended to use not more than 2 icons,
placing the larger icon first.
On Macintosh, this sets the Dock icon with the specified image.
See more
Supported formats since TkVersion 8.6 of tk.PhotoImage(filepath):
PNG
GIF
PPM/PGM
Therefore code can be simplified with a .png file to:
import tkinter as tk
root = tk.Tk()
photo = tk.PhotoImage(file = 'test.png')
root.wm_iconphoto(False, photo)
root.mainloop()
input for tkinter
from tkinter import *
app = Tk()
app.title('Tk')
app.geometry('')
app.iconbitmap(r'C:\Users\User\PycharmProjects\HelloWorld\my.ico')
app.mainloop()
input for pyinstaller
pyinstaller --onefile -w -F --add-binary "my.ico;." my.py
Here is another solution, wich doesn't force you to use an ico file :
from tkinter import *
root = Tk()
root.geometry("200x200")
root.iconphoto(False, tk.PhotoImage(file='C:\\Users\\Pc\\Desktop\\icon.png'))
root.mainloop()
You must not have favicon.ico in the same directory as your code or namely on your folder. Put in the full Pathname. For examples:
from tkinter import *
root = Tk()
root.iconbitmap(r'c:\Python32\DLLs\py.ico')
root.mainloop()
This will work
If you are using CustomTkinter:
app.wm_iconbitmap('yt.ico')
CustomTkinter documentation
from tkinter import *
root = Tk()
root.title('how to put icon ?')
root.iconbitmap('C:\Users\HP\Desktop\py.ico')
root.mainloop()
Here's another way via Tcl command:
import tkinter as tk
window=tk.Tk()
window.tk.call('wm', 'iconphoto', win._w, tk.PhotoImage(file=r"my_icon.png"))
window.mainloop()
Related
I have prepared some tk application. It could be really simple like:
from tkinter import *
# create root window
root = Tk()
# root window title and dimension
root.title("Welcome to GeekForGeeks")
# Set geometry (widthxheight)
root.geometry('350x200')
# all widgets will be here
# Execute Tkinter
root.mainloop()
I have using some method to convert the app to the exe file.
What is important,
I'm not using and I cannot do it with pyinstaller py2exe etc. I also cannot use method with changing my app.py to app.pyw.
But my conversion to .exe is working correctly.
The question - is it even possible to hide/disable/resize(reduce the size) of my console window and make the application still working?
I'm not exactly sure how is it done in pyinstaller py2exe etc, so maybe is it possible to do it inside an application?
All right, to solve above problem install:
pip install pywin32
and add code before running your tk gui application:
import win32gui
import win32.lib.win32con as win32con
the_program_to_hide = win32gui.GetForegroundWindow()
win32gui.ShowWindow(the_program_to_hide , win32con.SW_HIDE)
Then you can run the main.py in console, the console will disappear and the gui app will be still visible.
In case when you use pyinstaller etc - you can convert the application without "--noconsole" argument.
When you run the .exe file the console will appear for a second, and disappear. But the gui app will be still visible and usable.
Hope it help somebody somehow :)
I think you should run your script using pythonw.exe instead of python.exe. See .pyw files in python program
Does this help if using Toplevel?
from tkinter import *
root = Tk()
root.title("Main Window")
root.geometry("200x200")
def launch():
global second
second = Toplevel()
second.title("Child Window")
second.geometry("400x400")
def show():
second.deiconify()
def hide():
second.withdraw()
Button(root, text="launch Window", command=launch).pack(pady=10)
Button(root, text="Show", command=show).pack(pady=10)
Button(root, text="Hide", command=hide).pack(pady=10)
root.mainloop()
OS: Linux - Ubantu
Image I have used: It was originally .ico but I converted online to .xbm
I tried different images and different websites but nothing seems to work
Code:
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
root.title("B")
root.iconbitmap('#/home/zaki/Pictures/mar.xbm')
root.mainloop()
The window appears normally but without icon
Try to use the iconphoto() method.
from tkinter import *
root = Tk()
img = PhotoImage(file="image.png") # Replace "image.png" with any image file.
root.iconphoto(False, img)
root.mainloop()
Works on Ubuntu 18.04 with Python 3.6.
References:
https://www.geeksforgeeks.org/iconphoto-method-in-tkinter-python/
I was trying to call the file dialog of ubuntu to choose a directory with python3.6, and the code looks like this:
from tkinter import filedialog
filedialog.askdirectory()
but when i run this, a very old version file dialog shows:
Any idea on how to call the newest file dialog of ubuntu using python?
It is not old version, it is standard theme for GTK. You would have to use theme to change it. But Linux has only three styles as default
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
root = tk.Tk()
root.style = ttk.Style()
print(root.style.theme_names())
root.style.theme_use('clam')
filedialog.askdirectory()
root.mainloop()
classis/default:
clam:
alt:
You can get more themes installing module
pip install ttkthemes
And code
import tkinter as tk
from tkinter import ttk
import ttkthemes
root = tk.Tk()
root.style = ttkthemes.ThemedStyle()
for i, name in enumerate(sorted(root.style.theme_names())):
b = ttk.Button(root, text=name, command=lambda name=name:root.style.theme_use(name))
b.pack(fill='x')
root.mainloop()
List of styles
kroc:
radiance:
The UI components provided by tkinter (and the underlying tk library) are different from the UI components provided by, say, the GTK or the Qt libraries that are probably used by your desktop.
tkinter has a set of alternative widgets, that you can access with
from tkinter.ttk import *
that support the look and feel of your desktop, but (afaict) unfortunately the
filedialog widget is not supported.
I know there are lot of similar questions, but there aren't any simple enough that I am able to understand. I have the following code:
import Tkinter as tk
from PIL import Image, ImageTk
class MainWindow:
def __init__(self, master):
canvas = Canvas(master)
canvas.pack()
self.pimage = Image.open(filename)
self.cimage = ImageTk.PhotoImage(self.pimage)
self.image = canvas.create_image(0,0,image=self.cimage)
filename = full_filename
root = tk.Tk()
x = MainWindow(root)
mainloop()
and I get the following error:
TclError: image "pyimage36" doesn't exist
I've read some stuff about the image objects getting garbage cleaned but I don't quite understand it.
Figured it out. For some reason, while running in the debugger, if any previous executions had thrown errors I get the "pyimage doesn't exist" error. However, if I restart the debugger (or no previously executed scripts have thrown errors), then the program runs fine.
I had the same error message when using spyder 3.3.6 the only way i could get the .png file to load and display after getting the 'Tinker pyimage error ' was to go to the Console and restart the kernel. After that i worked fine.
(Python 3.8)
If you are using a IDE with a console(such as Spyder) just call root.mainloop() in the console.
Odds are that you have a bunch of partially loaded tkinter GUI's that never managed to be executed due to an error that prevented the root.mainloop() function from being run.
Once you have run the root.mainloop() a bunch of GUI's will likely appear on screen. After you have closed all those GUI's try running your code again.
Image of multiple Tkinter GUI's appearing on screen
Read more about mainloop() here: https://pythonguides.com/python-tkinter-mainloop/
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
root.title("Title")
img = Image.open('Paste the directory path')
bg = ImageTk.PhotoImage(img)
lbl = Label(root, image=bg)
lbl.place(x=0, y=0)
mainloop()
I was getting the same error. Try this code this will help you.
Additionally, in case if you create a button and use it to open other window, then there use window = Toplevel(), otherwise it will again show the same error.
From programmersought
image “pyimage1” doesn’t exist
Because there can only be one root window in a program, that is, only one Tk() can exist, other windows can only exist in the form of a top-level window (Toplevel()).
Original code
import tkinter as tk
window = tk.TK()
Revised code
import tkinter as tk
window = tk.Toplevel()
Keep other code unchanged
https://www.programmersought.com/article/87961175215/
Well I have this:
import tkinter
gui = tkinter.Tk()
gui.iconbitmap(default='/home/me/PycharmProjects/program/icon.ico')
gui.mainloop()`
But when I run I get an error saying
Traceback (most recent call last):
File "/home/spencer/PycharmProjects/xMinecraft/GUI.py", line 17, in <module>
gui.iconbitmap(default='/home/me/PycharmProjects/program/icon.ico')
File "/usr/lib/python3.3/tkinter/__init__.py", line 1638, in wm_iconbitmap
return self.tk.call('wm', 'iconbitmap', self._w, '-default', default)
_tkinter.TclError: wrong # args: should be "wm iconbitmap window ?bitmap?"`
I'm trying to use tkinter to set a window I've made's icon. I'm using Pycharm installed on ubuntu 13.10. I've tried various things from changing '/' to '\' and adding a Z:// to the front because that's my partition's name. But I still get the error so please help.
You need to either specify the path as the first positional argument, or use the keyword argument "bitmap". It's rather poorly documented, but the bitmap argument is required; you can't just give the default keyword argument. In fact, the bitmap keyword argument has been removed in python 3.
However, you can only use .ico files on windows. On ubuntu and other linux boxes you need to use a .xbm file, and need to prefix it with "#"
This should work on windows only:
gui.iconbitmap('/home/me/PycharmProjects/program/icon.ico')
On ubuntu, it would need to be something like this:
gui.iconbitmap('#/home/me/PyCharmProjets/program/icon.xbm')
You can't just rename a .ico file to .xbm, they are completely different file formats.
Interesting bit of research
png, svg, ico didn't work
I found one xbm on my machine (xubuntu - Linux dist) , thanks to sqlitemanager
tool.xbm
note the # - the code is a modification of Lutz "Programming Python" Chapter 1, tkinter103.py
from tkinter import *
from tkinter.messagebox import showinfo
def reply(name):
showinfo(title='Reply', message='Hello %s!' % name)
top = Tk()
#img = PhotoImage(file='py-blue-trans-out.ico') #no
top.title('Echo')
top.iconbitmap('#tool.xbm') #yes
#top.iconphoto(True, PhotoImage(file='tool.xbm')) #no
Label(top, text="Enter your name:").pack(side=TOP)
ent = Entry(top)
ent.pack(side=TOP)
btn = Button(top, text="Submit", command=(lambda: reply(ent.get())))
btn.pack(side=LEFT)
top.mainloop()
Still in 2018 a high Rank google question.
what works for me in python3
is to use ico in Windows and gif in Linux :
if ( sys.platform.startswith('win')):
gui.iconbitmap('logo_Wicon.ico')
else:
logo = PhotoImage(file='logo.gif')
gui.call('wm', 'iconphoto', gui._w, logo)
There are two ways,
1) use xbm file in ubuntu as ubuntu will not able to read ico files. but issue here is xbm can display only black and white images.
2) use tkinter.photoimage to display icon image like below,
img = PhotoImage(file='your-icon')
root.tk.call('wm', 'iconphoto', root._w, img)
issue here is photoimage can read only GIF and PGM/PPM images.
see details here - https://stackoverflow.com/a/11180300
To display colored icons in linux you need to do it as shown below:
import tkinter
window = tkinter.Tk()
window.title("My Application")
img = tkinter.PhotoImage(file='~/pharmapos/pharmapos.png')
window.tk.call('wm', 'iconphoto', window._w, img)
window.mainloop()
I had to convert to an XBM format and use the following root.iconbitmap('#imagename.xbm') however my platform is Ubuntu and I discovered my os theme has no spot for he image....
this worked for me in linux mint:
from tkinter import *
from PIL import Image, ImageTk
main_fn=Tk()
log= Image.open("path_to_image.ico")
logo = ImageTk.PhotoImage(log)
main_fn.tk.call('wm', 'iconphoto', main_fn._w, logo)
main_fn.mainloop()
We can use iconphoto on linux. Colored icon works well too. You can use .png files. The .ico file can be converted using 'convert' utility.
convert icon.ico icon.png
First create a PhotoImage widget:
icon = tkinter.PhotoImage(file='icon.png')
Then use iconphoto to change the icon:
root = Tk()
root.iconphoto(False, icon)
Reference: Please have a look at this link
import tkinter
gui = tkinter.Tk()
gui.iconbitmap()
gui.mainloop()
In place of gui.iconbitmap(default='/home/me/PycharmProjects/program/icon.ico') i used gui.iconbitmap() this just works for me.