Python 带有matplotlib的tkinter导航工具栏2Tk消失

Python 带有matplotlib的tkinter导航工具栏2Tk消失,python,python-3.x,matplotlib,tkinter,toolbar,Python,Python 3.x,Matplotlib,Tkinter,Toolbar,下午好 我已经试了两天来解决这个问题,不顾一切,我正在寻求你的帮助。我想在tkinter应用程序中显示一个绘图(使用matplotlib)(而不是在其他窗口中打开它),问题是,当我用鼠标或绘图线穿过时,按下任何工具栏按钮,绘图会在短时间内消失并再次出现,但是工具栏按钮消失了,直到我用鼠标再次穿过它们。所以为了让按钮显示在后面,我应该划掉它,划掉按钮 我注意到,如果我更改背景图像(仅更改标签中的颜色),消失的按钮将替换为该颜色,因此,当我穿过打印线时,标签中除打印外的所有内容都被背景覆盖 问候

下午好

我已经试了两天来解决这个问题,不顾一切,我正在寻求你的帮助。我想在tkinter应用程序中显示一个绘图(使用matplotlib)(而不是在其他窗口中打开它),问题是,当我用鼠标或绘图线穿过时,按下任何工具栏按钮,绘图会在短时间内消失并再次出现,但是工具栏按钮消失了,直到我用鼠标再次穿过它们。所以为了让按钮显示在后面,我应该划掉它,划掉按钮

我注意到,如果我更改背景图像(仅更改标签中的颜色),消失的按钮将替换为该颜色,因此,当我穿过打印线时,
标签
中除打印外的所有内容都被背景覆盖

问候,

瓦迪斯劳

代码是:

import tkinter as tk
from tkinter.ttk import *
from tkinter import *
import tkinter.font as tkFont
from tkinter import scrolledtext
import logging
from PIL import Image, ImageTk
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
# Implement the default Matplotlib key bindings.
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
import numpy as np

from matplotlib.figure import Figure
import numpy as np
import sys
if sys.version_info[0] < 3:
    import Tkinter as tk
else:
    import tkinter as tk
from matplotlib.backends import _backend_tk
from matplotlib.backends.backend_agg import FigureCanvasAgg



class WidgetLogger(logging.Handler):
    def __init__(self, widget):
        logging.Handler.__init__(self)
        self.setLevel(logging.INFO)
        self.widget = widget
        self.widget.config(state='disabled')

    def emit(self, record):
        self.widget.config(state='normal')
        # Append message (record) to the widget
        self.widget.insert(tk.END, self.format(record) + '\n')
        self.widget.see(tk.END)  # Scroll to the bottom
        self.widget.config(state='disabled')


