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

2024-04-29 00:44:42 发布

您现在位置:Python中文网/ 问答频道 /正文

下午好

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

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

How the buttons look liike after the plot is crossed

The line I should cross for the buttons to disappear

问候,

瓦迪斯劳

代码是:

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()```

Tags: textimportselfconfigurecolumnlabeltkgrid