0

声音应该紧随其后,self.scr.mainloop()但它立即发出声音。TKinterafter方法立即执行,然后在执行后暂停 3 秒。

我究竟做错了什么?文档说该函数将在暂停时间之后被调用,但它实际上是在之前发生的。我想调用音频功能def __init__(self):

    from tkinter import *
    from sqlite3 import *
    from tkinter import messagebox
    import pyttsx3
    import time
    engine = pyttsx3.init()
    voices = engine.getProperty('voices')
    engine.setProperty('voice', voices[1].id)

    admin_username1 = {"Prasham":"123456","Akshat":"0000"}
    costermer_password1 = {"user":"user"}

    class store:
        def __init__(self):
            try:
                self.scr.destroy()
                self.scr=Tk()      
                
            except:
                try:
                    self.scr=Tk()
                except:
                    pass
            
            self.scr.geometry("500x500+500+180")
            self.scr.title("Multipurpose Store")
            self.scr.maxsize("500","500")
            self.scr.minsize("500","500")
            frame1 = Frame(self.scr,borderwidth = 3)
            frame1.pack(pady = 155)
            Label(frame1, text = "Login AS",font = ("Roboto",24),background = "yellow").pack()
            b1 = Button(frame1,text = "Admin login",background = "yellow", command=lambda:self.Adminlogin())
            b1.pack(padx=25,pady =25, side = LEFT)
            b2 = Button(frame1,text = "Customer login",background = "yellow",command=lambda:self.Userlogin())
            b2.pack(padx=20,side = LEFT)

            scr.after(3, self.audio)
            self.scr.mainloop()

        def audio(self):
            
            engine.say('Hello World')
            engine.runAndWait()


        def Adminlogin(self):
            self.scr.destroy()
            self.scr=Tk()  
            self.scr.geometry("500x500+500+180")
            self.scr.title("Multipurpose Store")
            self.scr.maxsize("500","500")
            self.scr.minsize("500","500")
            Label(self.scr, text = "Login Confermation",font = ("Roboto",24)).place(x=115,y=155)
            user = Label(self.scr,text = "Username : ",font = ("Roboto",16))
            password = Label(self.scr,text = "Password : " ,font = ("Roboto",16))
            user.place(x = 125, y = 225)
            password.place(x = 125 ,y = 255 )
            uservalue = StringVar()
            passvalue = StringVar()
            
            global userentry
            global passentry

            userentry = Entry(self.scr, textvariable = uservalue)
            passentry = Entry(self.scr, textvariable = passvalue, show="*")
            userentry.place(x = 255,y =230)
            passentry.place(x = 255,y = 265)
            b3 = Button(self.scr,text = "Home",background = "yellow",command=lambda:self.__init__()).place(x=430,y=0,height=20,width=70)
            b4 = Button(self.scr,text = "login",background = "yellow",command=lambda:self.check1()).place(x=250,y=300,height=20,width=130)
            self.scr.mainloop()

        def Userlogin(self):
            self.scr.destroy()
            self.scr=Tk()  
            self.scr.geometry("500x500+500+180")
            self.scr.title("Multipurpose Store")
            self.scr.maxsize("500","500")
            self.scr.minsize("500","500")
            Label(self.scr, text = "Login Confermation",font = ("Roboto",24)).place(x=115,y=155)
            user = Label(self.scr,text = "Username : ",font = ("Roboto",16))
            password = Label(self.scr,text = "Password : " ,font = ("Roboto",16))
            user.place(x = 125, y = 225)
            password.place(x = 125 ,y = 255 )
            uservalue1 = StringVar()
            passvalue1 = StringVar()

            global userentry
            global passentry
        
            userentry = Entry(self.scr, textvariable = uservalue1)
            passentry = Entry(self.scr, textvariable = passvalue1, show="*")
            userentry.place(x = 255,y =230)
            passentry.place(x = 255,y = 265)
            b3 = Button(self.scr,text = "Home",background = "yellow",command=lambda:self.__init__()).place(x=430,y=0,height=20,width=70)
            b5 = Button(self.scr,text = "login",background = "yellow",command=lambda:self.check1()).place(x=265,y=300,height=20,width=100)
            b6 = Button(self.scr,text = "New User",background = "yellow",command=lambda:self.newuser()).place(x=265,y=327,height=19,width=100)
            self.scr.mainloop()

        def check1(self):

            a = userentry.get()
            b = passentry.get()
            
            if a=="" or b=="":
                messagebox.showerror('ERROR', 'Empty Entry is not allowed')
            
            elif a in admin_username1: 
                if admin_username1[a]==b:
                    messagebox.showinfo('Login', "You have Successfully Log In\nWelcome " +a)
                    self.AdminloginGUI()
                else:
                    messagebox.showerror('ERROR', 'INVALID PASSWORD')
            
            elif a in costermer_password1:
                if costermer_password1[a]==b:
                    messagebox.showinfo('Login', "You have Successfully Log In\nWelcome " +a)
                    self.UserloginGUI()
                else:
                    messagebox.showerror('ERROR', 'INVALID PASSWORD')

            else:
                messagebox.showinfo('ERROR', 'You are Not Registered Yet')

        def AdminloginGUI(self):
            self.scr.destroy()
            self.scr=Tk()  
            self.scr.geometry("500x500+500+180")
            self.scr.title("Admin")
            self.scr.maxsize("500","500")
            self.scr.minsize("500","500")
            b3 = Button(self.scr,text = "Home",background = "yellow",command=lambda:self.__init__()).place(x=430,y=0,height=20,width=70)

        def UserloginGUI(self):
            self.scr.destroy()
            self.scr=Tk()  
            self.scr.geometry("500x500+500+180")
            self.scr.title("User")
            self.scr.maxsize("500","500")
            self.scr.minsize("500","500")
            b3 = Button(self.scr,text = "Home",background = "yellow",command=lambda:self.__init__()).place(x=430,y=0,height=20,width=70)

        def newuser(self):
            self.scr.destroy()
            self.scr=Tk()  
            self.scr.geometry("500x500+500+180")
            self.scr.title("New User")
            self.scr.maxsize("500","500")
            self.scr.minsize("500","500")
            b3 = Button(self.scr,text = "Home",background = "yellow",command=lambda:self.__init__()).place(x=430,y=0,height=20,width=70)

    engine.runAndWait()
    store()
4

1 回答 1

1

应该self.scr.after(3000, self.audio)代替,scr.after(3, self.audio)因为延迟以毫秒为单位。

engine.runAndWait()之前也删除store()

于 2021-01-08T06:19:00.240 回答