Tkinter GUI阻止Discordpy并行运行

时间:2018-12-06 23:50:15

标签: python asynchronous tkinter discord.py

我正在尝试为服务器制作一个不和谐的管理GUI,但是我无法打开tkinter并同时运行不和谐。我对使用异步还相当陌生。我可以使用它来获取我最初遇到麻烦的printMes函数,但是现在我只需要并行加载即可。

import discord
import tkinter as tk
from tkinter import *
import asyncio


client = discord.Client()

@client.event
async def on_message(message):
    # we do not want the bot to reply to itself
    if message.author == client.user:
        return

    if message.content.startswith('!hello'):
        msg = 'Hello {0.author.mention}'.format(message)
        await client.send_message(message.channel, msg)


@client.event
async def on_ready():
    print('Logged in as')
    print(client.user.name)
    print(client.user.id)
    print('------')

async def asyncTest():
    print("test")
    await client.send_message(client.get_channel('520131551372247062'), "test")

#-- GUI --


async def run_tk(root, interval=0.05):
    try:
        while True:
            root.update()
            await asyncio.sleep(interval)
    except TclError as e:
        if "application has been destroyed" not in e.args[0]:
            raise

async def main():

    def printMes():
        return asyncio.ensure_future(asyncTest())

    root = tk.Tk()
    send = Button(root, text="Send", command=printMes)
    send.pack()
    label = tk.Label(root, text="Hello World")
    label.pack()

    await run_tk(root)


loop = asyncio.get_event_loop()
loop.run_until_complete(main())
loop.close()

client.run('')

0 个答案:

没有答案