本文介绍了tkinter和matplotlib:直到在Linux下关闭程序,窗口才会显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个程序,该程序基本上可以通过按不同的按钮来绘制不同的数据.该程序可以在Windows下按预期工作,但是当我尝试将其移植到Linux(Red Hat v6)时,我遇到了一个奇怪的问题:要绘制的窗口直到关闭主程序后才出现.不管我尝试绘制到哪个图形(图1,2等),或者尝试键入plt.show()等,都会发生这种情况.

I've written a program that basically plots different data upon pressing different buttons. The program works as intended under Windows, but when I tried to port it to Linux (Red Hat v6) I'm getting a strange issue: the window that I want to plot does not appear until after I close the main program. This happens regardless of the figure (figure 1,2 etc.) I'm trying to plot to, or if I try to type plt.show() etc.

我编写的程序几乎有1000行代码,但是我创建了一个具有相同问题的缩写程序.它可以在Windows下运行,但是在Linux下,我必须关闭根窗口才能显示matplotlib窗口.

The program I've written is almost 1000 lines of code, but I created an abbreviated program that has the same problem. It works under Windows, but under Linux I have to close the root window for the matplotlib window to appear.

工作代码:

 import matplotlib.pyplot as plt
 from tkinter import *

 def click():
    x=['0','1','2']
    plt.plot(x,x)

 plotGUI=Tk()
 butt1=Button(plotGUI,text="Test", command=click).grid()
 plotGUI.mainloop()

推荐答案

我能够通过添加以下内容使其简单工作:

I was able to make it work simply by adding:

matplotlib.use('TkAgg')

这使该程序可以像Windows一样工作,而无需进行任何其他修改.我确实需要学习user3666197为将来的项目发布的概念.

This made the program work just as in Windows, without any other modifications necessary. I really need to learn the concepts posted by user3666197 for future projects though.

这篇关于tkinter和matplotlib:直到在Linux下关闭程序,窗口才会显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 06:12