Tkinter: Unable to send data via subprocess.PIPE

fady malak

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"
martineau

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.

sender.py

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

receive.py

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

unable to send json data via ajax

From Dev

Trying to send commands via Subprocess to operate with ffmpeg

From Dev

ffmpeg in Python subprocess - Unable to find a suitable output format for 'pipe:'

From Dev

Provide Data Via Named Pipe

From Dev

Unable to send email via ajax

From Dev

WCF - Unable to send data

From Dev

Pipe script and binary data to stdin via ssh

From Dev

Python subprocess module and PIPE

From Dev

Python subprocess popen pipe

From Dev

Why none direcotry send via pipe to a grep get searched result?

From Dev

Why none direcotry send via pipe to a grep get searched result?

From Dev

send/receive data via Bluetooth

From Dev

Unable to send data to libwebsocket server

From Dev

Unable to send data using QSerialPort

From Dev

Unable to send data to libwebsocket server

From Dev

Unable to send data using QSerialPort

From Dev

Yii 2: Unable to send log via mail

From Dev

unable to send email via smtp over ssl

From Dev

Content script unable to send messages via send.port.emit

From Dev

Python subprocess stdin=subprocess.PIPE and unicode

From Dev

How can tornado use pipe to async send data to client?

From Dev

How to send part of the data through the pipe/file and later interactively?

From Dev

How can tornado use pipe to async send data to client?

From Dev

read data through parent and send it to child using pipe in c

From Dev

Python - subprocess with Quotes and Pipe Grep

From Dev

Substitution of subprocess.PIPE in Python?

From Dev

Python 3 subprocess pipe blocks

From Dev

Python 3 subprocess pipe blocks

From Dev

Substitution of subprocess.PIPE in Python?