Replace text/image of Tkinter logo - python

I have a question regarding the Tkinter logo which appears in the top left corner of the Tk window. I would like to know how to change the name, and eventually replace the whole thing with an alternate logo. Is this possible?
The tried to write to the following property that i saw on another thread but no luck:
master=Tk()
master.title("name")
#Note I have also tried master.title= "name"
Does anybody have the secret?
Daniel W.

I remembered reading about this years ago and yes the thread was still there, but looks like it takes an icon bitmap only http://www.daniweb.com/software-development/python/threads/63769/tk-logo-on-gui-window If you find another way please post it.

from tkinter import Tk
frame = Tk()
frame.geometry('300x300') #Frame size (width,height)
frame.title('Frame Title') # Frame title
frame.iconbitmap('logo.ico') # Frame logo
frame.mainloop()
its work for me!.

Related

Get icon of root window to be used by second window tkinter

I was wondering if there is anyway to get the ico file of one window and use it in the same window, without getting to know the icon location.
from tkinter import *
root = Tk()
root.iconbitmap('img/icn.ico')
top = Toplevel()
root.mainloop()
Here I want top to have icon of root without saying top.iconbitmap() or top.iconphoto(), the closest ive got is top.tk.call('wm','iconbitmap') but I dont know what is to be done with this as i couldnt find a understandable documentation.
Why dont I want to use iconbitmap(), its basically that, with tkinter.messagebox you can see the messagebox automatically inherit the icons from the parent widget. I was trying to duplicate this effect. Where if the icon is the default tk icon, then show blank icon or else show the custom icon.
Thanks in advance :D
[I'm using links into the core Tk documentation here. It's much more accurate than the Tkinter docs for most things, and Tkinter is mostly an obvious thin wrapper around it.]
You don't want wm iconbitmap. That's been effectively obsolete for decades; it uses an object class — bitmap — that's not relevant these days as it is monochrome and uses the weirdest format. (Filenames need to be preceded by # to make them work.)
Instead, you want to manipulate the wm iconphoto of the toplevel windows concerned. These take true photo images (there are many image file formats you can load into them) and you can share them easily.
# Load the image from the file; can also use PNG and other formats
my_image = PhotoImage(file="image.gif")
# Apply the image as the icons
first_toplevel_window.iconphoto(False, my_image)
second_toplevel_window.iconphoto(False, my_image)
Note that how the icon is displayed can vary wildly; it's not under your control.
You can use iconphoto() and set the first argument to True, then the same icon will be used for future created toplevels as well:
import tkinter as tk
root = tk.Tk()
icn = tk.PhotoImage(file='my-icon.png')
root.iconphoto(True, icn)
top = tk.Toplevel(root)
root.mainloop()
If you use the default instead of the bitmap (or first) argument, the icon will automatically be used on all TopLevel windows:
root.iconbitmap('img/icn.ico') # icon set only on root
root.iconbitmap(bitmap='img/icn.ico') # same as above
root.iconbitmap(default='img/icn.ico') # icon set on root and all TopLevels

"TclError: image", while adding a image to a Label

I have been stuck trying to add an image to my tkinter GUI and google does not seem to give answers. I understand that I should not use grid or pack gemoetry managers within the same master window, and I havent as far as I can tell but every attempt has resulted in either of the following error messages:
TclError: cannot use geometry manager grid inside . which already has slaves managed by pack
or:
TclError: image "pyimage86" doesn't exist
Incidently everytime I rerun my code the "pyimage86" changes, every run increases the number by 1, for example 'pyimage86', 'pyimage87', etc etc.
The first error message is particularly confusing because I am using .grid to place my labelled image into the class but the error is saying otherwise? (example code is not in a class, I know)
I have tried different images and converted the original image into a .TIF, .JNP, .PNG, .GIF but none give a result. I have also removed the Alpha channel (apprantly that might have been an issue when using ImageTK.PhotoImage but it did not help). I have also converted the image into grasyscale as a last ditch attempt but no luck.
import tkinter as tk
import PIL.Image
import PIL.ImageTk
root = tk.Tk()
image = Image.open("TemplateRack_GUI.png")
photo = ImageTk.PhotoImage(image)
label = tk.Label(image=photo)
label.image = photo
label.grid(row=5, column=5)
root.mainloop()
You say that your program gives you sometimes:
TclError: cannot use geometry manager grid inside which already has slaves managed by pack.
and sometimes:
TclError: image "pyimage86" doesn't exist.
I can't believe that!
Furthermore you said "I understand that I should not use grid or pack gemoetry managers within the same class"
About which class are you talking?
Tkinter docs says: Never mix grid and pack in the same master window.
Please check your code again because you are using somewhere pack and grid.

