Soumojit Dalui
Soumojit Dalui

Reputation: 13

How to stop a function without closing the application?

So, I want to close the function on_click_on_start which has while loop in it. And, dont want to close the window after hitting the stop button. Right now, I was using sys.exit() which crashed the program for some reason but, when I used only quit() it was closing the entire app. I am doing this on pyQt5. I am noob to GUI so please help me out. This is extra text which I have added so that I can post the question. Please ignore this.

import res1
import res
from PyQt5 import QtCore, QtGui, QtWidgets
import os
import pyaudio
import numpy as np
import pyautogui
import time


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.setEnabled(True)
        MainWindow.resize(800, 600)
        MainWindow.setMaximumSize(QtCore.QSize(800, 600))
        font = QtGui.QFont()
        font.setFamily("Californian FB")
        font.setPointSize(14)
        MainWindow.setFont(font)
        MainWindow.setMouseTracking(True)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(
            "../../../../Pictures/ugo_trans.ico"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        MainWindow.setWindowIcon(icon)
        MainWindow.setAutoFillBackground(True)
        MainWindow.setStyleSheet("")
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setAutoFillBackground(False)
        self.centralwidget.setStyleSheet("background-color: rgb(255, 0, 0);")
        self.centralwidget.setObjectName("centralwidget")
        self.Backgroundimage = QtWidgets.QLabel(self.centralwidget)
        self.Backgroundimage.setEnabled(True)
        self.Backgroundimage.setGeometry(QtCore.QRect(9, 9, 782, 582))
        sizePolicy = QtWidgets.QSizePolicy(
            QtWidgets.QSizePolicy.Maximum, QtWidgets.QSizePolicy.Maximum)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(
            self.Backgroundimage.sizePolicy().hasHeightForWidth())
        self.Backgroundimage.setSizePolicy(sizePolicy)
        self.Backgroundimage.setMaximumSize(QtCore.QSize(800, 600))
        self.Backgroundimage.setStyleSheet("")
        self.Backgroundimage.setText("")
        self.Backgroundimage.setPixmap(
            QtGui.QPixmap(":/newPrefix/Untitled-1.png"))
        self.Backgroundimage.setScaledContents(True)
        self.Backgroundimage.setObjectName("Backgroundimage")
        self.Start = QtWidgets.QPushButton(self.centralwidget)
        self.Start.setGeometry(QtCore.QRect(60, 240, 221, 111))
        font = QtGui.QFont()
        font.setPointSize(16)
        self.Start.setFont(font)
        self.Start.setStyleSheet("background-color: rgb(0, 0, 0);\n"
                                 "color: rgb(255, 255, 255);")
        self.Start.setObjectName("Start")
        self.Stop = QtWidgets.QPushButton(self.centralwidget)
        self.Stop.setGeometry(QtCore.QRect(510, 240, 221, 111))
        font = QtGui.QFont()
        font.setPointSize(16)
        self.Stop.setFont(font)
        self.Stop.setStyleSheet("background-color: rgb(0, 0, 0);\n"
                                "color: rgb(255, 255, 255);")
        self.Stop.setObjectName("Stop")
        self.Title = QtWidgets.QLabel(self.centralwidget)
        self.Title.setGeometry(QtCore.QRect(210, 70, 361, 61))
        font = QtGui.QFont()
        font.setPointSize(48)
        self.Title.setFont(font)
        self.Title.setStyleSheet("color: rgb(255, 255, 255);\n"
                                 "background-color: rgb(0, 0, 0, 60);")
        self.Title.setObjectName("Title")
        MainWindow.setCentralWidget(self.centralwidget)

        self.Start.clicked.connect(self.on_click_on_start) # button pressed on start
        self.Stop.clicked.connect(self.on_click_on_stop) # button pressed on stop and it only stops the on_click_on_start

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def on_click_on_start(self):
        CHUNK = 2**11
        RATE = 48016

        p = pyaudio.PyAudio()
        stream = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True,
                        frames_per_buffer=CHUNK)

        start = time.time()

        while 1:
            data = np.fromstring(stream.read(CHUNK), dtype=np.int16)
            peak = np.average(np.abs(data))*2
            bars = 100*peak/2**16
            if bars > 1:
                pyautogui.keyDown("NUM0")
                start = time.time()
                print('down')

            else:
                pyautogui.keyUp("NUM0")
                print('up')
                if time.time() - start > 5:
                    stream.stop_stream()
                    stream.close()
                    p.terminate()
                    break

    def on_click_on_stop(self):
        # Help me with this function

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Talk to Mute"))
        self.Start.setText(_translate("MainWindow", "Start"))
        self.Stop.setText(_translate("MainWindow", "Stop"))
        self.Title.setText(_translate("MainWindow", "Talk to Mute"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Upvotes: 0

Views: 145

Answers (1)

notNowOnlyCoding
notNowOnlyCoding

Reputation: 315

Generally using variables is easiest way to manage status. You can use variable for update running status.

running_status = false

Use like

self.running_status = true

in on_click_on_start.

Use

self.running_status = false

in on_click_on_stop. So now you can call your function if running_status is true.

Upvotes: 2

Related Questions