Python Forum
how to execute .py script when button is clicked - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: how to execute .py script when button is clicked (/thread-10562.html)



how to execute .py script when button is clicked - D_frucht - May-25-2018

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


RE: how to execute .py script when button is clicked - nilamo - Jun-22-2018

(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)