【发布时间】:2017-08-05 04:36:40
【问题描述】:
我想做发送和接收数据程序。没有数据发送到receive.py,当我关闭 tkinter GUI 时,我得到一个空列表。
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"
【问题讨论】:
-
你如何开始你的程序?它们需要连接在“管道”中才能使管道转发工作。
-
另请注意,在
receive.py第一次调用xx.stdout.readlines()时,它会读取所有行,因此第二次调用时不会剩下任何行。 -
piiipe.py是什么? -
@martineau piiipe.py == sender.py
-
@martineau 将 xx.stdout.readlines() 更改为 xx.stdout.readline() 没有任何反应
标签: python tkinter subprocess pipe