I'm looking to make a GUI telethon login and event listener using tkinter, but the GUI freeze when working with telethon, so I tried to use thread the first button is 'connect' which is connecting to telegram account, and 'send' button to start an event listening when a new message arrive but it's not working.
This is what I tried
root = tkinter.Tk()
root.geometry("200x100")
def refresh():
print("update")
root.update()
root.after(1000, refresh)
def connect():
global client
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
client = TelegramClient(phone, api_id, api_hash, flood_sleep_threshold=3)
client.connect()
if not client.is_user_authorized():
client.send_code_request(phone)
try:
client.sign_in(phone, code=input(f'Enter the code: {phone} : '))
except SessionPasswordNeededError:
password = input("Enter 2fa password : ")
client.sign_in(password=password)
print(client.get_me())
async def handler(event):
print(event)
def send():
client.add_event_handler(handler, events.NewMessage)
client.run_until_disconnected()
def click():
print("Clicked!")
B = tkinter.Button(root, text="Connect", command=lambda: threading.Thread(target=connect).start())
B.pack()
BB = tkinter.Button(root, text="Send", command=lambda: threading.Thread(target=send).start())
BB.pack()
C = tkinter.Button(root, text="Hello", command=click)
C.pack()
refresh()
root.mainloop()
I've got the following error message when 'send' button pressed
RuntimeError: There is no current event loop in thread 'Thread-2'.