簡體   English   中英

從 tkinter window 調用時使用 plt.close('all') 后 plt.show() 凍結

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

該代碼應該等待用戶點擊,然后它將關閉 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.

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用一個按鈕。 當您單擊該按鈕時,plot 將再次顯示,但這次當您單擊 plot 內部時,plot 將關閉,但不會顯示任何文本。 關閉 tkinter window 后,文本將顯示“hello from: tkinter”。

這不是我想要的,我希望像第一個示例一樣單擊 plot 后立即顯示文本。 有人可以提供任何指導嗎?

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

啊,看起來我通過使用 matplotlib 的 Qt5Agg 后端修復了它。 我嘗試了 WxAgg 但對我不起作用。

import matplotlib
matplotlib.use('QT5Agg')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM