This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 5 years ago.
I was trying to code a small window in a program that is giving you feedback about your statistics in the game. The output is working fine, but the image i tried to edit is not working. I tried to define the image global, i tried to take the direct path, i tried to change the suffix of the picture i want to edit (right now it's a .gif -file) but nothing is working. Whats wrong? What is my mistake?
import tkinter
def blackhole():
Black=tkinter.Tk()
Black.title("BlackHole")
schrift=tkinter.Label(Black,text="BlackHole: Game got reseted!")
schrift.pack()
Medal=tkinter.Label(Black,text="Congretulation! You earn the Bronze Medal!")
Medal.pack()
knopf=tkinter.Button(Black,text="Ok",command=submit)
knopf.pack()
canvas = tkinter.Canvas(Black, width=250, height=250)
canvas.pack()
tk_img = tkinter.PhotoImage(file = '/Users/Hannes/Desktop/Klickbot/b.gif')
canvas.create_image(150, 150, image=tk_img)
To include images with Tkinter you should use Pillow (PIL).
https://python-pillow.org/
Run pip install Pillow from the terminal to install.
Sample usage:
from PIL import ImageTk, Image
img = ImageTk.PhotoImage(Image.open("logo.png"))
panel = Label(root, image=self.img)
panel.pack(side="top", fill="both", expand="yes")
Related
This question already has answers here:
Tkinter understanding mainloop
(3 answers)
How do you run your own code alongside Tkinter's event loop?
(5 answers)
Closed 12 days ago.
I'm working on a script that requires the program to continue running while a message box is on screen. I want it to still do my program, but have a message box that appears while the script runs
I've been using Tkinter for the message boxes, and I can make multiple appear on screen. However, if I try to run code while the message boxes are on screen, it doesn't work.
Here's my code:
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.withdraw()
for i in range(0,5):
f = tk.Toplevel(root)
f.resizable(False, False)
f.geometry("150x75+100+100")
f.title("MessageBox")
ttk.Label(f, text="This is a Test").pack()
print("test")
root.mainloop()
while True:
print("doing code while there are msgboxes on screen")
What I want it to do is draw the message boxes on my screen and continue the code as if I hadn't.
Any help would be much appreciated, thanks!
This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 3 months ago.
I created a button with an image in Tkinter, but the image isn't showing, i can just see a blank space in the button. Maybe my OS is the problem. I have Linux Mint 21 Xfce Edition.
from tkinter import ttk
from tkinter import filedialog
from tkinter import messagebox
from tkinter import dialog
from tkinter import *
from PIL import ImageTk, Image
from modules import fonts, buttons
import shutil
import os
virusscan_win = Tk()
virusscan_win.title("Virus scan")
virusscan_win.geometry('400x200+60+60')
virusscan_win.configure(background="white")
Message(text="The virus scanner gives them the ability to scan files, folders or their system for viruses and remove threats automatically.", width=250, font=fonts.sansserif_small, background = "white").place(x=10, y=10)
safe_image=PhotoImage(file='./media/button.png')
Button(master=virusscan_win, image=safe_image, text="Scan file").place(x=10, y=90)
Can anyone explain, what was wrong with my code? I already checked, is the file in the correct folder. the debugger also shows nothing.
Python is notorious for aggressively garbage collecting image references, which leads to them being destroyed before the UI gets drawn. Something like this should work for getting an image onto a Button
from PIL import Image, ImageTk
from pathlib import Path
img_path = Path('C:/<path>/<to>/media/button.png') # use the full path to your img file
with Image.open(img_path) as img: # open the image file
img_tk = ImageTk.PhotoImage(img) # get the image as a Tk object
button = Button(virusscan_win, image=img_tk) # add image to button
Note that if your image is the wrong size for your button, you can resize it like so
img_tk = ImageTk.PhotoImage(img.resize(32, 32)) # or whatever size you need
This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Tkinter PIL image not displaying inside of a function
(1 answer)
Closed 2 years ago.
So I'm experimenting With Tkinter message boxes And I'm trying to insert an image in my toplevel window>
from tkinter import*
from tkinter import messagebox
root = Tk()
root.title('Hi')
root.iconbitmap('C:/Users/davids/Downloads/i2.ico')
e = Entry(root,borderwidth=8,width=35)
top = Toplevel()
def popup():
mg = messagebox.askyesno("Warning", "Click yes/No.")
if mg == True:
top = Toplevel()
top.geometry('1200x1200')
b= Label(top, text="hi")
b.pack()
image = PhotoImage(file="path")
Label(root, image=image).pack()
Button(root,text="Ask question", command=popup).pack()
root.mainloop()
However when I run this code nothing shows. There isn't an error so I can't see what I'm doing wrong. Only the text is displayed. I've tried adding root. but that still doesn't fix it. Please note that I do know that it is easier to use PIL however my new operating system won't install it for some reason so I'm trying to find a way without Pillow. Any idea what is happening?
This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 2 years ago.
I'm having a weird issue with the tkinter script below. If the outcommented lines are executed, it is showing the image like intended. When I call the function instead, it doesn't. The main window i opened and nothing else happens. 'Function called' is being printed in the Shell though. Am I missing something basic about tkinter and/or functions here? I found some reports about weird behavior with IDLE which I'm using as well, could that be the reason?
Yesterday I had some issues because I called the file tkinter.py, same as the module but in a different folder. Like a few more persons, that resulted in problems opening the scripts with IDLE. I wrote a new file with the code below and the problem persisted.
from tkinter import *
import tkinter as tk
from PIL import Image, ImageTk
from pathlib import Path
window = Tk()
window.title('xy')
window.geometry('600x400')
path = Path("C:/Python/png/")
def function_a():
print('function called')
img = Image.open(str(path)+'\\'+'pic'+'.png')
img = img.resize((300,200), Image.ANTIALIAS)
img = ImageTk.PhotoImage(img)
background_label = tk.Label(window, image=img)
background_label.pack()
function_a()
##img = Image.open(str(path)+'\\'+'pic'+'.png')
##img = img.resize((300,200), Image.ANTIALIAS)
##img = ImageTk.PhotoImage(img)
##background_label = tk.Label(window, image=img)
##background_label.pack()
window.mainloop
The photo img gets removed by garbage collection because it is a local variable.
To fix this add background_label.img = img at the end of the function, this stops the image from being removed by garbage collection.
This question already has answers here:
Removing the TK icon on a Tkinter window
(7 answers)
Closed 4 years ago.
Eyery single tkinter import creates a tkinter window with a winged sign
on the top. Here's a screenshot:
Any ideas how to remove it? Thanks in advance!
I need the answer for Windows, not for UNIX
As indicated in https://stackoverflow.com/a/18277350/4777984 this is probably the best solution.
import tkinter
import tempfile, base64, zlib
ICON = zlib.decompress(base64.b64decode('eJxjYGAEQgEBBiDJwZDBy'
'sAgxsDAoAHEQCEGBQaIOAg4sDIgACMUj4JRMApGwQgF/ykEAFXxQRc='))
_, ICON_PATH = tempfile.mkstemp()
with open(ICON_PATH, 'wb') as icon_file:
icon_file.write(ICON)
tk = tkinter.Tk()
tk.iconbitmap(default=ICON_PATH)
label = tkinter.Label(tk, text="Window with transparent icon.")
label.pack()
tk.mainloop()
Take a look at this question
Set window icon.
Basically you call root.iconbitmap(path_to_your_icon).