How do i resize the pop-up from simpledialog.askstring in tkinter?

While using the simplesialog.askstring is good and all, I would like to resize the pop-up window and also resize the width of the text input.
(sample code)
from tkinter import *
from tkinter import simpledialog
prompts = ["name", "age", "height", "wheight"]
root = Tk()
for p in prompts:
answer = simpledialog.askstring(p, root)
print(answer)
I have looked at different documentation, but could not seem to spot how to do it.
If you just want to make the dialog window wider, then add extra tabs at the end of your prompt string.
For example:
table_name = tk.simpledialog.askstring("Create Table", "Enter a name for the new table.\t\t\t")
Possible Copy of Increase tkSimpleDialog window size?
My understanding is that tkinter.simpledialog is a very simple and easy-to-use dialog box with very few options(basically title and prompt).
No answer here is going to look as 'clean' as your code, unless you modify the tkinter module.(unless you get away from tkinter)
I would recommend either simply using the given simpledialog one, or trying something like the easygui enterbox for a different look, or making a simple GUI with tkinter.

How to put image in another window in tkinter?

I want to put an image in the second window using tkinter, in the first window the code works good, but the second window shows nothing.
In this part I import necessary modules:
from tkinter import filedialog, Tk, Frame, Label, PhotoImage, Button
from PIL import Image
from tkinter import*
import tkinter as tk
Then create the principal window:
raiz = Tk()
raiz.title("ventana")
Then I create the frame and put the image in the frame:
miFrame = Frame()
miFrame.pack()
miFrame.config(width="1400", heigh=("1200"))
fondo=tk.PhotoImage(file="fondoF.png")
fondo=fondo.subsample(1,1)
label=tk.Label(miFrame,image=fondo)
label.place(x=0,y=0,relwidth=1.0,relheight=1.0)
Then a button that will call the second window function:
btn3 = Button(raiz, text="boton")
btn3.place(x=500, y=500)
btn3.config(command=abrirventana2)
Here we have the function which opens the second window and here (I guess) is where I want to put the image.
This part also has two buttons named mih which does nothing in the meantime and ok which calls the function to close the second window:
def abrirventana2():
raiz.deiconify()
ventana2=tk.Toplevel()
ventana2.geometry('500x500')
ventana2.title("ventana2")
ventana2.configure(background="white")
fondov=tk.PhotoImage(file="xxx.gif")
label1=tk.Label(ventana2,image=fondov)
label1.place(x=50,y=50,relwidth=5.0,relheight=5.0)
mensaje=tk.Label(ventana2,text="funciona")
mensaje.pack(padx=5,pady=5,ipadx=5,ipady=5,fill=tk.X)
boton1=tk.Button(ventana2,text='mih')
boton1.pack(side=tk.TOP)
boton2=tk.Button(ventana2,text='ok',command=ventana2.destroy)
boton2.pack(side=tk.TOP)
Function to close the second window:
def cerrarventana2():
ventana.destroy()
I use the mainloop to keep the window open
raiz.mainloop()
Note: I had already tried creating a frame in the second window, but it didn't work.
Apologies for my previously incorrect answer.
The reason the image is not showing is due to the fact that you did not create a reference to it. If you don't create a reference, the image is garbage collected, which doesn't remove it, but in a sense just renders a blank placeholder on the GUI.
In order to display the image correctly you need to add a reference to the image within the code that displays the image.
You therefore now have:
fondov=tk.PhotoImage(file="giphy.gif")
label1=tk.Label(ventana2,image=fondov)
label1.image = fondov
label1.pack()
(label1.image = fondov is the reference)
Sorry for the confusion there. This should work.

How to Change the Title Font Size and Logo Size For the Tkinter window

Have couple of queries.
a. In order to set any title we use the following below command
master.title("Hello Welcome To Python World")
But then how can I change the Title Text Font Size??
b. Was able to add a logo to the title window with the below command
img_tmp="pylogo.jpg"
img=ImageTk.PhotoImage(Image.open(img_tmp))
root.call('wm','iconphoto',root,img)
But then how can I change the size of the logo image that gets loaded onto the frame ??
Kindly drop in your comments/suggestions/temporary code snipet for the same.
Thanks in Advance !
Regards,
Vimo
The following code will only change the Font.
import tkinter as tk
root = tk.Tk()
root.option_add('*Font', '19')
root.geometry("200x150")
label = tk.Label(root, text = "Hello World")
label.pack(padx = 5, pady = 5)
root.mainloop()
Unfortunately Tkinter is really limited, you'd have to use some other kind of window manager import or some sort; I did try this a while back, however with tkinter alone - it is impossible.
Theoretically you can, however, archieve this by making a custom window.
Or creating a custom title bar.

Categories