-1

So I want to know how I can make a delay between executing two functions. The goal is to replace regular, blank button by black after it was on screen for one second. My current program, simplified looks like this, and it just delays the the execution of CreateInterface():

class Program(Frame):
    def __init__(self,root):
        self.root=root
        self.root.title('Test')
        super().__init__(self.root)
        self.grid()
        self.Start()
        return

    def Start(self):
        startbtn=Button(self,width=5, font=('Calibri',16,'bold'), height=2, text='start',command=lambda:self.CreateInterface())
        startbtn.grid(row=1,column=1)

    def CreateInterface(self):
        time.import
        btn1=Button()
        btn1.grid(row=1,column=1)
        time.sleep(10)
        self.Function2(self)
        return

    def Function2(self):
        btn2=Button(bg='black')
        btn2.grid(row=1,column=1)
        return
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685

2 Answers2

0

Use time.sleep to pause program execution for a certain amount of time. If you wanted to pause for 1 second after calling CreateInterface, change it to this:

def CreateInterface(self):
        btn1=Button()
        btn1.grid(row=1,column=1)
        time.sleep(10)
        self.Function2(self)
        time.sleep(1)

Don't forget to import time when you do this.

Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
  • No, I want to call CreateInterface the moment I click start, then 1 second passes and then Function2() is called; I want a blank button to stay for a second after the start button is clicked and then a black button replace it. The problem is that after I click start, a second passes without creating interface, and then a black button is immediately spawned. In real program, I have a much bigger frame than one button, so I am sure it isn't a problem of not creating a blank button at all. – Robi Cvjetinović Dec 19 '18 at 18:31
  • @RobiCvjetinović then just put `time.sleep` in a different function. – Pika Supports Ukraine Dec 19 '18 at 18:32
0

In a GUI interface, calling time.sleep makes the whole process wait, so the application appears to freeze. With Tk in Python, a way to do is to use the Tk after method on a window or frame, and then call a function that makes the necessary change to your Button. There are examples of how to do this at How to create a timer using tkinter

Win
  • 551
  • 2
  • 5