2014-10-08 2 views
1

Kivy python을 사용하는 방법을 배우려하고 있습니다. 명령을 실행하기 위해 OS 콘솔/터미널과 상호 작용하는 방법을 알고 결과를 수신합니다. 지금까지 본 자습서에서는 위젯, 버튼 등을 만드는 방법 만 보여줍니다. 예를 들어, "uname"명령을 kivy에 표시하는 결과를 얻는 방법은 무엇입니까? 아래에 그러한 코드가 있습니다. "on press"를 사용합니다. OS와 상호 작용하여 명령을 실행하고 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() 

업데이트 :

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

가능한 [파이썬에서 쉘 명령을 실행하고 출력을 캡쳐] (0120-385-333) –

+0

아니요 그냥 콘솔 출력을 받고 ..하지만 콘솔과 상호 작용할 수있는 kivy를 사용하여 GUI 응용 프로그램을 빌드 – mikie

+0

그냥 kivy를 배우는 경우 이것은 다소 복잡한 프로젝트처럼 들립니다. 아마, 서브 프로세스 모듈이 도움이 될까요? Google subprocess.call – Totem

답변

0

내가.

파이썬 첫 번째 코드 :

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

0

난 정말이 표시되지 않는 : 여기 는에는 EasyGUI 모듈을 사용하는 것 임 achieve.This에 시도의 예입니다 그와 같은 일을하는 지점이지만 원하는 경우 별도의 스레드에서 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

내가 생각하는 가장 간단한 방법은 파일 상단에 다음과 같이 파일을 씁니다.

다음 프로그램에서 당신은 단순히 넣어 파일은 프로그램과 같은 폴더에 저장됩니다

printlog("whatever you wanted printed!") 넣어 인쇄를 원하는 그 어느 때. 프로그램을 실행하는 동안 이론적으로 열 수는 있지만 포스트 모어 (postmourtum)로 더 유용합니다.

관련 문제