I am trying to create an application with Tkinter that allows me to display a different image (from a set defined in a list) each time it is executed.
The program must do the following:
Show a splash screen that must contain an image (introductory does not belong to the list) that is shown for a certain time and then closes.
When the splash is closed, the main application must be opened, which shows another image (this is the one that must change in each execution), it is shown for another determined time and it closes.
This cycle is repeated, so that the image displayed in the main application rotates according to the order defined in the list of images. The problem is that I need to somehow save the image used by the script so that in the next execution it continues with the next one in the list. I'm trying with pickle
but I can't.
This is my code:
from Tkinter import *
import sys
from datetime import *
import time
import random
import os
import threading
import sched
from sched import *
import pickle
hora1=15
hora2=21
hora3=16
fichero = open("Imagen.txt", "w")
obj=-1
pickle.dump(obj, fichero)
fichero.close()
fichero=open("Imagen.txt","r")
obj=pickle.load(fichero)
class SplashScreen(Frame):
def __init__(self, master=None, useFactor=True):
Frame.__init__(self, master)
self.master.geometry("1366x768+0+0")
self.master.config(bg="black")
self.master.overrideredirect(True)
self.lift()
def p():
root = Tk()
sp = SplashScreen(root)
sp.config(bg="black")
fichero.close()
Img1=PhotoImage(file="Libre.gif")
Img2=PhotoImage(file="Soy.gif")
Img3=PhotoImage(file="universe.gif")
Img4=PhotoImage(file="BBN.gif")
Img5=PhotoImage(file="tr.gif")
Img6=PhotoImage(file="final.gif")
Img7=PhotoImage(file="alc.gif")
Img8=PhotoImage(file="cer.gif")
Img9=PhotoImage(file="at.gif")
Img10=PhotoImage(file="bos.gif")
Img11=PhotoImage(file="se.gif")
#Img=PhotoImage(file=".gif")
Imagenes=[Img1,Img2,Img3,Img4,Img5,Img7,Img8,Img9,Img10,Img11]
Imgf=obj
Imagenf=Imagenes[Imgf]
#logo=Label(root,image=Img6).place(x=00,y=00)
lbl=Label(root,image=Imagenf).place(x=00,y=00)
#lbl2=Label(root,image=Img10).place(x=00,y=00)
root.after(20000,root.destroy)
root.mainloop()
def t():
root = Tk()
sp = SplashScreen(root)
sp.config(bg="black")
Img6=PhotoImage(file="final.gif")
logo=Label(root,image=Img6).place(x=00,y=00)
root.after(2000,root.destroy)
root.mainloop()
p=threading.Thread(target=p)
t=threading.Thread(target=t)
if (hora3 == datetime.now().hour) or ((datetime.now().hour == hora2) or(datetime.now().hour == hora1)):
p.start()
t.start()
So I can't get the index to increase between executions.
First of all, to make a Splash you can resort to threads, in some cases where certain heavy tasks are done in the main thread while the splash is being displayed, they are necessary. However, in your case, you can do without them and use the method
Toplevel
to create a child window.You can use the method
withdraw
to hide the window while the Splash is showing and then show the main window again withdeiconify
.You don't need to load all the images, just load the one you show in that run.
Your big problem was how to store the index for later executions. The idea is simple:
First the program checks if the file exists or not. If it does not exist, it assumes the index 0.
If it exists get the index gets it and adds one to it.
All this is solved in a simple way by means of a block
try-except-finally
, although it can be done in other ways.The code looks like this:
I've removed the hours part, but you can add the conditional in the del block just
if __name__ == "__main__"
fine. I don't know if you call this module from another importing it or how you use it exactly, if you have any problem with this, comment it.I have removed the
from Tkinter import *
, this in Python is bad practice and should be avoided. I mention it in all the answers about Tkinter because it is very common since Effbot (reference site for Tkinter in terms of documentation) uses this way despite being a bad practice and being marked like this in the PEPs. In order not to repeat the same thing, you can look at this answer where I explain this.The images are displayed one each time the script is executed and when it reaches the last one it starts again. This is done by the modulus (%) operator in
(pickle.load(f) + 1) % len(self.imagenes)
. This way we always keep the index within the bounds of the list.