For example, when I try to run a pyrogram in tkinter in this code, there is no problem, but when I make a loop or a long process, tkiner does not work.
Example Code;
import tkinter as tk
from pyrogram import Client
from pyrogram.errors import ApiIdInvalid, UsernameNotOccupied
import time
from pyrogram.enums import MessageMediaType
import os
app = Client(os.getcwd() + "\\users\\main")
def toggle_connection():
if not app.is_connected:
try:
app.start()
print("Pyrogram bağlandı!")
_handle_chat_history()
connect_button.config(text="Disconnect")
except ApiIdInvalid:
print("API ID veya API Hash yanlış!")
except UsernameNotOccupied:
print("Kullanıcı adı mevcut değil!")
else:
_handle_disconnect()
def _handle_chat_history():
for _ in range(10):
app.send_message("me","a")
time.sleep(1)
def _handle_disconnect():
app.stop()
print("Pyrogram bağlantısı kesildi!")
connect_button.config(text="Connect")
root = tk.Tk()
root.title("Pyrogram Connector")
connect_button = tk.Button(root, text="Connect", command=toggle_connection)
connect_button.pack(pady=20)
def _on_closing():
app.stop()
root.destroy()
root.protocol("WM_DELETE_WINDOW", _on_closing)
root.mainloop()
Video;
https://drive.google.com/file/d/1ZxuWmQbOcAeFSBjvKkhuu13zGH0Qje0_/view?usp=sharing
When you press connect, let the code in the for loop run, which is the app send message.
and no matter where the loop or process is, when you say disconnect, it stops immediately, tkinter does not crash like in the video