Use tkinter display animated gif little sister can not afford to shut the program python tkinter achieve learning tutorial package ---

The basic idea: each frame pictures animated gif map stored inside an array, then calls after () method to loop continuously updated picture

Follow the code described previously ( off little sister can not afford to implement learning programs python tkinter --- packed tutorial code), this code can understand, then, the following code will be well understood, and it is one more update () function, which was called after () function constantly updated picture

tkinter Import AS tk # tkinter this statement to import the module, but for convenience, for it defines an alias tk.
import os
import tkinter.messagebox #messagebox ordinary windows programming frequently used, the purpose is to display a message box
After decoding the picture data import base64 # To encode and save it as a picture
import os # we delete the image you want to add this to come
from main_w_gif Import img AS socket_one # The next step is to import the file you just created xxx.py file, because each image file img inside are variable, so the time can be used to import an alias, which is the basic usage of the python, I say no more.
from not_gif Import img AS socket_two
 from sure_gif Import img AS socket_three

tmp = Open ( ' socket_one.gif ' , ' WB ' ) # This is decoded. save Picture
tmp.write(base64.b64decode(socket_one))

tmp = open('socket_two.gif', 'wb')
tmp.write(base64.b64decode(socket_two))

tmp = open('socket_three.gif', 'wb')
tmp.write(base64.b64decode(socket_three))

tmp.close () # can use it now, run out (after the program is loaded into the inside)
"" "
 Python our code calls the built-in Tkinter, Tkinter package of Tk interface to access;

Tk is a graphics library that supports multiple operating systems, using the Tcl language development;

Tk will call the local GUI interface to the operating system, complete the final GUI.

So, we just need to call the code provided by Tkinter interfaces on it

Tkinter of two way, both the wording is different,

First, we need to increase the use of the class name, which is the need to add tkinter,  
The second has been the tkinter full import, and you do not control what is called class, just use it. So you do not need to add the class name.



Law # 1:
import tkinter
win=tkinter.Tk()
win.mainloop ()
Law # 2:
from tkinter import *
win=Tk()



"""
window = tk.Tk()
# Set the main window size
window.geometry('600x500')
# Set the main window title
window.title('caiya')

def closeWindow():

    tkinter.messagebox.showerror (title = ' Come on ' , the Message = ' did not answer it! ' ) # error message box

    return

window.protocol('WM_DELETE_WINDOW', closeWindow)

# Set the text
that Lable1 = tk.Label (window, text = " Ash - Ash - ash - non - Fly " ) #, font = ( " Arial " , 14 )
lable2 = tk.Label (window, text = " I ask you not your lab the most beautiful, most adorable, most efforts, the most serious of small junior sister apprentice? " ) #, font = ( " Arial " , 34 )


numIdx = . 6 frames of # gif
frames = [tk.PhotoImage(file='socket_one.gif', format='gif -index %i' %(i)) for i in range(numIdx)]
imgLabel = tk.Label(window)
# Tkinter call layout management module
lable1.pack()
lable2.pack()
imgLabel.pack()


numIdx1 = . 6 frames of # gif
photo1 = [tk.PhotoImage(file='socket_two.gif', format='gif -index %i' %(i)) for i in range(numIdx1)]
numIdx2 = . 6 frames of # gif
photo2 = [tk.PhotoImage(file='socket_three.gif', format='gif -index %i' %(i)) for i in range(numIdx2)]

def Love():

    love = tk.Toplevel(window)
    love.geometry('380x320')
    love.title("caiya")
    Lable = tk.Label (Love, text = " Haha, I guess that is " ) #, font = ( " Arial " , 24- )

    imgLabel1 = tk.Label(love)
    imgLabel1.pack()
    update1(0,imgLabel1,love)

    btn = tk.Button (Love, text = " perfect ending " , width = 30 , height = 2 )
    btn.config(command=lambda :closelove(love))
    lable.pack()

    love.protocol('WM_DELETE_WINDOW', closeall)
    btn.pack()

# Click dislike operation
def NoLove():
    no_love = tk.Toplevel(window)
    no_love.geometry('600x350')
    no_love.title("caiya")
    Lable = tk.Label (no_love, text = " lie to yourself really you? " ) #, font = ( " Arial " , 24- )

    imgLabel2 = tk.Label(no_love)
    imgLabel2.pack()


    btn = tk.Button(no_love, text="BACK",width=30, height=2)
    btn.config(command=lambda :closenolove(no_love))
    lable.pack()
    btn.pack()
    update2(0, imgLabel2,no_love)

# Child window close operation
def closeall():
    window.destroy()



def closelove(love):
    window.destroy()

def closenolove(no_love):
    no_love.destroy()

# Set button
btn1 = tk.Button(window, text="",width=30, height=2)
# Configure button
btn1.config(command=Love)
btn2 = tk.Button(window, text="不是",width=30, height=2)
btn2.config(command=NoLove)
# Tkinter call layout management module
btn1.pack()
btn2.pack()


# Timer function
def update(idx):
    frame = frames[idx]
    IDX + = . 1 # next sequence number
    imgLabel.configure(image=frame)
    window.after ( 100 , Update, IDX% numIdx) # 200 milliseconds after the timer function continues

def update1(idx,imgLabel1,love):
    frame = photo2[idx]
    idx += 1
    imgLabel1.configure(image=frame)
    idx=idx%numIdx1
    love.after(200, update1,idx%numIdx1,imgLabel1,love)

def update2(idx,imgLabel2,no_love):
    frame = photo1[idx]
    idx += 1
    imgLabel2.configure(image=frame)
    idx=idx%numIdx2
    no_love.after(200, update2,idx%numIdx2,imgLabel2,no_love)

update(0)
window.mainloop ()

"" "
 Window.mainloop will make window constantly refresh, if not mainloop, it is a static window, there will be a value passed into circulation, mainloop the equivalent of a big while loop, there is a while, each click will be updated once, so we must have a cycle
  Therefore, the document window must have a similar mainloop 
  mainloop is a critical key to the key file window.

"" "
 
Os.remove ( ' socket_one.gif ' ) # delete pictures
os.remove('socket_two.gif')
os.remove('socket_three.gif')

 

Guess you like

Origin www.cnblogs.com/caiya/p/11917372.html