Advertisement
jwinterm

multiprocessANDpexpectEXAMPLEkivy

May 31st, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.37 KB | None | 0 0
  1. from kivy.app import App
  2. from kivy.lang import Builder
  3. from kivy.uix.boxlayout import BoxLayout
  4. from kivy.properties import ObjectProperty
  5. import sys
  6. if sys.platform == 'win32':
  7.     import winpexpect
  8. elif sys.platform == 'linux' or sys.platform == 'linux32':
  9.     import pexpect
  10. from multiprocessing import Process, Queue
  11.  
  12.  
  13. Builder.load_string('''
  14. #:kivy 1.8.0
  15.  
  16. <RootWidget>:
  17.    label_text: label_text
  18.    Label:
  19.        id: label_text
  20.        text: 'initial'
  21.        ''')
  22.  
  23.  
  24. def puthello(q):
  25.     if sys.platform == 'win32':
  26.         child = winpexpect.winspawn("cmd", timeout=9000)
  27.     elif sys.platform == 'linux' or sys.platform == 'linux32':
  28.         child = pexpect.spawn("cmd", timeout=9000)
  29.     while True:
  30.         child.sendline('echo hello')
  31.         child.expect('\r\n')
  32.         q.put(child.before+child.after)
  33.  
  34.  
  35. class RootWidget(BoxLayout):
  36.     """Root Kivy widget class"""
  37.     label_text = ObjectProperty()
  38.     q = Queue()
  39.     p = Process(target=puthello, args=(q,))
  40.  
  41.     def __init__(self):
  42.         super(RootWidget, self).__init__()
  43.         self.launchprocess()
  44.  
  45.     def launchprocess(self):
  46.         self.p.start()
  47.         while True:
  48.             print self.q.get()
  49.             self.label_text.text = self.q.get()
  50.  
  51.  
  52. class ExampleApp(App):
  53.     def build(self):
  54.         return RootWidget()
  55.  
  56.  
  57. if __name__ == '__main__':
  58.     ExampleApp().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement