Wednesday, April 7, 2021

Sliding frame in tkinter


(Right-click the images, choose "Save image as ..." and save them to the same folder as the script. Save images by the default names.)


from tkinter import *

from time import sleep


win = Tk()

win.geometry("300x200")

win.title("Sliding Frame")


frm = Frame(win, width=80, height=150)

frm.place(x=-100, y=38)


frmShowing = False

def slide():

  global frmShowing

  if frmShowing == False:

    for i in range(-80, -1, 1):

      frm.place(x=i, y=38)

      sleep(.01)

      win.update()

    frmShowing = True

  else:

    pic = PhotoImage(file="Blank.png")

    lblImage.config(image=pic)

    lblImage.image = pic

    for i in range(0, -81, -1):

      frm.place(x=i, y=38)

      sleep(.01)

      win.update()

    frmShowing = False

    

pic = PhotoImage(file="Menu.png")

btnMenu = Button(win, image=pic, command=slide)

btnMenu.place(x=0, y=0)


lblImage = Label(win);  lblImage.place(x=150, y=40)


def Show(mood):

  pic = PhotoImage(file=mood + ".png")

  lblImage.config(image=pic)

  lblImage.image = pic


btnHappy = Button(frm, text="Happy", font="Arial 12 bold",\

                  activeforeground="red", bd=0, command=lambda:Show("Happy"))

btnHappy.place(x=5, y=20)


btnGoofy = Button(frm, text="Goofy", font="Arial 12 bold",\

                  activeforeground="red", bd=0, command=lambda:Show("Goofy"))

btnGoofy.place(x=5, y=50)


btnCreepy = Button(frm, text="Creepy", font="Arial 12 bold",\

                   activeforeground="red", bd=0, command=lambda:Show("Creepy"))

btnCreepy.place(x=5, y=80)


win.mainloop()


No comments:

Post a Comment