class Product:
    def __init__(self, window):
        self.wind = window
        self.wind.geometry("1060x700") 
        self.wind.title('Artificial intelligence based pricer')
        self.wind.lift()
        #self.wind.resizable(width=False, height=False)

        background_color = '#526b70'
        background_color2 = '#e4e8eb'
        background_color3 = '#e4e8eb'
        background_color4 = '#c4cfd2'
        text_color = 'white'
        style = ttk.Style()
        style.configure("TProgressbar", background=background_color)

        img = Image.open('Files/Images/background_image.jpg')
        img = ImageTk.PhotoImage(img)
        background = tk.Label(self.wind, image=img, bd=0)
        background.grid(row = 0, column = 0, rowspan = 8, columnspan = 5)
        background.image = img

        img2 = Image.open('Files/Images/background_image2.jpg')
        img2 = ImageTk.PhotoImage(img2)
        background2 = tk.Label(self.wind, image=img2, bd=0)
        background2.grid(row = 9, column = 0, rowspan = 10, columnspan = 10)
        background2.image = img2


        ########## LEFT TOP SIDE ##############################

        fontStyleTitle = tkFont.Font(family="Times New Roman (Times)", size=12, weight='bold')
        fontStyleText = tkFont.Font(family="Arial", size=10, weight='bold')

        Label1 = Label(background, text = '    ')
        Label1.grid(row = 0, column = 0)
        Label1.configure(background=background_color)
        Label2 = Label(background, text = 'GLOBAL CONFIGURATION', fg=text_color, font=fontStyleTitle)
        Label2.grid(row = 1, column = 1, padx = 3, columnspan = 2, sticky = W)
        Label2.configure(background=background_color)

        Label3 = Label(background, text = 'Shop ID: ', fg=text_color, font=fontStyleText)
        Label3.grid(row = 2, column = 1, pady = 4, sticky = W)
        Label3.configure(background=background_color)
        self.shop = Entry(background)
        self.shop.focus()
        self.shop.grid(row = 2, column = 2)
        self.shop.configure(background=background_color3)

        Label4 = Label(background, text = 'Item ID: ', fg=text_color, font=fontStyleText)
        Label4.grid(row = 3, column = 1, sticky = W)
        Label4.configure(background=background_color)
        self.item = Entry(background)
        self.item.grid(row = 3, column = 2)
        self.item.configure(background=background_color3)


        Label5 = Label(background, text = '')
        Label5.grid(row = 4, column = 1)
        Label5.configure(background=background_color)
        Label6 = Label(background, text = 'ANN CONFIGURATION', font = fontStyleTitle, fg=text_color)
        Label6.grid(row = 5, column = 1, padx = 3, columnspan = 2, sticky = W)
        Label6.configure(background=background_color)

        Label7 = Label(background, text = 'Model: ', fg=text_color, font=fontStyleText)
        Label7.grid(row = 6, column = 1, pady = 4, sticky = W)
        Label7.configure(background=background_color)
        self.model = Entry(background)
        self.model.grid(row = 6, column = 2)
        self.model.configure(background=background_color3)

        Label8 = Label(background, text = 'Test set: ', fg=text_color, font=fontStyleText)
        Label8.grid(row = 7, column = 1, sticky = W)
        Label8.configure(background=background_color)
        self.test_set = Entry(background)
        self.test_set.grid(row = 7, column = 2)
        self.test_set.configure(background=background_color3)

        Button1 = tk.Button(background, bg=background_color2, text = 'Calculate performance')
        Button1.grid(row = 8, column = 1, padx = 50, pady = 10, columnspan = 2, sticky = W+E)
        #Button1.configure(background=background_color)


        ########## CENTER TOP SIDE ############################

        Label9 = Label(background, text = 'ANN MODEL PERFORMANCE', font=fontStyleTitle, fg=text_color)
        Label9.grid(row = 1, column = 3, padx = 40, sticky = W)
        Label9.configure(background=background_color)

        performace = Text(background, height=8, width=50)
        performace.grid(row = 2, column = 3, padx = 40, rowspan = 6)
        temporalText = '''MSE of standarized mean predictions: 700,5496
MSE of standarized ANN predictions: 700,5496

MSE of deseasonalized mean predictions: 700,5496
MSE of deseasonalized ANN predictions: 700,5496

MSE of seasonalized mean predictions: 700,5496
MSE of seasonalized ANN predictions: 700,5496'''
        performace.insert(tk.END, temporalText)
        performace.configure(background=background_color3)
        Widget_Logger = WidgetLogger(performace)

        progress = Progressbar(background, style='TProgressbar', orient = HORIZONTAL, length = 100, mode = 'determinate')
        progress.grid(row = 8, column = 3, padx = 40, sticky = W+E)
        #progress.configure(background=background_color)


        ########## RIGHT TOP SIDE #############################

        Label10 = Label(background, text = '        ')
        Label10.grid(row = 0, column = 6)
        Label10.configure(background=background_color)
        Label11 = Label(background, text = "PREDICTION'S CONFIGURATION", font=fontStyleTitle, fg=text_color)
        Label11.grid(row = 1, column = 4, padx = 3, columnspan = 2, sticky = W)
        Label11.configure(background=background_color)

        Label12 = Label(background, text = 'Precision: ', fg=text_color, font=fontStyleText)
        Label12.grid(row = 2, column = 4, pady = 4, sticky = W)
        Label12.configure(background=background_color)
        self.precision = Entry(background)
        self.precision.focus()
        self.precision.grid(row = 2, column = 5)
        self.precision.configure(background=background_color3)

        Label13 = Label(background, text = 'Max. price multiplicator: ', fg=text_color, font=fontStyleText)
        Label13.grid(row = 3, column = 4, sticky = W)
        Label13.configure(background=background_color)
        self.max_price_multiplicator = Entry(background)
        self.max_price_multiplicator.grid(row = 3, column = 5)
        self.max_price_multiplicator.configure(background=background_color3)

        Label14 = Label(background, text = 'Delta multiplicator: ', fg=text_color, font=fontStyleText)
        Label14.grid(row = 4, column = 4, pady = 4, sticky = W)
        Label14.configure(background=background_color)
        self.delta_multiplicator = Entry(background)
        self.delta_multiplicator.grid(row = 4, column = 5)
        self.delta_multiplicator.configure(background=background_color3)

        Label15 = Label(background, text = 'Item cost: ', fg=text_color, font=fontStyleText)
        Label15.grid(row = 5, column = 4, sticky = W)
        Label15.configure(background=background_color)
        self.item_cost = Entry(background)
        self.item_cost.grid(row = 5, column = 5)
        self.item_cost.configure(background=background_color3)
        Radiobutton(background, text = "absolute", variable = (background,"1"), value = "1", indicator = 0, background = "light blue", activebackground=background_color2, activeforeground='black').grid(row = 6, column = 4, sticky = E)
        Radiobutton(background, text = "mean price multiplicator", variable = (background,"1"), value = "2", indicator = 0, background = "light blue", activebackground=background_color2, activeforeground='black').grid(row = 6, column = 5, pady = 4, sticky = W)

        Label16 = Label(background, text = 'Fixed costs: ', fg=text_color, font=fontStyleText)
        Label16.grid(row = 7, column = 4, sticky = W)
        Label16.configure(background=background_color)
        self.fixed_costs = Entry(background)
        self.fixed_costs.grid(row = 7, column = 5)
        self.fixed_costs.configure(background=background_color3)

        Button2 = tk.Button(background, bg=background_color2, text = 'Calculate predictions')
        Button2.grid(row = 8, column = 4, padx = 80, pady = 10, columnspan = 2, sticky = W+E)

        Label17 = Label(background, text = '                                                                                                                                                                                                                                                               ')
        Label17.grid(row = 0, column = 6, sticky = W)
        Label17.configure(background=background_color)


        ########## LEFT BOTTOM SIDE ###########################

        Label18 = Label(background, text = '        ')
        Label18.grid(row = 9, column = 1)
        Label18.configure(background=background_color)

        fig = Figure(figsize=(6, 4), dpi=100)
        t = np.arange(0, 3, .01)
        fig.add_subplot(111).plot(t, 2 * np.sin(2 * np.pi * t))

        canvas = FigureCanvasTkAgg(fig, master=background2)
        canvas.draw()
        canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)

        toolbarFrame = Frame(master=background2)
        toolbarFrame.pack(side=TOP, fill=BOTH, expand=1)
        toolbar = NavigationToolbar2Tk(canvas, toolbarFrame)
        toolbar.update()

        canvas.mpl_connect("key_press_event", on_key_press)



