首页 > 解决方案 > 为什么 patplotlib 发送错误在主线程之外启动 Matplotlib GUI 可能会失败

问题描述

我正在编写一个绘制函数图的机器人。为此,我使用了一个库 matplotlib 和一个远程机器人。起初,所有消息都正常处理。但是在输入绘制图形的数据后,调用了该函数,它给出了一个错误:

D:\pythonProject\__main__.py:78: UserWarning: Starting a Matplotlib GUI outside of the main thread will likely fail.

plt.figure()

这是我的代码:

import telebot
import matplotlib.pyplot as plt
from telebot import types

bot = telebot.TeleBot('1707466910:AAHPoEcNJkLJ3au0fj_BMlBwH3cnC4L3Fqs')
keyboard1 = telebot.types.ReplyKeyboardMarkup()
keyboard1.row('Hi', 'By')

@bot.message_handler(commands=['start'])
def start_message(message):
    stike = open('sticker.webp', 'rb')
    bot.send_sticker(message.chat.id, stike)
    bot.send_message(message.chat.id, 'Hi you wrote hi /start', reply_markup=keyboard1)

@bot.message_handler(content_types=['text'])
def get_text_messages(message):


    if message.text.lower().strip() == "hi":
        # Пишем приветствие

        bot.send_message(message.chat.id, "Witch func do you want?")

        keyboard = types.InlineKeyboardMarkup()

        key_lineykx = types.InlineKeyboardButton(text='Line | y = kx', callback_data='lineFunc')

        # И добавляем кнопку на экран
        keyboard.add(key_lineykx)

        bot.send_message(message.chat.id, text='Выбери функцию для построения', reply_markup=keyboard)
    elif message.text == "/help":
        bot.send_message(message.chat.id, "Напиши Привет")
    else:
        bot.send_message(message.chat.id, "Я тебя не понимаю. Напиши /help.")

    @bot.callback_query_handler(func=lambda call: True)
    def callback_worker(call):
        if call.data == "lineFunc":
            operator(message)

def operator(message):
    msg = bot.send_message(message.chat.id, 'Write data  >>  y  =  k  *  x. Like  -  (5  =  1  *  5)')
    #bot.send_message(message.from_user.id, 'Введи данные в формате  >>  y  =  k  *  x. Например  -  (5  =  1  *  5)')
    bot.register_next_step_handler(msg, graph) #After the user has answered, I call the function to which I pass the value of the user's message
    print(msg.text)

def graph(msg):
    mes = msg.text
    y = mes[0]
    k = mes[4]
    x = mes[8]
    print(mes)
    plt.figure()
    plt.axis([-10, 10, -10, 10])
    plt.grid(True)
    plt.plot((x, -x), (y, -y))
    plt.plot([x, -x], [y, -y], 'ro')
    plt.plot((0, 0), (10, -10), color='#ff7f0e')
    plt.plot((-10, 10), (0, 0), color='#ff7f0e')
    plt.ylabel('line func')
    plt.show()
    plt.savefig('your plot')
    return plt

bot.polling(none_stop=True, interval=0)

我在导入 matplotlib 时出错。我不能

用户警告:在主线程之外启动 Matplotlib GUI 可能会失败。plt.figure()

此函数中的错误:

    def graph(msg):
    mes = msg.text
    y = mes[0]
    k = mes[4]
    x = mes[8]
    print(mes)
    plt.figure()
    plt.axis([-10, 10, -10, 10])
    plt.grid(True)
    plt.plot((x, -x), (y, -y))
    plt.plot([x, -x], [y, -y], 'ro')
    plt.plot((0, 0), (10, -10), color='#ff7f0e')
    plt.plot((-10, 10), (0, 0), color='#ff7f0e')
    plt.ylabel('Line func')
    plt.show()
    plt.savefig('your plot')
    return plt

标签: python

解决方案


推荐阅读