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


Messages In This Thread
Running linux command line apps... - by dbrdh - Jan-30-2020, 01:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Is possible to run the python command to call python script on linux? cuten222 6 629 Jan-30-2024, 09:05 PM
Last Post: DeaD_EyE
  Running 3rd party libs on Steam Deck (Arch Linux) with restricted access metulburr 0 1,793 Jan-07-2023, 10:41 PM
Last Post: metulburr
  Command line argument issue space issue mg24 5 1,279 Oct-26-2022, 11:05 PM
Last Post: Yoriz
  accept command line argument mg24 5 1,236 Sep-27-2022, 05:58 PM
Last Post: snippsat
  Pandas - error when running Pycharm, but works on cmd line zxcv101 1 1,320 Jun-18-2022, 01:09 PM
Last Post: snippsat
  How to use a variable in linux command in python code? ilknurg 2 1,547 Mar-14-2022, 07:21 AM
Last Post: ndc85430
  use subprocess on linux\pi wwith a "grep " command korenron 2 7,899 Oct-19-2021, 10:52 AM
Last Post: DeaD_EyE
  Accessing varying command line arguements Rakshan 3 2,008 Jul-28-2021, 03:18 PM
Last Post: snippsat
  Setting up multiple flask apps in the same server leoramirez 0 2,051 May-04-2021, 08:10 PM
Last Post: leoramirez
  How to input & output parameters from command line argument shantanu97 1 2,505 Apr-13-2021, 02:12 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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