Reputation: 29
I want the image to appear when the command related to it is execute, but insted am getting this, the image remains there when different command related to image is executed, and it feel kind of untiedy overlaped.
HERES THE CODE.
from tkinter import *
from PIL import ImageTk,Image
w=Tk()
w.geometry('800x400')
nam=Label(w,text="name of car")
nam.place(x=130,y=50)
w.configure(bg='#b000ff')
le2=Label(w,text="NUMBER",bg='#6863f6',fg='black',font='comicsansms 10 bold')le2.place(x=0,y=180)
ent2=Entry(w,textvar=StringVar,bg='white',fg='black')
ent2.place(x=70,y=180)
lo = Image.open('F:\\all net downoades\\audier.png')
vo = ImageTk.PhotoImage(lo)
lo4 = Image.open('F:\\all net downoades\\lambo.png')
vo2 = ImageTk.PhotoImage(lo4)
def one():
cn = Canvas(w, widt=400, height=250) # n, ne, e, se, s, sw, w, nw, or center
cn.pack(side=BOTTOM, anchor=E)
cn.create_image(200,135,image=vo)
#another func
def two():
cn6 = Canvas(w, widt=400, height=250) # n, ne, e, se, s, sw, w, nw, or center
cn6.pack(side=BOTTOM, anchor=E)
cn6.create_image(200,135,image=vo2)
def both():
if int(ent2.get()) == 1:
one()
elif int(ent2.get()) == 2:
two()
else:
pass
bui=Button(w,text="ok",command=both)
bui.place(x=0,y=100)
w.mainloop()
Upvotes: 1
Views: 254
Reputation: 47173
You should not create new canvas whenever one()
or two()
is called. You should create the canvas once and hide it initially, then update the image inside the canvas and show it inside one()
or two()
functions:
def one():
cn.itemconfigure('image', image=vo)
def two():
cn.itemconfigure('image', image=vo2)
...
cn = Canvas(w, width=400, height=250, bg='#b000ff', highlightthickness=0)
cn.pack(side=BOTTOM, anchor=E)
cn.create_image(200, 135, image=None, tag='image')
w.mainloop()
Upvotes: 1
Reputation: 15098
Try something like this and let me know
count = 0
def one():
global cn,count
count+=1
if count >1:
cn6.pack_forget()
cn = Canvas(w, widt=400, height=250) # n, ne, e, se, s, sw, w, nw, or center
cn.pack(side=BOTTOM, anchor=E)
cn.create_image(200,135,image=vo)
#another func
def two():
global cn6,count
count+=1
if count > 1:
cn.pack_forget()
cn6 = Canvas(w, widt=400, height=250) # n, ne, e, se, s, sw, w, nw, or center
cn6.pack(side=BOTTOM, anchor=E)
cn6.create_image(200,135,image=vo2)
Upvotes: 1