Home > Software design >  Root.mainloop() then disregards the code below
Root.mainloop() then disregards the code below

Time:02-25

i am new to coding and i wanted to create a bot that could make computer life easier. I was trying to put a animation in using tkinter, but after i put the root.mainloop() after the tkinter code, it disregards the code below. Sample below.

import tkinter as tk
from tkinter import *
import datetime
class ImageLabel(tk.Label):
"""
A Label that displays images, and plays them if they are gifs
:im: A PIL Image instance or astring filename
"""

def load(self, im):
    if isinstance(im, str):
        im = Image.open(im)
    frames = []

    try:
        for i in count(1):
            frames.append(ImageTk.PhotoImage(im.copy()))
            im.seek(i)
    except EOFError:
        pass
    self.frames = cycle(frames)

    try:
        self.delay = im.info['duration']
    except:
        self.delay = 100

    if len(frames) == 1:
        self.config(image=next(self.frames))
    else:
        self.next_frame()

def unload(self):
    self.config(image=None)
    self.frames = None

def next_frame(self):
    if self.frames:
        self.config(image=next(self.frames))
        self.after(self.delay, self.next_frame)

lbl = ImageLabel(root)
lbl.pack()
lbl.load('pyimage1.gif')
root.mainloop()

def WishMe():
hour = int(datetime.datetime.now().hour)
if hour >= 0 and hour < 12:
    speak("Good Morning !")             

elif hour <= 12 and hour < 18:
    speak("Good Afternoon !")

else:
    speak("Good Evening !")

CodePudding user response:

Disregarding the many issues with undefined names and missing imports, what you are doing is to define a function after the main loop. That in itself is ok.

But you are never actually calling this function.

And even if you did, you could only call it after it was defined (otherwise you would get a NameError exception). So WishMe could only run after the mainloop exits and the window was closed. It is unclear if this is what you intend.

  • Related