0

I've been trying all sort of things but I can't seem to get the two to work together. I was trying to display the messages I receive on telegram to a Tkinter GUI. I can't run them on separate threads because the telethon client wouldn't be able to interact with the tkinter labels.

Here's the basic structure of my code:

client = TelegramClient('robot', api_id, api_hash)

root = tk.Tk()
root.title("Telegram Watcher")
root.geometry("300x350")
some_label = tk.Label(root, text='Last Message')
some_label.pack()

@client.on(events.NewMessage(chats=channel))
async def my_event_handler(event):
    some_label.config(text=event.raw_text)
    # or some other things I could do with the root tkinter

client.start()
client.run_until_disconnected()
root.mainloop()

I know root.mainloop() will never start because client.run_until_disconnected() is already running as a loop. I have already tried threading and client.loop.run_until_complete(main()) but nothing here works as I intended. I am not too familiar in using async and await either.

Is there anyway to code this in a simple way? Thank you in advance

Austin
  • 73
  • 9
  • put one of the loops in another thread (preferably not the `mainloop`), you can look at some `threading` answers [here](https://stackoverflow.com/questions/459083/how-do-you-run-your-own-code-alongside-tkinters-event-loop) – Matiiss Dec 29 '21 at 17:11
  • @Matiiss I'm getting errors `There is no current event loop in thread Thread-1` when I try to put the telegram client loop in a thread for some reason. – Austin Dec 30 '21 at 08:35
  • That seems like you need to put all of other telegram stuff there, actually based on the question I memtioned above, it may be easier if you follow those answers and put all of tkinter in the other thread following those answers – Matiiss Dec 30 '21 at 10:07
  • You may find the [gui.py](https://github.com/LonamiWebs/Telethon/blob/master/telethon_examples/gui.py) example of the repository useful. – Lonami Dec 30 '21 at 14:20

0 Answers0