Async and Tkinter function

Asked

Viewed 42 times

0

I’m trying to integrate the async function of the Telethon below to the command of a button on the Tkinter, but I don’t know how to proceed... The function below works for sending message at the username, but would like the message to be the variable cpf.

async def main():
    await client.send_message('username', 'message')

with client:
    client.loop.run_until_complete(main())

I tried with the code below but the function runs main soon when initializing the code, and not at the click of the button, then cpf does not exist yet and I get the error ValueError: The message cannot be empty unless a file is provided

from telethon import TelegramClient, events, sync
from tkinter import *

api_id = xxxx
api_hash = 'xxxxx'

client = TelegramClient('session_name', api_id, api_hash)
client.start()

root = Tk()
root.title('ME CONSULTAS')

canvas = Canvas(root)
canvas.pack()

cpf_entry = Entry(canvas)
cpf_entry.pack()
cpf = cpf_entry.get()

async def main():
    await client.send_message('consignadobot', cpf)

with client:
    client.loop.run_until_complete(main())

search_btn = Button(canvas, text='Consultar', command=main)
search_btn.pack()

root.mainloop()

1 answer

0

I don’t understand the concept async, however you can take what it contains in Entry inside the Main() function, maybe this solves the problem.

async def main():
    await client.send_message('consignadobot', cpf_entry.get())

Browser other questions tagged

You are not signed in. Login or sign up in order to post.