tkinter 和 matplotlib:在 Linux 下程序关闭之前不显示窗口 [英] tkinter and matplotlib: windows not showing until program closes under Linux

查看:26
本文介绍了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()

推荐答案

我只需添加:

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 下程序关闭之前不显示窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