【问题标题】:coroutine was never awaited error in pythonpython中的协程从未等待过错误
【发布时间】:2020-11-09 12:18:55
【问题描述】:

我写了一个可以运行的代码,我想把我的程序变成一个 GUI 程序。 每当我运行程序时,我都会收到此错误:

C:\Users\Mohamad Bilal\AppData\Local\Programs\Python\Python39\lib\threading.py:888: RuntimeWarning: coroutine 'start_sending' was never awaited
  self._target(*self._args, **self._kwargs)

这是我的代码:

import openpyxl as xl
import os
from telethon import TelegramClient
import tkinter as tk
from tkinter import filedialog
import threading

root = tk.Tk()
canvas = tk.Canvas(root, height=500, width=500, bg='#263D42')
canvas.pack()
frame = tk.Frame(root, bg='white')
frame.place(relwidth=0.8, relheight=0.8, relx=0.1, rely=0.1)
load_excel = tk.Button(root, text='Load Excel', padx=10, pady=5, fg='white', bg='#263D42', command=load_excel)
product_count_txt = tk.Entry(root)
product_count_txt.insert(0, '1')
channel = tk.Entry(root, )
channel.insert(0, 'mohammadbelal')

product_count_txt.pack()
channel.pack()
load_excel.pack()
wb = xl.load_workbook('new.xlsx')

sheet = wb['Sheet1']
api_id = 
api_hash = ''
token = ''
phone = ''
client = TelegramClient('session', api_id, api_hash)


async def start_sending():
    await client.connect()
    if not client.is_user_authorized():
        await client.send_code_request(phone)
        await client.sign_in(phone, input('Enter the code: '))
    pp = await product_count_txt.get()
    product_count = int(pp)
    receiver = await channel.get()

    for i in range(product_count):
        s = i + 2
        d = await sheet.cell(s, 3)
        price = await sheet.cell(s, 27)
        seri = await sheet.cell(s, 43)
        sku = await d.value
        size = await sheet.cell(s, 7)
        message = f'''<b>Package (Seri) :</b> {seri.value} Pcs
    <b>Size:</b> {size.value[343:-350]}
    </br>
    <b>Price:</b> {price.value} $
    <b>Code:</b> {sku}
    
    <b>Whatsapp for more info:</b>
    ????+90 541 841 66 36
    
    <b><a href="https://www.facebook.com/istabuyGCC/">Facebook</a> |
<a href="https://www.instagram.com/istabuy/">Instagram</a> | istabuy.com®</b>
    '''
        image = []
        for folder, subs, files in os.walk('images\\' + sku):
            for filename in files:
                if filename.endswith(".jpg"):
                    await image.append(os.path.abspath(os.path.join(folder, filename)))
        await client.send_file(receiver, image)
        await client.send_message(receiver, message, parse_mode='html', link_preview=False)
        print(sku, 'Has been Sent!')


start_sending_btn = tk.Button(root, text='Start Sending', padx=10, pady=5, fg='white', bg='#263D42',
                              command=threading.Thread(target=start_sending).start())

start_sending_btn.pack()

root.mainloop()

【问题讨论】:

  • 请参阅this example,了解如何将 Telethon 与 tkinter 集成

标签: python-3.x tkinter telethon


【解决方案1】:

我不知道tkinter,但要运行我一直使用的异步函数,也见过使用asyncio。

import asyncio

async def function_name():
    # your code

loop = asyncio.get_event_loop()
loop.run_until_complete(function_name())

【讨论】:

  • 它没用 :( 仍然说协程从未被等待
  • @MhamadBelal 因为你可能使用command=threading.Thread(target=start_sending) 调用start_sending .. 尝试使用command=threading.Thread(target=await start_sending) 代替。顺便说一句,你应该只使用 asyncio 而不是线程
  • 当我使用 target=await start_sending 时,它显示“SyntaxError: 'await' outside function”
  • 那是因为你正在混合 asyncio 和线程,只使用 asyncio 就可以了
  • @TheKill-996 如果需要,您可以轻松地在其他线程中运行其他事件循环(不知道为什么有人需要它)。请查找类似问题的示例:stackoverflow.com/a/68652643/10311377
猜你喜欢
  • 1970-01-01
  • 2020-05-07
  • 2021-02-23
  • 2020-07-03
  • 2020-10-10
  • 2018-06-05
  • 2018-07-26
  • 2022-07-19
  • 1970-01-01
相关资源
最近更新 更多