0

I have been working with binance websocket. Worked well if the start/stop command is in the main programm. Now I wanted to start and stop the socket through a GUI. So I placed the start/stop command in a function each. But it doesn't work. Just no reaction while calling the function. Any idea what's the problem?

Here the relevant parts of my code (I am quite new to python, any hints to this code are welcome):

    def start_websocket(conn_key):

        bm.start()


    def stop_websocket(conn_key):

        bm.close()


    def process_message(msg):

        currentValues['text']= msg['p']

    # --- main ---

    PUBLIC = '************************'
    SECRET = '************************'

    client = Client(api_key=PUBLIC, api_secret=SECRET)
    bm = BinanceSocketManager(client)
    conn_key = bm.start_trade_socket('BNBBTC', process_message)

    # create main window and set its title
    root = tk.Tk()
    root.title('Websocket')

    # create variable for displayed time and use it with Label
    label = tk.Label(root)
    label.grid(column=5, row=0)
    #root.geometry('500x500')
    bt_start_socket = tk.Button(root, text="Start Websocket", command=start_websocket(conn_key))
    bt_start_socket.grid (column=1, row=1)

    bt_stop_socket = tk.Button(root, text="Sop Websocket", command=stop_websocket(conn_key))
    bt_stop_socket.grid (column=1, row=10)
Jaanus Varus
  • 3,508
  • 3
  • 31
  • 49
TPlesch
  • 1
  • 1

2 Answers2

0

I figured out how to do this. The start and stop command should be in one function. The function is called with a parameter to start or stop. Interestingly the conn_key has to be global. Otherwise a new Websocket is opened if the function is called again for closing. As I told earlier: I am quite new to python. So, no guarantee that this is the best way to du it. It just worked ;-)

def start_stop_websocket(switch):

global conn_key

if switch == 'on':
    bm.start()
    print('started')

if switch == 'off':
    bm.stop_socket(conn_key)
    bm.close()
    print('stoped')
TPlesch
  • 1
  • 1