简体   繁体   English

从 tkinter window 调用时使用 plt.close('all') 后 plt.show() 冻结

[英]plt.show() freezes after using plt.close('all') when calling from tkinter window

The code is supposed to wait for user click, then it will close the plot and continue.该代码应该等待用户点击,然后它将关闭 plot 并继续。 This works great when not done from the tkinter loop, but if the plot function is invoked from tkinter, the program freezes until the tkinter window is closed. This works great when not done from the tkinter loop, but if the plot function is invoked from tkinter, the program freezes until the tkinter window is closed.

So if you run this code, you will get a plot, then when you click on the plot, the plot will close, and program will display "Hello from: main program" which is what I want, but then, the tkinter window opens with a button. So if you run this code, you will get a plot, then when you click on the plot, the plot will close, and program will display "Hello from: main program" which is what I want, but then, the tkinter window opens用一个按钮。 When you click the button, the plot will show again, but this time when you click inside the plot, the plot will close, but no text will be shown.当您单击该按钮时,plot 将再次显示,但这次当您单击 plot 内部时,plot 将关闭,但不会显示任何文本。 Once you close the tkinter window, the text will display "hello from: tkinter."关闭 tkinter window 后,文本将显示“hello from: tkinter”。

This is not what I want, I want the text to display as soon as I click on the plot like on the first example.这不是我想要的,我希望像第一个示例一样单击 plot 后立即显示文本。 Can someone provide any guidance?有人可以提供任何指导吗?

import tkinter as tk
from tkinter import *
from matplotlib import pyplot as plt

class patito():
    def __init__(self, location):
        self.x = [0,1,2,3,4,5,6]
        self.y = [1,2,1,2,1,2,1]

        self.fig, self.ax = plt.subplots()
        self.plot_this(location)
        

    def select_peak(self,event):
        plt.close("all")# close plot and continue to print("hello from:")

    def plot_this(self, location):
        plt.plot(self.x,self.y)
        self.fig.canvas.mpl_connect('button_press_event', self.select_peak)#call select_peak when clicking inside plot
        plt.show()
        print("hello from: " + location)

def call_patito(location):
    x = patito(location)

def tkinter_call_patito():
    x = patito("tkinter")
########################### Program starts Here ##############
    
call_patito("main program")
             
root = tk.Tk()
main_frame = Frame(root)
title_frame = Frame(root)

source_button =      Button(title_frame,justify=LEFT, text='Create patito object', command = tkinter_call_patito).grid(row=1, column = 1)
title_frame.pack()
main_frame.pack()
root.mainloop()

Ah, looks like I got it fixed by using the Qt5Agg backend for matplotlib.啊,看起来我通过使用 matplotlib 的 Qt5Agg 后端修复了它。 I tried WxAgg but didn't work for me.我尝试了 WxAgg 但对我不起作用。

import matplotlib
matplotlib.use('QT5Agg')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM