Python Forum
How to run a script within a pyqt window
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to run a script within a pyqt window
#1
Hi,

I have written a pyQt script for a window with some button indicators in it. Within this window I have written a second section which monitors the th inputs of my raspberry pi and changes the colours of my indicators depending on what input is on. However I am struggling to get the second script to run within the window without it taking over the window or running but not changing the colours if the indicators. I know the second script works as I have created a test button and to run the script when pressed and it changes the colours of the indicators fine.

How can I run my input monitoring script within my pyQt window while still being able to interact with the window as well as it live changing the colours of the indicators when input turn on and off.

Thanks in advance.


import sys
from PyQt4 import QtGui, QtCore
from PyQt4.QtGui import *
import subprocess
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)

GPIO.setup(10, GPIO.IN)
GPIO.setup(11, GPIO.IN)
GPIO.setup(12, GPIO.IN)

#state_GPIO10 = GPIO.input(10)
#state_GPIO11 = GPIO.input(11)
#state_GPIO12 = GPIO.input(12)


class Window(QtGui.QMainWindow):

	def __init__(self):
		super(Window, self).__init__()
		self.initUI()

	def initUI(self):

		self.setGeometry(50, 50, 800, 480)
		self.setWindowTitle("Master Window") 
		self.home()
		self.GPIO_control()

	def home(self):

		global button
		global button1
		global button2
		global button3
		
		button = QtGui.QPushButton("Quit", self)
		button.clicked.connect(self.close_application)
		button.resize(100, 50)
		button.move(700, 430)
		
		button1 = QtGui.QPushButton("RUNNING", self)
		button1.resize(200, 50)
		button1.move(50, 215)
		
		button2 = QtGui.QPushButton("MATERIAL REQUIRED", self)
		button2.resize(200, 50)
		button2.move(300, 215)

		button3 = QtGui.QPushButton("MAINTENANCE REQUIRED", self)
		button3.resize(200, 50)
		button3.move(550, 215)

#		button4 = QtGui.QPushButton("TEST", self)
#		button4.clicked.connect(self.GPIO_control)
#		button4.resize(200, 50)
#		button4.move(300, 320)

		self.showFullScreen()


	def close_application(self):

		close_choice = QtGui.QMessageBox.question(self, 'Double Checker', "Are you sure you want to quit?",
							QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
		if close_choice == QtGui.QMessageBox.Yes:
			sys.exit()
		else:
			pass

	
	def GPIO_control(self):
		
        while True:
			if GPIO.input(10):
				print "INPUT 10"
				button1.setStyleSheet("background-color: red")
				button2.setStyleSheet("background-color: none")
				button3.setStyleSheet("background-color: none")
			elif GPIO.input(11):
				print "input 11"
				button1.setStyleSheet("background-color: none")
				button2.setStyleSheet("background-color: red")
				button3.setStyleSheet("background-color: none")
			elif GPIO.input(12):
				print "input 12"
				button1.setStyleSheet("background-color: none")
				button2.setStyleSheet("background-color: none")
				button3.setStyleSheet("background-color: red")
			else:
				button1.setStyleSheet("background-color: none")
				button2.setStyleSheet("background-color: none")
				button3.setStyleSheet("background-color: none")
		else:
			pass

def main():
	
	app = QtGui.QApplication(sys.argv)
	GUI = Window()
	sys.exit(app.exec_())

main()
Reply
#2
Qthread can be helpful in this scenario. By using threading you can monitor the input state and simultaneously retain control on main window.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a way to call and focus any popup window outside of the main window app? Valjean 6 1,739 Oct-02-2023, 04:11 PM
Last Post: deanhystad
  Pyspark Window: perform sum over a window with specific conditions Shena76 0 1,165 Jun-13-2022, 08:59 AM
Last Post: Shena76
  Using Subprocess.Popen to start another python script running in background on Window johnb546 0 13,661 Jun-01-2018, 01:57 PM
Last Post: johnb546

Forum Jump:

User Panel Messages

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