Python Forum

Full Version: how to execute .py script when button is clicked
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hello,

im using a pyqt5 for building a gui.

i want that clicking a button will execute gps.py script.

here is the relevant piece of code

def window():
    app = QtWidgets.QApplication(sys.argv)
    w = QtWidgets.QWidget()

    blunch = QtWidgets.QPushButton(w)
    blunch.setStyleSheet("background-color:white;")
    blunch.resize(300,96)
    blunch.move (200,300)
    blunch.setText("Lunch")
    blunch.setFont(QtGui.QFont('', 15))
    blunch.clicked.connect(gps.runscript())
here is the code from the gps.py:
from random import uniform
import time

def runscript():

    for gps in range(0, 10):
        gps = round(uniform(31.852863, 31.853108), (6))
        time.sleep(2)

        print(gps)
the "error that i get is what the gps.py runs before the GUI, regardless if the button was clicked.

thanks
(May-25-2018, 07:46 AM)D_frucht Wrote: [ -> ]
    blunch.clicked.connect(gps.runscript())
You're calling the function, and then binding whatever it returns as the button's event handler. Instead, just bind the function directly:
    blunch.clicked.connect(gps.runscript)