Python Forum
how to execute .py script when button is clicked
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to execute .py script when button is clicked
#1
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
Reply
#2
(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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] Execute the script in remote and get the output maiya 2 1,368 Jul-10-2022, 05:03 AM
Last Post: maiya
  [PyQt] Button clicked not working catlessness 10 8,082 Oct-21-2021, 12:36 PM
Last Post: catlessness
  [Tkinter] Hide clicked buttons Rubberduck 6 3,444 Jun-02-2021, 12:44 PM
Last Post: Rubberduck
  Button in GUI linked to new python script blakefindlay 5 13,565 Jan-30-2021, 02:47 AM
Last Post: deanhystad
  [tkinter] not getting checkbutton value when clicked OogieM 5 5,882 Sep-20-2020, 04:49 PM
Last Post: deanhystad
  [PyQt] Avoid clicked event from button when button is not physically selected and clicked mart79 2 2,309 May-05-2020, 12:54 PM
Last Post: mart79
  [Tkinter] Displaying Data from a database and run a function when clicked? PythonNPC 1 2,022 Mar-11-2020, 08:16 PM
Last Post: Larz60+
  tkinter button not accessing the command when clicked jhf2 1 3,500 Nov-23-2019, 10:17 PM
Last Post: DT2000
  [Tkinter] How to check after 30 minutes if Buttons have been clicked? bmghafoor 1 2,076 Aug-09-2019, 04:57 PM
Last Post: Yoriz
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 4,947 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020