Removing TK from Window Title Python tkinter - python

I re-used a code I found here to set the transparency to the TK icon, but it leaves the TK there in the invoked window. I created a second window, and I am able to edit that title. But why can't I edit the code I found from an existing post. I looked everywhere but the new window I made, the title can be edited, why not the other window with Tk in it?
from tkinter import *
import tkinter
import tempfile
ICON = (b'\x00\x00\x01\x00\x01\x00\x10\x10\x00\x00\x01\x00\x08\x00h\x05\x00\x00'
b'\x16\x00\x00\x00(\x00\x00\x00\x10\x00\x00\x00 \x00\x00\x00\x01\x00'
b'\x08\x00\x00\x00\x00\x00#\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x01\x00\x00\x00\x01') + b'\x00'*1282 + b'\xff'*64
_, ICON_PATH = tempfile.mkstemp()
with open(ICON_PATH, 'wb') as icon_file:
icon_file.write(ICON)
root = Tk()
root.title("rename")
tk = tkinter.Tk()
tk.iconbitmap(default=ICON_PATH)
label = tkinter.Label(tk, text="Window with transparent icon.")
label.pack()
root.mainloop()

First of all, you should not create multiple Tk() applications in the same program.
The issue occurs because you create the new window (Application) using Tk() , but you are renaming the title only root application. This does not rename the title of tk application. That you create.
If all you want is for the title to be renamed for the window with the label - Window with transparent icon. . You should use tk.title() (instead of root.title()) . Example -
import tkinter
import tempfile
ICON = (b'\x00\x00\x01\x00\x01\x00\x10\x10\x00\x00\x01\x00\x08\x00h\x05\x00\x00'
b'\x16\x00\x00\x00(\x00\x00\x00\x10\x00\x00\x00 \x00\x00\x00\x01\x00'
b'\x08\x00\x00\x00\x00\x00#\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x01\x00\x00\x00\x01') + b'\x00'*1282 + b'\xff'*64
_, ICON_PATH = tempfile.mkstemp()
with open(ICON_PATH, 'wb') as icon_file:
icon_file.write(ICON)
tk = tkinter.Tk()
tk.title("rename")
tk.iconbitmap(default=ICON_PATH)
label = tkinter.Label(tk, text="Window with transparent icon.")
label.pack()
tk.mainloop()
And you do not need multiple tkinter imports, it does not do anything. Importing tkinter (or any module) once caches it in sys.modules , and any time you try to import it again, you get that cached module from sys.modules .
If you want to create more windows in your application you should use Toplevel widget for that . Example -
import tkinter
import tempfile
ICON = (b'\x00\x00\x01\x00\x01\x00\x10\x10\x00\x00\x01\x00\x08\x00h\x05\x00\x00'
b'\x16\x00\x00\x00(\x00\x00\x00\x10\x00\x00\x00 \x00\x00\x00\x01\x00'
b'\x08\x00\x00\x00\x00\x00#\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
b'\x00\x01\x00\x00\x00\x01') + b'\x00'*1282 + b'\xff'*64
_, ICON_PATH = tempfile.mkstemp()
with open(ICON_PATH, 'wb') as icon_file:
icon_file.write(ICON)
tk = tkinter.Tk()
tk.title("rename")
tknewwindow = tkinter.Toplevel(tk)
tknewwindow.title("rename1")
tknewwindow.iconbitmap(default=ICON_PATH)
label = tkinter.Label(tknewwindow, text="Window with transparent icon.")
label.pack()
tk.mainloop()

Related

How do I change the icon of a simple dialog in tkinter, Python

Not sure how to change the icon of a simple dialog window. I try to use .bitmap but doesn't work. Need Help
Just add the default keyword argument to the iconbitmap() for the icon of the root, then all the child windows will inherit the icon.
import tkinter
from tkinter import simpledialog
root = tkinter.Tk()
root.iconbitmap(default="C:\\Users\\username\\random.ico")
dialog = simpledialog.askstring("INFO", "wut ur name?")
root.mainloop()
Result with default:
Results without default:
Are you looking for this
from tkinter import Tk
master = Tk()
photo = PhotoImage(file = "Any image file")
master.iconphoto(False, photo)

how to change an app icon in tkinter? I trayed to look online but it didn't work

