Skip to content

Instantly share code, notes, and snippets.

@paulproteus
Last active June 16, 2020 05:57
Show Gist options
  • Save paulproteus/b623b8d8a5894bfe77c293841d90ff94 to your computer and use it in GitHub Desktop.
Save paulproteus/b623b8d8a5894bfe77c293841d90ff94 to your computer and use it in GitHub Desktop.
TCP asyncio hello world

Look at this TCP hello world video

"""
My first application
"""
import asyncio
import toga
from toga.style import Pack
from toga.style.pack import COLUMN, ROW
from rubicon.java.android_events import AndroidEventLoop
class HelloWorld(toga.App):
def startup(self):
loop = AndroidEventLoop()
asyncio.set_event_loop(loop)
loop.run_forever_cooperatively()
main_box = toga.Box(style=Pack(direction=COLUMN))
name_label = toga.Label("Your name: ", style=Pack(padding=(0, 5)))
self.name_input = toga.TextInput(style=Pack(flex=1))
name_box = toga.Box(style=Pack(direction=ROW, padding=5))
name_box.add(name_label)
name_box.add(self.name_input)
async def hello_world_async():
name_label.text = "TCP conn..."
print("hi, sleeping for 1...")
await asyncio.sleep(1)
print("ok, done sleeping")
print("async hello from hello_world_async()")
print("Gonna try tcp")
reader, writer = await asyncio.open_connection("195.34.89.241", 7)
print("Sending hi")
writer.write(b"hi")
await writer.drain()
data = await reader.read(100)
print(f"Received: {data.decode()!r}")
name_label.text = data.decode()
print("Close the connection")
writer.close()
await writer.wait_closed()
asyncio.ensure_future(hello_world_async())
button = toga.Button(
"Say Hello!", on_press=self.say_hello, style=Pack(padding=5)
)
main_box.add(name_box)
main_box.add(button)
self.main_window = toga.MainWindow(title=self.formal_name)
self.main_window.content = main_box
self.main_window.show()
async def say_hello(self, widget):
print("Hello", self.name_input.value)
def main():
return HelloWorld()
@paulproteus
Copy link
Author

Screen Recording 2020-06-15 at 10 52 57 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment