I recieve real-time market prices from Bitcoin Exchange WebSocket and want display them on tkinter. But the mainloop doesn't work. What should I do? help..
import websockets
import json
""" websocket client(real time data receive)"""
async def upbit_client():
uri = "wss://api.upbit.com/websocket/v1"
async with websockets.connect(uri, ping_interval=60) as websocket:
subscribe = [{"ticket":"test"}, {"type":"ticker", "codes":["KRW-BTC"], "isOnlyRealtime": True}, {"format":"SIMPLE"}]
subscribe = json.dumps(subscribe)
await websocket.send(subscribe)
while True:
data = await websocket.recv()
data = json.loads(data)
print('BTC: ', round(data['tp']), 'percent', round(data['scr']*100, 2), '%')
import tkinter as tk
import asyncio
"""tkinter gui"""
class Win(tk.Tk):
def __init__(self, loop):
self.loop = loop
self.root = tk.Tk()
self.root.title("Btc")
self.root.geometry("640x400+100+100")
self.root.resizable(False, False)
self.label = tk.Label(self.root, text="hi.", bg='#fff', fg='#f00',pady=10,padx=10, font=10)
self.label.pack()
self.after(10000, self.loop.run_until_complete(upbit_client())) # working
self.root.mainloop() # not working
# how to working together?
Win(asyncio.get_event_loop())
self.after(10000, self.loop.run_until_complete(upbit_client())) in infinite loop and self.root.mainloop() cannot run.