3

I have been trying to learn how to use Kivy python and I would like to Know how to interact with the Os console/terminal to run commands and receive results. The tutorials I have seen so far only show how to create widgets,buttons,etc For example how do You get the result from running the command "uname" displayed in kivy. With such code below. using "on press". How can I let it interact with the OS run a command and display it back in the kivy app. Is there any tutorial on creating desktop apps/utilities

    from kivy.app import App
    from kivy.uix.button import Button

    class tutap(App):
        def build(self):
            return Button(text="Press here")

    tutap().run()

Update: Here is example of what Im trying to achieve.This uses the easygui module:

    import subprocess
    from easygui import *
    msg= "what you want"
    out = subprocess.check_output("uname -a",shell=True)
    title = "My choice"
    choices=["kernel version","nothing"]
    choice=boolbox(msg,title,choices)
    if choice==1:
        msgbox(out)
    elif choice==0:
        msgbox("The End")
mikie
  • 95
  • 1
  • 1
  • 8
  • possible duplicate of [Running shell command from python and capturing the output](http://stackoverflow.com/questions/4760215/running-shell-command-from-python-and-capturing-the-output) – kitti Oct 08 '14 at 19:48
  • Not just getting console output..but building a gui app using kivy that can interact with console – mikie Oct 08 '14 at 20:00
  • This sounds like a rather complicated project if you are just learning kivy. Perhaps, the subprocess module could help? Google subprocess.call – Totem Oct 08 '14 at 22:53
  • 1
    The reason kivy tutorials don't show this is that it's nothing to do with kivy. You can interact with OS in any normal python way, and display the results in any kivy way, but these are independent components of the task. As others have said, you can look at the subprocess module if you literally want to call a simple command and get the result, and in kivy you could display the result in a label just like you would any other text. – inclement Oct 08 '14 at 23:36
  • If you want to run a command and get output, then this is a duplicate. If you want to build a terminal-like app in Kivy, that question is too broad for SO. – kitti Oct 09 '14 at 14:51

3 Answers3

1

The simplest way I can think to do this is just make a function that writes to a file at the top of your file like this

def printlog(message):
    with open('./log.txt','a') as f: f.write(message+"\n")

then in your program when ever you want the print out put simply put printlog("whatever you wanted printed!")

The file will be saved in the same folder as your program. More of a you can theoretically open it while the program is running, but this is more useful as a postmourtum.

jeffpkamp
  • 2,732
  • 2
  • 27
  • 51
0

I don't really see the point of doing something like that but if you want you can call App.run() method in separate thread so it won't block command line.

An example using cmd module:

import logging
logging.getLogger("kivy").disabled = True

from kivy.app import App
from kivy.uix.listview import ListView

from cmd import Cmd
from threading import Thread

class MyApp(App):
    def build(self):
        self.lv = ListView()
        return self.lv  

    def update(self, line):
        self.lv.adapter.data.append(line)
        return "list updated"

class MyCmd(Cmd, object):
    def __init__(self, app, *args):
        super(HelloWorld, self).__init__(*args)
        self.app = app

    def do_EOF(self, line):
        self.app.stop()
        return True

    def default(self, line):
        ret = self.app.update(line)
        print(ret)

if __name__ == '__main__':
    app = MyApp()
    Thread(target=app.run).start()
    MyCmd(app).cmdloop()
Nykakin
  • 8,657
  • 2
  • 29
  • 42
0

Here I how I went about getting console command output.

The python code first:

    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    from kivy.uix.popup import Popup
    from kivy.properties import ObjectProperty
    from kivy.uix.label import Label 
    import subprocess

    class shellcommand(BoxLayout):
        first=ObjectProperty()
        second=ObjectProperty()
        third=ObjectProperty()

        def uname(self):
            v=subprocess.check_output("uname -a",shell=True)
            result=Popup(title="RESULT",content=Label(text="kernel is\n" + v))
            result.open()
        def date(self):
            d=subprocess.check_output("date",shell=True)
            res=Popup(title="DATE",content=Label(text="the date today is\n" + d))
            res.open()
        def last(self):
            last=subprocess.check_output("w",shell=True)
            ls=Popup(title="LOGIN",content=Label(text="logged in \n" + last))
            ls.open()


    class shellApp(App):
        def build(self):
            return shellcommand()

    shellApp().run()

And then the kivy file named shellapp.kv

<shellcommand>:
orientation: "vertical"
first:one
second:two
third:three
canvas:
    Rectangle:
        source: "snaps.png" #location of any picture
        pos: self.pos
        size: self.size



BoxLayout:
    orientation: "horizontal"
    Button:
        id:one
        text: "UNAME"
        background_color: 0,0,0,1
        font_size:32
        size_hint:1,None
        on_press: root.uname()


    Button:
        id:two      
        text: "DATE"
        background_color: 1,1.5,0,1
        font_size:32
        size_hint:1,None
        on_press: root.date()


    Button:
        id: three
        text: "LOGGED IN"
        background_color: 1,0,0,1
        font_size:32
        size_hint: 1,None
        on_press: root.last()

If there is a way to improve this code please let Me know how to.Thanks

mikie
  • 95
  • 1
  • 1
  • 8