首页 > 解决方案 > 在 CentOS 上涉及 Tkinter/matplotlib “没有显示名称和没有 $DISPLAY 环境变量”的错误

问题描述

我在这里看到的大多数相关问题都没有解决我的问题。我正在编写一个使用 matplotlib 和 tkinter 制作 GUI 的程序。我正在运行 CentOS7。当我试图python36 testGraph.py在我的服务器上运行时,我得到了这个:

Traceback (most recent call last):
  File "testGraph.py", line 167, in <module>
    app = SeaofBTCapp()
  File "testGraph.py", line 57, in __init__
    tk.Tk.__init__(self, *args, **kwargs)
  File "/usr/lib64/python3.6/tkinter/__init__.py", line 2020, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

我不确定下一步该尝试什么。该文件如下。很多人说只是将 matplotlib 的导入语句移到文件顶部就解决了他们的问题。它不适合我的。

import matplotlib
matplotlib.use("TkAgg")
import tkinter as tk
from tkinter import ttk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure
import matplotlib.animation as animation
from matplotlib import style
style.use("ggplot")

# Function to get CPU frequency
def getfreq():

    with open('/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq', 'r') as f:

        for line in f.readlines():

            lineSplit = line.split(':')

            if len(lineSplit) == 2:

                lineSplit[0] = lineSplit[0].strip()
                lineSplit[1] = lineSplit[1].strip()

                if lineSplit[0] == "cpu MHz":
                    frequency = float(lineSplit[1])
                    return frequency


# Data fields needed for plotting live data
time = 0
xs = []
ys = []

f = Figure(figsize = (5, 5), dpi = 100)
a = f.add_subplot(111)

# Animation function to update plot
def animate(i):
    global time

    frequency = getfreq()

    xs.append(float(time))
    time = time + 1
    ys.append(frequency)
    a.clear()
    a.plot(xs, ys)


class SeaofBTCapp(tk.Tk):

    def __init__(self, *args, **kwargs):

        # Initialize Tkinter

        tk.Tk.__init__(self, *args, **kwargs)

        tk.Tk.wm_title(self, "CPU Frequency and Power Plot")

        container = tk.Frame(self)

        container.pack(side = "top", fill = "both", expand = True)

        container.grid_rowconfigure( 0, weight = 1)
        container.grid_columnconfigure(0, weight = 1)

        self.frames = {}

        for F in (StartPage, PageOne, PageTwo, PageThree):

            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row = 0, column = 0, sticky = "nsew")

        self.show_frame(StartPage)


    def show_frame(self, cont):

        frame = self.frames[cont]

        frame.tkraise()


class StartPage(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)

        label = tk.Label(self, text = "hello world")

        label.pack()

        button1 = ttk.Button(self, text = "Visit Page 1", command = lambda: controller.show_frame(PageOne))

        button1.pack()

        button2 = ttk.Button(self, text = "Visit Page 2", command = lambda: controller.show_frame(PageTwo))

        button2.pack()

        button3 = ttk.Button(self, text="Visit Graph Page", command=lambda: controller.show_frame(PageThree))

        button3.pack()

class PageOne(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)

        label = ttk.Label(self, text="Page 1!")

        label.pack()

        button1 = ttk.Button(self, text="Visit Home Page", command= lambda: controller.show_frame(StartPage))

        button1.pack()

class PageTwo(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)

        label = ttk.Label(self, text="Page 2!")

        label.pack()

        button1 = ttk.Button(self, text="Visit Home Page", command= lambda: controller.show_frame(StartPage))

        button1.pack()

# Frequency Graph Page
class PageThree(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)

        label = ttk.Label(self, text="Graph Page!")

        label.pack()

        button1 = ttk.Button(self, text="Visit Home Page", command= lambda: controller.show_frame(StartPage))

        button1.pack()

        canvas = FigureCanvasTkAgg(f, self)

        canvas.draw()

        canvas.get_tk_widget().pack(side = tk.TOP, fill = tk.BOTH, expand = True)

        toolbar = NavigationToolbar2Tk(canvas, self)

        toolbar.update()

        canvas._tkcanvas.pack(side = tk.TOP, fill = tk.BOTH, expand = True)



app = SeaofBTCapp()

ani = animation.FuncAnimation(f, animate, interval = 1000)

app.mainloop()

标签: pythonpython-3.xmatplotlibtkintercentos

解决方案


推荐阅读