This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 6 months ago.
I am trying to make a sprite sheet editor.
I have a button where you can select an image.
When I put the code in a function it doesn't work, but when it is outside a function it does work.
The BrowseComputer function is what I am having a problem with
from tkinter import *
from tkinter import filedialog
#Window specs
window = Tk()
window.title("TileSplit")
window.configure(background="white")
filepath = ""
#Searches computer and displays Image
def BrowseCompter():
filepath = filedialog.askopenfilename()
photo = PhotoImage(file = filepath)
print(filepath)
SelectedImage = Label(window, image=photo, bg="White").grid(row=0,column=0)
#Makes a variable with information about the instruction text on the screen, Sets the text background, Text color, font and location
InstructionsLabel = Label(window, text="Please select the image you would like to split:" ,bg="white" ,font= "none 12 bold")
EmptyLabel = Label(window, text="" ,bg="white" ,font= "none 12 bold")
#Makes a button
ImportImageButton = Button(window, text="Browse:", command=BrowseCompter)
#Location data
InstructionsLabel.grid(row=0, column=60, sticky=E)
ImportImageButton.grid(row=3,column=0)
EmptyLabel.grid(row=1, column=0)
#Tells python to make the window
window.mainloop()
I think you have a typo. The function shown is actually named 'BrowseCompter'.
also you might need to change your command in the button from 'BrowseCompter' to 'BrowseCompter()'
Related
This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 9 months ago.
I am trying to display an image in a window with tkinter, by clicking a button.
I created this function :
def tabuim():
tabu = Frame(win, width=600, height=400)
tabu.pack()
tabu.place(anchor='center', relx=0.8, rely=0.35)
img2 = ImageTk.PhotoImage(Image.open("Tabu.jpg"), master=win)
label2 = Label(tabu, image = img2, borderwidth=3, relief="solid")
label2.pack()
with this button :
button2 = Button(win, text = 'Tabu Route Solution', command = tabuim)
button2.place(relx = 0.5, rely = 0.5)
But it partially works. It only show a border without my image...
Thanks for you help
The following example displays a background image in a frame:
Import tkinter
from tkinter import *
from PIL import Image, ImageTk
root = Tk()
Create a photoimage object of the image in the path
image1 = Image.open("<path/image_name>")
test = ImageTk.PhotoImage(image1)
label1 = tkinter.Label(image=test)
label1.image = test
Position image
label1.place(x=<x_coordinate>, y=<y_coordinate>)
root.mainloop()
This an example you can do as instead of resizing before loading the page you could resize after the image is loaded.
This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 10 months ago.
I was trying to show the image when I clicked the button however it failed( the program does not report an error but the image is still not displayed). I'm sure I put the right path to the picture. This is code
import tkinter as tk
from PIL import ImageTk, Image
window = tk.Tk()
def def_btn1():
image1 = Image.open("csdl.png")
image1 = image1.resize((710, 400), Image.ANTIALIAS)
test = ImageTk.PhotoImage(image1)
lbl2 = tk.Label(image=test)
lbl2.pack()
btn1 = tk.Button(text="click to show", relief=tk.RIDGE, width=15, font=(12),command=def_btn1)
btn1.pack()
window = tk.mainloop()
I want when I click the button the image will show in the program. Thank you!
You need to add lbl2.image = test for the image to not be destroyed by tkinter, you new code should be this -
import tkinter as tk
from PIL import ImageTk, Image
window = tk.Tk() # You might want to move this line below your functions, with the other global variables
def def_btn1():
image1 = Image.open("csdl.png")
image1 = image1.resize((710, 400), Image.ANTIALIAS)
test = ImageTk.PhotoImage(image1)
lbl2 = tk.Label(image=test)
lbl2.image = test # You need to have this line for it to work, otherwise it is getting rid of the image. the varibale after the '=' must be the same as the one calling "ImageTk.PhotoImage"
lbl2.pack()
btn1 = tk.Button(text="click to show", relief=tk.RIDGE, width=15, font=(12),command= lambda : def_btn1()) # adding 'lambda :' stops the function from running straight away, it will only run if the button is pressed
btn1.pack()
window = tk.mainloop()
This question already has answers here:
How to reconfigure tkinter canvas items?
(2 answers)
Closed 1 year ago.
While learning Tkinter, I got a problem while creating a function for a button.
I need to config a canvas text in function. But I don't know how to do it.
canvas.create_text(700,350,font=("Arial Bold", 35),fill='white')
def clicked():
res = "THANKYOU " + txt.get()
res1.config(text=res)
I want that res1 to be that canvas.create_text()
You can make use of canvas.itemconfig/canvas.itemconfigure to change the properties of items on canvas.
minimal example:
import tkinter as tk
root = tk.Tk()
canvas = tk.Canvas(root, bg="black")
canvas.pack()
text = canvas.create_text((100,50),text="Hello", fill='white')
def clicked():
res = "THANKYOU "
canvas.itemconfig(text, text=res)
tk.Button(root, text="click", command=clicked).pack()
root.mainloop()
This question already has answers here:
How do I get an event callback when a Tkinter Entry widget is modified?
(9 answers)
Closed 2 years ago.
I'm making a program that gets user input and then exports it into an excel sheet where it performs all the calculations. I've made a button that exports all the data to excel, then another button that then checks a cell value, and if the value is within a range, the background color changes so I don't have to go open my excel file.
What I'm having trouble is, auto updating values displayed on the GUI. My real code is way too long to past here so a very dumbed down version is pasted below. I've also attached an image that shows what I'm wanting my end result to be. I've seen videos about making calculators, but that's simple because the button has a command function in it. I really don't know how to get my end result and any suggestions are welcomed.
[![Picture of what I'm trying to do][1]][1]
from tkinter import *
#main Window using Tk
win = Tk()
win.title("Building")
win.geometry('800x480')
#Labels
L0= Label(win, text = "Bay 1")
L1= Label(win, text = "Bay 2")
L2= Label(win, text = "Bay 3")
L3= Label(win, text = "Bay 4")
L4= Label(win, text = "Bay 5")
L5= Label(win, text = "Ft")
L6= Label(win, text = "Ft")
L7= Label(win, text = "Ft")
L8= Label(win, text = "Ft")
L9= Label(win, text = "Ft")
L10= Label(win,text ="Building Length:")
L11= Label(win,text = "Length:")
#Place Labels
L0.place(y=50)
L1.place(y=80)
L2.place(y=110)
L3.place(y=140)
L4.place(y=170)
L5.place(x=100,y=50)
L6.place(x=100,y=80)
L7.place(x=100,y=110)
L8.place(x=100,y=140)
L9.place(x=100,y=170)
L10.place(y=30)
L11.place(y=250)
#Entries
E0= Entry(win,width=5)
E1= Entry(win,width=5)
E2= Entry(win,width=5)
E3= Entry(win,width=5)
E4= Entry(win,width=5)
E5= Entry(win,width=10)
E6= Entry(win,width=5)
#Place Entries
E0.place(x=50,y=50)
E1.place(x=50,y=80)
E2.place(x=50,y=110)
E3.place(x=50,y=140)
E4.place(x=50,y=170)
E5.place(x=50, y=250)
E6.place(x=100, y=30)
win.mainloop()
```enter image description here
[1]: https://i.stack.imgur.com/2umLS.png
Is this what you want?
def updated:
##The code to run whenever the entry field is modified
E0= Entry(win, width=5, command = updated) ##The entry field you would like to detect updates in
Let me know if this is what you need
This question already has answers here:
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 4 years ago.
How come I can't add images using this:
from tkinter import *
root = Tk()
def logo():
photo = PhotoImage(file="Logo.png")
Label(root, image=photo).grid()
logo()
root.mainloop()
But I can add images using this:
from tkinter import *
root = Tk()
photo = PhotoImage(file="Logo.png")
Label(window, image=photo).grid()
logo()
root.mainloop()
Any help?
You have to keep a reference to the image to prevent it being garbage collected. Try this:
def logo():
photo = PhotoImage(file="Logo.png")
lbl = Label(root, image=photo)
lbl.image = photo # keep a reference
lbl.grid()
You don't have to do that in your other block because you are using global variables, which are never garbage collected.
See the note on the bottom of this page.