Dec-15-2017, 07:54 PM
(This post was last modified: Dec-15-2017, 08:58 PM by sparkz_alot.)
So the task is to create a drop down menu with 3 options and whenever an option is chosen an image changes in order to what that option's image is. My problem is that whenever i choose an option, the image shows but for a limited amount of time, then it disappears. I need that image to stay until i choose another option from the drop down menu.
Here is my code:
from tkinter import *
import time
from PIL import Image
from PIL import ImageTk
date = time.strftime("%d/%m/%Y")
window = Tk()
window.wm_title("Weather for " + date )
window.config(background = "#C0C0C0")
window.geometry("500x300+300+300")
CountryList = ("Malta", "Italy", "Sweden")
menu = StringVar()
global tkimage
label = None
def get_selection(value):
if(value == "Malta"):
SunnyImage = Image.open("/home/pi/Desktop/Assignment/SunnyImage.jpg")
tkimage = ImageTk.PhotoImage(SunnyImage)
label.config(image = tkimage)
window.update()
time.sleep(3)
if(value == "Italy"):
CloudImage = Image.open("/home/pi/Desktop/Assignment/CloudImage.jpg")
tkimage = ImageTk.PhotoImage(CloudImage)
label.config(image = tkimage)
window.update()
time.sleep(3)
if(value == "Sweden"):
RainImage = Image.open("/home/pi/Desktop/Assignment/RainImage.png")
tkimage = ImageTk.PhotoImage(RainImage)
label.config(image = tkimage)
window.update()
time.sleep(3)
weatherImage = Image.open("/home/pi/Desktop/Assignment/NoImage.png")
tkimage = ImageTk.PhotoImage(weatherImage)
label = Label(window, image=tkimage)
label.pack(side=RIGHT, padx=15, pady=20)
labelWeather = Label(window, text = "Enter Country")
labelWeather.pack(side=LEFT, padx=5 , pady=20)
userWeather = OptionMenu(window, menu, *CountryList, command = get_selection)
userWeather.pack(side=LEFT, padx=5 , pady=20)
window.mainloop()
Here is my code:
from tkinter import *
import time
from PIL import Image
from PIL import ImageTk
date = time.strftime("%d/%m/%Y")
window = Tk()
window.wm_title("Weather for " + date )
window.config(background = "#C0C0C0")
window.geometry("500x300+300+300")
CountryList = ("Malta", "Italy", "Sweden")
menu = StringVar()
global tkimage
label = None
def get_selection(value):
if(value == "Malta"):
SunnyImage = Image.open("/home/pi/Desktop/Assignment/SunnyImage.jpg")
tkimage = ImageTk.PhotoImage(SunnyImage)
label.config(image = tkimage)
window.update()
time.sleep(3)
if(value == "Italy"):
CloudImage = Image.open("/home/pi/Desktop/Assignment/CloudImage.jpg")
tkimage = ImageTk.PhotoImage(CloudImage)
label.config(image = tkimage)
window.update()
time.sleep(3)
if(value == "Sweden"):
RainImage = Image.open("/home/pi/Desktop/Assignment/RainImage.png")
tkimage = ImageTk.PhotoImage(RainImage)
label.config(image = tkimage)
window.update()
time.sleep(3)
weatherImage = Image.open("/home/pi/Desktop/Assignment/NoImage.png")
tkimage = ImageTk.PhotoImage(weatherImage)
label = Label(window, image=tkimage)
label.pack(side=RIGHT, padx=15, pady=20)
labelWeather = Label(window, text = "Enter Country")
labelWeather.pack(side=LEFT, padx=5 , pady=20)
userWeather = OptionMenu(window, menu, *CountryList, command = get_selection)
userWeather.pack(side=LEFT, padx=5 , pady=20)
window.mainloop()