0

I have a Tkinter text widget and I want to capture and put a server output in it.

Project folder:

project
| main.py
| servers_folder
| | default_server.py

In my main. I'm importing default_server.py with server = importlib.import_module('default_server.py'). It has to be another file tho.

main.py code:

def connect(server_name):
    for widget in frame.winfo_children():
        widget.grid_forget()

    def run():
        server = importlib.import_module(server_name)
        server.Server().start()

    command = Text(frame)
    command.grid(row=0, column=0)

    thread = t.Thread(target=run)
    thread.start()


Label(frame, text='Server name:').grid(row=0, column=0)
server_name = Entry(frame)
server_name.grid(row=0, column=1, sticky='NSEW')
Button(frame, text='Run server', command=lambda: connect(server_name.get())).grid(row=1, column=0, columnspan=2)

Server start function:

def start(self):
    print('[STARTING] Server is starting...')
    self.server.listen()
    print(f'[LISTENING] Server is listening on {self.SERVER}')
    while True:
        conn, addr = self.server.accept()
        thread = t.Thread(target=self.handle_client, args=(conn, addr))
        thread.start()

Server handle_client function:

def handle_client(self, conn, addr):
    print(f'[NEW CONNECTION] {addr} connected.')
    print(f'[ACTIVE CONNECTIONS] {t.active_count() - 1}')

    connected = True
    while connected:
        msg_lenght = conn.recv(self.HEADER).decode(self.FORMAT)
        if msg_lenght:
            msg_lenght = int(msg_lenght)
            msg = conn.recv(msg_lenght).decode(self.FORMAT)
            if msg == self.DISCONNECT_MESSAGE:
                connected = False

            print(f'[MESSAGE RECIEVED] FROM: {addr}; Message: {msg}')
            conn.send('Message received!'.encode(self.FORMAT))

    conn.close()

How can I display server output in my tkinter text widget?

Thanks for help!

1
  • Can you show your handle_client function? Commented Apr 5, 2022 at 8:55

1 Answer 1

1

It would be easily for me to see the full code, but I think you can do something similar to this:

def start(self, msg_callback):
    print('[STARTING] Server is starting...')
    self.msg_callback = msg_callback
    self.server.listen()
    print(f'[LISTENING] Server is listening on {self.SERVER}')
    while True:
        conn, addr = self.server.accept()
        thread = t.Thread(target=self.handle_client, args=(conn, addr))
        thread.start()


def handle_client(self, conn, addr):
    print(f'[NEW CONNECTION] {addr} connected.')
    print(f'[ACTIVE CONNECTIONS] {t.active_count() - 1}')

    connected = True
    while connected:
        msg_lenght = conn.recv(self.HEADER).decode(self.FORMAT)
        if msg_lenght:
            msg_lenght = int(msg_lenght)
            msg = conn.recv(msg_lenght).decode(self.FORMAT)
            self.msg_callback(msg)
            if msg == self.DISCONNECT_MESSAGE:
                connected = False

            print(f'[MESSAGE RECIEVED] FROM: {addr}; Message: {msg}')
            conn.send('Message received!'.encode(self.FORMAT))

    conn.close()


def connect(server_name):
    for widget in frame.winfo_children():
        widget.grid_forget()

    command = Text(frame)
    command.grid(row=0, column=0)
    
    def msg_callback(msg):
        command.delete('1.0', END)
        command.insert('1.0', msg)

    def run():
        server = importlib.import_module(server_name)
        server.Server().start(msg_callback)

    thread = t.Thread(target=run)
    thread.start()


Label(frame, text='Server name:').grid(row=0, column=0)
server_name = Entry(frame)
server_name.grid(row=0, column=1, sticky='NSEW')
Button(frame, text='Run server', command=lambda: connect(server_name.get())).grid(row=1, column=0, columnspan=2)
Sign up to request clarification or add additional context in comments.

6 Comments

It is not working with both command["text"] = msg or command.insert("1.0", msg)
Ok, sorry, I will try to fix it.
@BokiX I have updated the answer. Can you try with command.delete('1.0', END) command.insert('1.0', msg)
I used a simple print statement for testing and it isn't even printing anything, the function is not working. I put it like this: print(f"Message: {msg}")
@BokiX Can you then post the full code? It is hard to debug if I can't reproduce the error. Also, did you insert self.msg_callback(msg) into handle_client()?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.