I'm traying to make an app GUI using tkinter. how do I change the window icon?
I trayed to do:
import tkinter as tk
root = tk.Tk()
root.iconbitmap('app_icon.ico')
root.mainloop
I think you have to show that path to the ico picture, try this:
import tkinter as tk
root = tk.Tk()
root.iconbitmap('/path/to/ico/app_icon.ico')
root.mainloop()
Below code worked for me, you can check this for your scenario
from tkinter import *
root=Tk()
p1 = PhotoImage(file = "/path/to/ico/app_icon.ico")
root.iconphoto(False, p1)
root.mainloop()

Trouble With Tkinter in Python [duplicate]

I'm trying to make a simple outline for a gui, and I'm getting the warning
"variable" May be undefined or defined from star imports: tkinter for all of my variables.
Here is my code:
from tkinter import *
class myApp :
def __init__(self, gui,) :
self.root = gui
self.bframe = Frame(self.root) # Create a container Frame at bottom
self.bframe.pack(side=BOTTOM)
self.xlabel = Label(self.root, text="Item ID") # Create the Label
self.xlabel.pack(side=LEFT)
self.xentry = Entry(self.root, bd=5) # Create the Entry box
self.xentry.pack(side=LEFT)
self.xentry.bind('<Return>', self.showStockItem)
self.xentry.focus_set() # Set focus in the Entry box
self.xopen = Button(self.root, text="Show", command=self.showStockItem) # Create the open Button
self.xopen.pack(side=LEFT)
self.xquit = Button(self.bframe, text="Quit", command=self.quitit) # Create the quit Button
self.xquit.pack(side=BOTTOM)
return
gui = Tk()
gui.title("Travel")
app = myApp(gui)
gui.mainloop()
from tkinter import *
In this line, you import everything from tkinter. This is not recommended, so linter will warn you. But if you really want to do this, it's OK, just ignore it.
To be better, you should explicitly import what you need. For example:
from tkinter import Tk, Label, Frame, Entry, Button
Consider using:
import tkinter as tk
and then, prefix all your calls like:
root = tk.Tk()
or,
variableName.pack(side = tk.LEFT)
and so on...

How to set a tkinter windows on top when running a slide app (e.g. Keynote)

I want to show some dynamic text in a window on top when playing a slide show in fullscreen mode. I use the OS X.
I had tried the method attributes("-topmost", 1). Not work...
Thanks
Here is my code:
# coding=utf-8
from tkinter import *
from tkinter import ttk
root = Tk()
root.title(u"弾幕")
root.attributes("-alpha", 0.3)
root.attributes("-topmost", 1)
root.mainloop()

Python-Tkinter, for-loop to create pictured button

I'm still quiet new to programming, so maybe my question in pretty easy or even stupid. As said in the title I'm trying to programm a for loop, which creates a pictured button widget for each picuture in a certain folder. This is what I have so far:
import tkinter
from tkinter import ttk
from tkinter import PhotoImage
import os
root = tkinter.Tk()
list_files = os.listdir(".")
for file in list_files:
if file.endswith(".gif"):
drink = PhotoImage(file)
print(drink)
b1 = ttk.Button(image=drink, text="Hello", compound="right").pack()
l1 = ttk.Label(image=drink).pack()
root.mainloop()
Now what I get is two widgets, one label displaying nothing and a button displaying Hello. In the shell it says drink1.gif, which is correct, because that's the only gif file in my standard python folder...
what have I done wrong?
Use PhotoImage(file='path_to_file') to create image from path.
When PhotoImage object is garbage-collected by Python, the label is cleared. You must save reference to drink object somewhere: l1.image = drink:
http://effbot.org/pyfaq/why-do-my-tkinter-images-not-appear.htm
widget.pack() method return nothing.
import tkinter
from tkinter import ttk
from tkinter import PhotoImage
import os
root = tkinter.Tk()
list_files = os.listdir(".")
for path in list_files:
if path.endswith(".gif"):
drink = PhotoImage(file=path)
b1 = ttk.Button(root, image=drink, text="Hello", compound="right")
b1.pack()
l1 = ttk.Label(root, image=drink)
l1.image = drink
l1.pack()
root.mainloop()
PhotoImage(file) creates an image and gives it the name "drink1.gif", which is returned. If you actually want to load the file into the image, you need PhotoImage(file = file).
I believe you are supposed to run self.pack, according to the Zetcode tutorial.

Categories

Resources