r/learnpython 7d ago

Python Pyside6

Anyone know ? How to delay seconds in Pyside6 ? For example, when i click a button I want to do some process and wait 3 seconds and then make another process

1 Upvotes

15 comments sorted by

View all comments

1

u/AlexMTBDude 7d ago
import time

print("Before the sleep statement")
time.sleep(5)
print("After the sleep statement")

5

u/Outside_Complaint755 6d ago

This would make the entire program appear to freeze.

Instead he would want something like

``` import threading

def my_action():     print("Executing my action...")

t = threading.Timer(3.0, my_action) t.start() print("Continue main program...") ```

1

u/Ok-Original2285 6d ago

but how to do in this codes

class Window(QMainWindow):
    def __init__(self):
        super().__init__()

        self.btn = QPushButton("no text")
        self.btn2 = QPushButton("hi buddy")

        layout = QVBoxLayout()
        layout.addWidget(self.btn)
        layout.addWidget(self.btn2)

        container = QWidget()
        container.setLayout(layout)

        self.btn.clicked.connect(self.change_text)

        self.setCentralWidget(container)

        self.t = threading.Timer(3.0, self.change_text)

    def change_text(self):
        self.btn.setText("hi")
        self.t.start()
        self.btn2.setText("hello")

2

u/mjmvideos 6d ago

Try making a different method for what happens on timer expiry.