Python Forum
Running linux command line apps...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Running linux command line apps...
#1
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()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Accessing varying command line arguements Rakshan 3 172 Jul-28-2021, 03:18 PM
Last Post: snippsat
  Setting up multiple flask apps in the same server leoramirez 0 310 May-04-2021, 08:10 PM
Last Post: leoramirez
  How to input & output parameters from command line argument shantanu97 1 556 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  Error when running script on startup in Linux NoahTheNerd 0 412 Mar-07-2021, 04:54 PM
Last Post: NoahTheNerd
  how to run linux command with multi pipes by python !! evilcode1 2 483 Jan-25-2021, 11:19 AM
Last Post: DeaD_EyE
  Sound Approach for Running a Shell Command? matt_the_hall 8 830 Dec-14-2020, 02:52 PM
Last Post: matt_the_hall
  Running Python in Command Prompt Anwar 3 862 Nov-15-2020, 03:15 PM
Last Post: snippsat
  Passing List of Objects in Command Line Python usman 7 999 Sep-27-2020, 03:45 PM
Last Post: ndc85430
  Error when running mktorrent subprocess command pythonnewbie138 4 815 Sep-16-2020, 01:55 AM
Last Post: pythonnewbie138
  Taking Multiple Command Line Argument Input bwdu 6 1,450 Mar-29-2020, 05:52 PM
Last Post: buran

Forum Jump:

User Panel Messages

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