I have just started utilizing Python's tkinter module to create some basic GUIs. In the GUI shown below, the user is prompted to upload the cafe image and subsequent cafe name will appear. However, I have not found a convenient way to clear the cafe label text for when the user upload another cafe image and classify again. I have the full code attached below.
import numpy as np
import cv2
import tkinter as tk
from tkinter import filedialog
from tkinter import *
from PIL import ImageTk, Image
from h5py import File
def upload():
global img
global filename
f_types = [('Png files', '*.png')]
filename = filedialog.askopenfilename(filetype=f_types)
img = ImageTk.PhotoImage(file = filename)
e1 = tk.Label(window)
e1.place(x=50, y=150)
e1['image'] = img
window.configure(background = '#CDCDCD')
show_classify_button()
def show_classify_button():
classify_btn = tk.Button(window, text='Classify Image', command=classification)
classify_btn.place(x=250, y=400)
classify_btn.configure(background = '#364156', foreground = 'white', font=('cambria', 15, 'bold') )
def classification():
matrix = []
image = cv2.imread(filename, 0)
array = np.array(image)
flatten_array = array.flatten()
matrix.append(flatten_array)
X = pca.transform(matrix)
pred = clf.predict(X)
if pred == 0:
Label(window, text='Casa De Cafe', font='Cambria 20 bold', background = '#CDCDCD', foreground = '#364156').place(x=170, y=300)
elif pred == 1:
Label(window, text='Coffeology', font='Cambria 20 bold', background = '#CDCDCD', foreground = '#364156').place(x=170, y=300)
else:
Label(window, text='Mori Cafe', font='Cambria 20 bold', background = '#CDCDCD', foreground = '#364156').place(x=170, y=300)
def main_account_screen():
global window
window = Tk()
window.title('Cafe Recognition System')
window.geometry('500x500')
window.configure(background = '#CDCDCD')
Label(window, text='Cafe Recognition System', font='Cambria 20 bold', background = '#CDCDCD', foreground = '#364156').place(x=110, y=50)
button = tk.Button(window, text='Upload', command=upload)
button.place(x=130, y=400)
button.configure(background = '#364156', foreground = 'white', font=('cambria', 15, 'bold') )
window.mainloop()
main_account_screen()
Related
This question already has answers here:
Tkinter vanishing PhotoImage issue [duplicate]
(1 answer)
Why does Tkinter image not show up if created in a function?
(5 answers)
Closed 29 days ago.
hello i try to build a simpel watermark app
i user custom tkinter for user interface ( its same to tkinter )
when i upload a picture with button my function everything work expact picture in canvas i see the canvas but i dont see a picture i test the code with a text on canvas and its show when i add a label with same photo ( i comment that line in this cod) even i dont grid it on window my picture show in canvas this is my cod :
import customtkinter
from PIL import Image, ImageTk
my_font1 = ('times', 18, 'bold')
customtkinter.set_appearance_mode("dark")
customtkinter.set_default_color_theme("dark-blue")
window = customtkinter.CTk()
window.title("Water mark Photo")
window.config(padx=50, pady=50)
window.geometry("600x600")
input_pic_label = customtkinter.CTkLabel(master=window,
text="Choose Your Photo",
font=my_font1)
input_pic_label.grid(row=0, column=0, padx=(180, 0), pady=(200, 0))
choose_pic_button = customtkinter.CTkButton(master=window,
text="Upload Photo",
command=lambda: upload_file())
choose_pic_button.grid(row=1, column=0, padx=(180, 0))
def upload_file():
choose_pic_button.destroy()
input_pic_label.destroy()
f_types = [('Jpg Files', '*.jpg'), ('Png Files', '*.png')]
filename = customtkinter.filedialog.askopenfilename(filetypes=f_types)
image = Image.open(filename)
original_image_size = image.size
print(original_image_size)
resized_image = image.resize((500, 500))
resized_image.save("temp.jpg")
new_image = ImageTk.PhotoImage(resized_image)
canvas = customtkinter.CTkCanvas(master=window, width=500, height=500, highlightthickness=0, bg="black")
canvas.grid(row=2, column=1, columnspan=2)
canvas.create_image(250, 250, image=new_image)
window.update()
# label = customtkinter.CTkLabel(master=window, text="", image=new_image)
window.mainloop()
i expect to see a picture but its show nothing i also try to di it in tkinter with the blow cod but output its same expect i get nothing as outpout here without any error:
import tkinter
from PIL import Image, ImageTk
from tkinter import filedialog
my_font1 = ('times', 18, 'bold')
window = tkinter.Tk()
window.title("Water mark Photo")
window.config(padx=50, pady=50)
input_pic_label = tkinter.Label(window,
text="Choose Your Photo",
font=my_font1)
input_pic_label.grid(row=0, column=0, padx=(180, 0))
choose_pic_button = tkinter.Button(window,
text="Upload Photo",
command=lambda: upload_file())
choose_pic_button.grid(row=1, column=0, padx=(180, 0))
canvas = tkinter.Canvas(window, width=500, height=500, highlightthickness=0)
text = canvas.create_text(250, 250, text="Your image")
canvas.grid(row=2, column=1, columnspan=2)
def upload_file():
#choose_pic_button.destroy()
#input_pic_label.destroy()
#canvas.itemconfig(text, text="")
f_types = [('Jpg Files', '*.jpg'), ('Png Files', '*.png')]
filename = tkinter.filedialog.askopenfilename(filetypes=f_types)
image = Image.open(filename)
img = ImageTk.PhotoImage(Image.open(filename))
original_image_size = image.size
print(original_image_size)
resized_image = image.resize((500, 500))
resized_image.save("temp.jpg")
resized_image = ImageTk.PhotoImage(resized_image)
canvas.create_image(250, 250, image=resized_image)
label = tkinter.Label(window, text="helloooooo", image=resized_image)
label.grid(row=2, column=4)
window.mainloop()
Good day everyone, can I ask how can I manage to put this button inside an image background
code:
from tkinter import *
from PIL import ImageTk, Image
root = Tk()
root.title("Japanese Retro Hub")
root.geometry('2200x1000')
#Image
bg = Image.open("RetroHub_BG_Main.png")
resized = bg.resize((2200,1000), Image.ANTIALIAS)
bg = ImageTk.PhotoImage(resized)
bglabel = Label(root, image=bg)
bglabel.pack(pady=20)
#text for my_command and Start exit button
text = Label(root, text= "Hello world")
text.pack(pady=30)
def my_command():
text.config(text="You have clicked Me...")
def start_exit_button():
click_btn = PhotoImage(file='start_button.png')
img_label= Label(image=click_btn)
button = Button(root, image=click_btn,command=my_command,borderwidth=0)
button.pack(padx=50, pady=20)
root.mainloop()
start_exit_button()
root.mainloop()
This is the image example that I am trying to portray
https://drive.google.com/file/d/1VzbhNazEnfOCk0_QZOD_nWR5jsb-a8GB/view?usp=sharing
I'm trying to do an image search engine with Tkinter. Basically a "Pokedex" that searches for images inside a specific folder on my computer associated with a specific name.Example of the image output
import tkinter as tk
from io import BytesIO
import matplotlib.pyplot as plt
import os
import glob
import natsort
from PIL import Image
from PIL import ImageTk
window = tk.Tk()
window.geometry("600x500")
window.title("CoreaninhaDex")
window.config(padx=10, pady=10)
title_label = tk.Label(window, text = 'CoreaninhaDex')
title_label.config(font=('Arial', 32))
title_label.pack(padx=10, pady=10)
target_image = tk.Label(window)
target_image.pack(padx=10, pady=10)
target_information = tk.Label(window)
target_information.config(font=("Arial", 20))
target_information.pack(padx=10, pady=10)
photos_names_list = ["target1","target2","target3","target4","target5"]
#FUNCTION
def showimage():
dir1 = r"C:\Users\path"
path1 = os.path.join(dir1, '*g')
files = glob.glob(path1)
files1 = natsort.natsorted(files, reverse=False)
for x in files1:
img = plt.imread(x)
image = Image.open(img)
imag = ImageTk.PhotoImage(image)
target_image.config(image=imag)
target_image.image = imag
label_id_name = tk.Label(window, text="ID or Name")
label_id_name.config(font=("Arial", 20))
label_id_name.pack(padx=10, pady=10)
text_id_name = tk.Text(window, height = 1)
text_id_name.config(font=("Arial", 20))
text_id_name.pack(padx=10, pady=10)
btn_load = tk.Button(window, text="Load target", command = showimage)
btn_load.config(font=("Arial", 20))
btn_load.pack(padx=10, pady=10)
window.mainloop()
The output generates a display. But I can't associate the image with any name or even upload the images.
I would like help to write a showimage () function that would do this.
This is for a school project, so i kind off want to understand what I am doing.
I want to add a photo, for example the photo with the name 'teletubbies.jpg' as the background. I have no clue how to do this and how this works, been searching for hours now and dying to find an answer :$
This is the bit of code that i have now:
from tkinter import *
from tkinter.messagebox import showinfo
def clicked():
bericht = 'Test for stackflow!',
showinfo(title='popup', message=bericht)
def clicked1():
bericht = 'Test for stackflow'
showinfo(title='popup', message=bericht)
root = Tk()
label = Label(master=root,
text='This is a test for stackflow',
background='black',
foreground='white',
height = 2
)
label.pack()
button2 = Button(master=root, text='Klik hier voor het beheerportaal', command=clicked, fg='red')
button2.pack(side=BOTTOM,pady=10)
button1 = Button(master=root, text='Klik hier voor het klantportaal', command=clicked1)
button1.pack(side=TOP,pady=10)
entry = Entry(master=root)
entry.pack(padx=10, pady = 10)
root.configure(background='black')
root.mainloop()
If you have a .gif or .pgm/ppm file you could use the Tkinter PhotoImage class to load your image and put it as a background to your label:
backgroundImage = PhotoImage(file = <yourFilePath>)
label = Label(master=root,
image = backgroundImage,
text='This is a test for stackflow',
height = 2
)
label.place(x=0, y=0, relwidth=1, relheight=1)
This will put your image as background in your label.
For the other image formats you can use the Python Image Library.
I have an application wherein, I have to pop a Spinbox widget on click of a button. The widget needs to be overlayed on a background which is an image. I have tried with the code below, but the widget does not appear on the click of the button. I believe the image display is taking precedence over the widget display.
import tkinter as tk
import cv2
from PIL import Image,ImageTk
top = tk.Tk()
count = 1
image = cv2.imread("frames/0.jpg")
w = tk.Spinbox(top, from_=0, to=10)
def helloCallBack():
global count,w
if count%2 != 0:
w.pack()
else:
w.forget()
print(count)
count+=1
B = tk.Button(top, text ="Hello", command = helloCallBack)
B.pack()
label = tk.Label(top)
label.pack()
img = Image.fromarray(image)
imgtk = ImageTk.PhotoImage(image=img)
label.imgtk = imgtk
label.configure(image=imgtk)
top.update()
top.mainloop()
I do not know if this is effect you are looking for:
I used place() and place_forget() to achieve that:
import tkinter as tk
import cv2
from PIL import Image,ImageTk
top = tk.Tk()
count = 1
def helloCallBack():
global count,w
if count%2 != 0:
w.place(x=180, y=650)
else:
w.place_forget()
print(count)
count+=1
B = tk.Button(top, text ="Hello", command = helloCallBack)
B.pack()
label = tk.Label(top)
label.pack()
image = cv2.imread("frames/0.jpg")
img = Image.fromarray(image)
imgtk = ImageTk.PhotoImage(image=img)
label.imgtk = imgtk
label.configure(image=imgtk)
w = tk.Spinbox(top, from_=0, to=10)
top.update()
top.mainloop()