This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 11 months ago.
I want to set my Canvas background as gif. Here's what I'm currently trying to do:
def convertPage():
for i in mw.winfo_children():
i.destroy()
drop_file = Canvas(mw, width=270, height=270)
drop_file.place(x=25, y=25)
image = ImageTk.PhotoImage(file="media/ukral.gif")
drop_file.create_image(10, 10, image=image, anchor=NW)
Button(mw, text='KEYWORD IMAGE', command=fileDialog, font=('Inter', 24), fg='#3EC96D', bg='black').place(width=250, height=60, relx=0.1875, rely=0.628)
Button(mw, text='BACK', command=mainPage, font=fnt_sml, fg='#3EC96D', bg='black').place(width=250, height=60, relx=0.1875, rely=0.806)
There are no errors returned in the console, but gif doesn't work, the background of Canvas is white. BTW, I'm doing it from Mac, so maybe this could be the issue.
Update: finally found the solution here:
Play an Animated GIF in python with tkinter
Have you tried to look online?
I found this link, it might be helpful
In short, the answer could be adding the right format kwarg
Related
This question already has answers here:
How to change the foreground or background colour of a Tkinter Button on Mac OS X?
(10 answers)
Closed 5 months ago.
I know this is a common SO question, but I must be overlooking something very simple:
from tkinter import Tk,Button
root = Tk()
root.geometry('100x50')
button = Button(root, bg='blue',text = 'Submit' )
button.pack()
root.mainloop()
The button background simply will not change from default gray. On Mac with Monterey 12.5.1
According to the documentation, color words work if they are also found in the rgb.txt file.
Out of curiosity, do other common colors work? Like red, blue, green? Or nothing works?
This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 7 months ago.
I am new to Python and trying to play with Tkinter and Image placement in frames.
I am wanting to create a function that will allow me to place an image into any frame. Below is the function I have created.
The code in the function works by itself (outside of a function) but does not produce the same result in the function. Outside the function it gives me the image in the frame, however in the function (unless I show the image first - commented out code) it produces a black colored frame.
I have seen that there are numerous threads similar to this problem, but none make sense to me.
# Create a function to place an image into a set frame
def PlaceImageInFrame(ImageURL, Frame, NewWidth, NewHeight, backgroundColor):
# Import the Login Logo
Login_Image = Image.open(ImageURL)
# Shrink Image
Login_Image.thumbnail((NewWidth,NewHeight))
# Show Image
#Login_Image.show()
# Bring Image into Python Tkinter
Login_Logo = ImageTk.PhotoImage(Login_Image) # PIL solution
# Create New Label in a Frame
Logo_Image = Label(Frame, image = Login_Logo, bg=backgroundColor)
# Place Label in the Frame
Logo_Image.place(x=0, y=0)
PlaceImageInFrame("Logo.jpg",Login_Logo_Frame, 400, 400, "black")
Edit 1: For example, other stack overflow posts i have seen include:
Why does Tkinter image not show up if created in a function?
[Solution] Edit 2: Found an interesting post on Github that shows the fix is as simple as reassigning the image to the label - a little strange.
https://github.com/ythy/blog/issues/302
"
photo = PhotoImage(...)
label = Label(image=photo)
label.image = photo # keep a reference!
label.pack()
"
you could not call address image as direct
first by PhotoImage class fetch this image ,then put in parameter
image=PhotoImage(file="Logo.jpg")
PlaceImageInFrame(iamge,Login_Logo_Frame, 400, 400, "black")
This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed last year.
I'm trying to load an image into tkinter, to use as a background image. The image is a GIF (originally JPG, but I heard that tkinter doesn't support that format) with the same dimensions as the window. Anyway, when I ran my code, it ran, but the tkinter window was empty! Here's my code for the window:
class Window(Tk):
def __init__(self):
super().__init__()
self.geometry("700x600")
self.resizable(False, False)
def background_img(self, img_path):
background_img = PhotoImage(file=img_path)
background_img_label = Label(self, image=background_img)
background_img_label.place(x=0, y=0)
window = Window()
img_path = "background.gif"
window.background_img(img_path)
window.mainloop()
Can you please tell me what I'm doing wrong? Thanks in advance.
As TDG said, the names background_img (function) and background_img (tkinter.PhotoImage) override each other. You should rename one.
This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 3 years ago.
I want my half circle to be bolded when I click. Currently, it keeps drawing as already bold.
import tkinter as tk
root = tk.Tk()
canvas = tk.Canvas(root, width=300, height=200, bg='black')
canvas.pack(fill="both", expand=True)
# when you click on the half circle, it becomes bold
half_circle = canvas.create_arc(100, 0, 200, 100, start=0, extent=-180, outline="white", style="arc")
def bold():
canvas.itemconfigure(half_circle,width=2.5)
canvas.tag_bind(half_circle,"<Button-1>", bold())
root.mainloop()
Update: I changed bold() to bold(event), and am also passing bold. Still not working. I think it could be a problem with PyCharm. Even when I ask it to just print("random") after a click, when the window opens "random" prints immediately and I can't seem to interact with it after.
Second update: I wasn't clicking the exact outline, and now understand the difference between calls and callbacks. lol
The argument should be a callback, not a call:
canvas.tag_bind(half_circle, "<Button-1>", bold)
This question already has answers here:
Image resize under PhotoImage
(5 answers)
Closed 6 years ago.
I have an image that I display and I would like to resize (enlarge, actually) that image. Here's my code, using other SO questions, but I get no result - my image still has the same size. Resizing the button also does not change the image size.
I've tried the answers from this SO question: Image resize under PhotoImage but they won't work.
from Tkinter import *
root = Tk()
root.withdraw()
def cleanUp():
root.destroy()
def openWebsite():
print 'Will try to implement opening the website here.'
window = Toplevel(root)
window.protocol('WM_DELETE_WINDOW', cleanUp)
photo = PhotoImage(file="Header.pgm")
photo.zoom(2)
button = Button(window, image=photo, command=openWebsite)
button.pack()
root.mainloop()
PhotoImage.zoom() returns a new image, it does not modify the original image. Try rebinding photo like this:
photo = photo.zoom(2)
From the help:
zoom(self, x, y='') method of Tkinter.PhotoImage instance
Return a new PhotoImage with the same image as this widget
but zoom it with X and Y.