3

I am using Pyrogram and Tkinter:

from tkinter import *
from pyrogram import Client

root = Tk()
app = Client("my_account")

First, I register a handler with Pyrogram:

@app.on_message()
def message(client, message):
    print("Message!")

Second, I register a handler with Tkinter:

def button(event):
    print("Button!")
    
root.bind('<Button>', button)

But how can I start the loops for Pyrogram and Tkinter? Obviously the following (or the reverse) does not work:

root.mainloop()
app.run()

Edit: Since one of the key feature of Pyrogram is that it is fully asynchronous (e.g., https://docs.pyrogram.org/start/updates#registering-a-handler), I would expect an answer based on Asyncio. Here is however my attempt with threading following comments by @SylvesterKruin.

t = Thread(target=app.run)
t.start()
root.mainloop()

Il fails with RuntimeError: There is no current event loop in thread 'Thread-1'.

Bob
  • 1,713
  • 10
  • 23
  • 3
    I don't know Pyrogram, but would using threading work? You could have the `tkinter` code in the main thread and the `pyrogram` code in another thread. – Sylvester Kruin Jan 07 '22 at 16:08
  • @SylvesterKruin This might be an answer. But one of the key feature of `pyrogram` is that it is fully asynchronous (e.g., https://docs.pyrogram.org/start/updates#registering-a-handler), so I would expect an answer based on `asyncio`. – Bob Jan 07 '22 at 16:32
  • 1
    @SylvesterKruin I have edited my question with an attempt at threading. – Bob Jan 07 '22 at 16:55
  • Why are you creating a second thread for `tkinter`, and a third thread for `pyrogram`? You need to run `tkinter` in the _main_ thread. That is, you don't need to create a separate thread for `tkinter`; just run all the `tkinter` stuff as you normally would without using threading. – Sylvester Kruin Jan 07 '22 at 17:08
  • 1
    @SylvesterKruin I have edited accordingly. But it still fails. – Bob Jan 07 '22 at 17:24
  • Hmm... well, then maybe `asyncio` is the best bet, especially since you're looking for an `asyncio` answer anyway. I'm sorry, but I hardly know anything about asynchronous programming, so I won't be able to help you out there. Best of luck! – Sylvester Kruin Jan 07 '22 at 18:27
  • 1
    As @SylvesterKruin stated in the first place, you should use a thread to sperate `pyrogram` and `tkinter` it makes things cleaner, even if there are [work arounds for asyncio and tkinter](https://stackoverflow.com/a/47896365/13629335), you will need to synchronize the api- and gui-framework anyway. [Exampels for tkinter and threading](https://stackoverflow.com/q/16745507/13629335) are available and as soon as you have something to show, that actually works, I suggest to post your code at [CodeReview](https://codereview.stackexchange.com/) where you can get additional input. – Thingamabobs Jan 07 '22 at 20:47
  • 1
    Even if I upvoted your question, since it seems usefull and has research in it. It currently states multiple question at once and isnt suitable for [so]. So I am voting to close this question, but feel free to ask a follow up question that has more focus at a particular error or unexpacted behavior. You may ask this question at [Software Engineering](https://softwareengineering.stackexchange.com/) or another [sites by Stack Exchange](https://stackexchange.com/sites?view=grid#) – Thingamabobs Jan 07 '22 at 20:54
  • @Bob Didn't the answer I gave below work? – Samuel Kazeem Jan 17 '22 at 20:29

1 Answers1

-2

A bit of looking through the pyrogram examples and This answer to prevent blocking. I think this might be helpful.

import asyncio
import tkinter as tk
from pyrogram import Client
import threading

api_id = 12345
api_hash = "0123456789abcdef0123456789abcdef"
app = Client("my_account", api_id, api_hash)

def init_async():
    print('starting')
    loop = asyncio.get_event_loop()  # create loop
    event = asyncio.Event()  # create event
    #new thread to run loop. Prevents tkinter from blocking
    thr = threading.Thread(target=run_main, args=(loop, event))
    thr.start()

def run_main(_loop, _e):
    try:
        _loop.run_until_complete(main())
    finally:
        _loop.close()


async def main():
    async with app:
        await app.send_message("me", "Greetings from **Pyrogram**!")

@app.on_message()
async def message(client, message):
    print("Message!")

def button(event):
    print("Button!")

root = tk.Tk()
root.bind('<Button>', button)
tk.Button(master=root, text='Start Pyrogram', command=init_async).pack()
root.mainloop()
Samuel Kazeem
  • 787
  • 1
  • 8
  • 15