I want to do send & receive data program. No data sent to receive.py
and when I close tkinter GUI, I get an empty list.
sender.py
import Tkinter as tk
import sys
def send(x):
sys.stdout.write(x)
return sys.stdout.flush()
class SampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.entry = tk.Entry(self)
self.button = tk.Button(self, text="Get", command=self.on_button)
self.button.pack()
self.entry.pack()
def on_button(self):
x = ''.join(str(self.entry.get()))
return send(x)
app = SampleApp()
app.mainloop()
receive.py
import subprocess
import time
xx = subprocess.Popen(["python","sender.py"], stdout=subprocess.PIPE,
stdin=subprocess.PIPE, shell=True)
while True:
time.sleep(0.5)
if xx.stdout.readlines():
print xx.stdout.readlines()
else:
print "wait data"
I think the problem is the way you're trying to get data back from sender.py
in receive.py
. Your code with the changes shown below seems to work for me. Note I cleaned-up your Tkinter GUI code to only do what was necessary.
import Tkinter as tk
import sys
def send(x):
sys.stdout.write(x)
sys.stdout.flush()
class SampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
self.entry = tk.Entry(self)
self.button = tk.Button(self, text="Get", command=self.on_button)
self.button.pack()
self.entry.pack()
self.entry.focus_set() # added (optional)
def on_button(self):
x = ''.join(str(self.entry.get()))
send(x)
app = SampleApp()
app.mainloop()
import subprocess
with subprocess.Popen(["python","sender.py"], stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, stdin=subprocess.PIPE,
shell=True).stdout as output:
for line in output:
print(line)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments