Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter Help
#1
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()
Reply
#2
please reformat as sparkz_alot requests, then we can help.
Your code can't be understood without indentation.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020