def on_key_press(event):
    print("you pressed {}".format(event.key))
    key_press_handler(event, canvas, toolbar)


if __name__ == '__main__':
    window = Tk()
    application = Product(window)
    window.mainloop()```
将tkinter作为tk导入
从tkinter.ttk导入*
从tkinter进口*
将tkinter.font作为tkFont导入
从tkinter导入滚动文本
导入日志记录
从PIL导入图像,ImageTk
导入matplotlib
matplotlib.use(“TkAgg”)
从matplotlib.backends.backend_tkagg导入(图CAVASTKAGG,导航工具栏2TK)
#实现默认的Matplotlib键绑定。
从matplotlib.backend\u base导入键\u按\u处理程序
从matplotlib.figure导入图形
将numpy作为np导入
从matplotlib.figure导入图形
将numpy作为np导入
导入系统
如果系统版本信息[0]<3:
将Tkinter作为tk导入
其他:
将tkinter作为tk导入
从matplotlib.backends导入_backend_tk
从matplotlib.backends.backend_agg导入图Canvasagg
类WidgetLogger(logging.Handler):
定义初始化(自我,小部件):
logging.Handler.\uuuuu init\uuuuu(self)
self.setLevel(logging.INFO)
self.widget=widget
self.widget.config(state='disabled')
def排放(自我,记录):
self.widget.config(state='normal')
#将消息(记录)附加到小部件
self.widget.insert(tk.END,self.format(记录)+'\n')
self.widget.see(tk.END)#滚动至底部
self.widget.config(state='disabled')
类别产品:
定义初始化(自,窗口):
风=窗户
自身风几何(“1060x700”)
self.wind.title(‘基于人工智能的定价器’)
自我提升
#self.wind.reshable(宽度=False,高度=False)
背景颜色='#526b70'
背景颜色2='#e4e8eb'
背景颜色3='#e4e8eb'
背景颜色4='#c4cfd2'
文本颜色='白色'
style=ttk.style()
配置(“TProgressbar”,background=background\u color)
img=Image.open('Files/Images/background_Image.jpg'))
img=图像。照片图像(img)
背景=tk.Label(self.wind,image=img,bd=0)
background.grid(行=0,列=0,行span=8,列span=5)
background.image=img
img2=Image.open('Files/Images/background_image2.jpg'))
img2=ImageTk.PhotoImage(img2)
背景2=tk.标签(self.wind,image=img2,bd=0)
背景2.网格(行=9,列=0,行span=10,列span=10)
背景2.image=img2
##########左上侧##############################
fontStyleTitle=tkFont.Font(family=“Times New Roman(Times)”,size=12,weight='bold')
fontStyleText=tkFont.Font(family=“Arial”,size=10,weight='bold')
Label1=标签(背景,文本=“”)
标签1.网格(行=0,列=0)
标签1.配置(背景=背景颜色)
Label2=标签(背景,文本='全局配置',fg=文本\颜色,字体=fontStyleTitle)
标签2.网格(行=1,列=1,padx=3,列span=2,粘性=W)
标签2.配置(背景=背景颜色)
Label3=标签(背景,文本='Shop ID:',fg=文本\颜色,字体=fontStyleText)
标签3.网格(行=2,列=1,pady=4,粘性=W)
标签3.配置(背景=背景颜色)
self.shop=条目(背景)
self.shop.focus()
self.shop.grid(行=2,列=2)
self.shop.configure(background=background\u color3)
Label4=标签(背景,文本='Item ID:',fg=文本\颜色,字体=fontStyleText)
标签4.网格(行=3,列=1,粘性=W)
标签4.配置(背景=背景颜色)
self.item=条目(背景)
self.item.grid(行=3,列=2)
self.item.configure(background=background\u color3)
Label5=标签(背景,文本=“”)
标签5.网格(行=4,列=1)
标签5.配置(背景=背景颜色)
Label6=标签(背景,文本='ANN配置',字体=fontStyleTitle,fg=文本颜色)
标签6.网格(行=5,列=1,padx=3,列span=2,粘性=W)
标签6.配置(背景=背景颜色)
Label7=标签(背景,文本='Model:',fg=文本\颜色,font=fontStyleText)
标签7.网格(行=6,列=1,pady=4,粘性=W)
标签7.配置(背景=背景颜色)
self.model=条目(背景)
self.model.grid(行=6,列=2)
self.model.configure(background=background\u color3)
Label8=标签(背景,文本='测试集:',fg=文本\颜色,字体=fontStyleText)
标签8.网格(行=7,列=1,粘性=W)
标签8.配置(背景=背景颜色)
self.test_set=条目(背景)
自测试集网格(行=7,列=2)
self.test\u set.configure(后台=background\u color3)
Button1=tk.Button(背景,背景=background\u color2,文本=‘计算性能’)
按钮1.网格(行=8,列=1,padx=50,pady=10,列span=2,粘性=W+E)
#按钮1.配置(背景=背景颜色)
##########中上侧############################