Python Forum

Full Version: Running linux command line apps...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Herd,

Have done a bit of C/C++, Java, PHP, VB, Basic in the past and Python is new to me. I'm wanting to learn it as I have teamed a Raspberry Π 4 with my iPad pro 12.9 - killer team!

Just playing about with stuff. The cooling fan on the Π is annoying/noisy so was looking to team the π with my Arduino Uno and am writing an app to get the core temperature from the Π then send a signal via GPIO to the Arduino board which will then turn on a relay and the 5v fan - simples!! 8-)

Never written GUI for Linux before [Raspbian} but after a lot of playing and enjoyment, I'm at the stage where I have a simple GUI app running on the π which calls command line app to get the temperature and display it. All good fun.

However, to progress, I want to find a way of toggling the temperature detection on or off using a button in the app to toggle this on or off. Where I'm stuck is that I have a function that tests whether the switch is on or off. If on, it then calls the function that gets the temperature and returns the value.

The issue is in the second function: how do I check to see whether the button has been pressed in the first function to stop monitoring temperature in the second - I really need both functions to be running as loops but that seems beyond what Python does as an interpreted, procedural language.

Am reading up about threads at the moment. Am I on the right lines here? Can this be achieved or am I barking up the wrong tree...?

TIA..

import gi
import subprocess
import time
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk

test = 1 #global variable for button click token

        

class MyWindow(Gtk.Window): # declare instance of window class

    def __init__(self): # define instance layout and members
        
        Gtk.Window.__init__(self, title="PiTemp") # declare window with title
        s=self.get_screen()
        horiz=s.get_width()
        vert=s.get_height()
        self.move(horiz/3,vert/3)
        
        self.box = Gtk.Box(spacing=6) # declare a box container with space between box child contents
        self.add(self.box) # add the box container inside window

        self.button = Gtk.Button(label="Click Here") # declare a button to the box with a label
        self.button.connect("clicked", self.on_button_clicked) # connect button to the clicked event
        self.box.pack_start(self.button, True, True, 0) # put the button in the box in 1st vacant left position with expansion used as child size and no space between siblings
        #self.add(self.button)
        self.labl = Gtk.Label() # as prev three lines but adds a label child
        #self.labl.set_text('              ') # set intial string
        self.box.pack_start(self.labl, True, True, 20) #draw the button and display box
        #self.add(self.labl)
        
    def run_shell(self, widget): #### Really asking to find a way for this to loop unless the button is clicked - can it be done??###
        print(test), print('in function')
        if (test % 2==0): # if even nmber ie switch is ON
            print(test), print('in while') # console trace output
            out = subprocess.Popen(["vcgencmd", "measure_temp"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True) #run terminal command and return temperature
        
            try:
                outs, errs = out.communicate(timeout=15)
                self.labl.set_text('\n'+ outs + '\n' + 'working') #send the temperature to the app display
            #time.sleep(1)
            
            except TimeoutExpired:
                out.kill()
                outs, errs = out.communicate()
                print(outs)                
            #time.sleep(5)
        else:
            self.labl.set_text('\n' + 'Off') # switch is OFF - we're not monitoring
            
    def test_state(self,widget): # think this is defined but not used at the moment - just an idea to test the button state
        global test
        if (test % 2==0):
            self.run_shell(widget)

    def on_button_clicked(self, widget): # acts on the button clicked event and toggles the button state by incrementing the global token variable
        #print("Hello World")
        global test
        test = test + 1
        self.run_shell(widget)
        #time.sleep(5)
        
       
win = MyWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()