Thursday, September 23, 2021

Difference between for and while loop, using microwave tkinter program


 Dimensions:                     442 x 281                                    59 x 16            62 x 30          44 x 42 


from tkinter import *

from tkinter import simpledialog

from playsound import playsound

from time import sleep

from random import randint


win = Tk()

win.geometry("450x300+800+50")


picMW = PhotoImage(file="Microwave.png")

lblMW = Label(win, image=picMW)

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


S = StringVar()                           # Change in font size here     

lblCtr = Label(win, textvariable=S, font=("Helvetica", 22, "bold"),

               width=3, anchor="c", bg="black", fg="yellow")

lblCtr.place(x=370, y=45)  # Change here


def setTime(e):

  S.set("")

  secs = simpledialog.askinteger("Input", "How many seconds?", parent=win)

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

    S.set(str(i))

    if i < 10:

      S.set("  " + str(i))

    win.update_idletasks()

    sleep(1)


picBtn1 = PhotoImage(file="TimeButton.png")

lblBtn1 = Label(win, image=picBtn1, bd=0)

lblBtn1.place(x=380, y=173)

lblBtn1.bind("<Button-1>", setTime)


state = False

def setTemp(e):

  global state

  state = True

  S.set("")


  currentTemp = randint(30, 40)

  while state == True:

    inc = randint(1, 2)

    currentTemp += inc

    sleep(.1)

    S.set(str(currentTemp))

    win.update_idletasks()


picBtn2 = PhotoImage(file="TempButton.png")

lblBtn2 = Label(win, image=picBtn2, bd=0)

lblBtn2.place(x=374, y=85)

lblBtn2.bind("<Button-1>", setTemp)


def stopTemp(e):

  global state

  state = False


picBtn3 = PhotoImage(file="StopButton.png")

lblBtn3 = Label(win, image=picBtn3, bd=0)

lblBtn3.place(x=374, y=135)

lblBtn3.bind("<Button-1>", stopTemp)


win.mainloop()


No comments:

Post a Comment