2014-10-08 143 views
1

我一直在试图学习如何使用Kivy python,我想 知道如何与Os控制台/终端进行交互以运行命令,并且 会收到结果。目前为止我看到的教程仅显示如何创建 小部件,按钮等 例如,如何从运行显示在kivy中的命令“uname” 获得结果。下面有这样的代码。使用“按下”。我如何让它与操作系统交互运行命令并将其显示回kivy应用程序。是否有创建桌面应用/公用事业如何获取控制台输出使用kivy打印

任何教程
from kivy.app import App 
    from kivy.uix.button import Button 

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

    tutap().run() 

更新: 这里是例子我试着去achieve.This什么使用easygui模块:

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") 
+0

[从python运行shell命令并捕获输出]可能的重复(http://stackoverflow.com/questions/4760215/running-shell-command-from-python-and-capturing-the-output) – 2014-10-08 19:48:13

+0

不可以只是获取控制台输出..但使用可以与控制台交互的kivy构建一个gui应用程序 – mikie 2014-10-08 20:00:35

+0

如果您刚刚学习kivy,这听起来像是一个相当复杂的项目。也许,子进程模块可以帮助? Google subprocess.call – Totem 2014-10-08 22:53:19

回答

0

在这里,我怎么去有关获取控制台命令的输出。

的Python代码第一:

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() 

然后是kivy文件名为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() 

如果没有改善这种代码,请让我知道路怎么to.Thanks

+0

不是_单行文档_,而不是_ **一个** _ – Nearoo 2017-02-18 23:17:03

0

我真的不看点做这样的事情,但如果你想你可以在单独的线程中调用App.run()方法,所以它不会阻止命令行。

使用cmd模块的示例:

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() 
0

我能想到的最简单的方法就是写一个函数,写入文件顶部的文件,就像这样

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

然后在你的程序时,你永远要打印出来把简单的把printlog("whatever you wanted printed!")

文件将被保存在同一文件夹作为程序。 在程序运行时,理论上可以打开更多的程序,但作为后期程序,这更有用。