在CentOS上涉及Tkinter / matplotlib的错误“无显示名称和$ DISPLAY环境变量”

时间:2019-01-23 05:02:49

标签: python python-3.x matplotlib tkinter centos

我在这里看到的大多数相关问题都无法解决我的问题。我正在编写一个使用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语句移动到文件顶部即可解决问题。这不是我的。

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

0 个答案:

没有答案