我遵循了https://pythonprogramming.net/how-to-embed-matplotlib-graph-tkinter-gui/的代码 我只是用这种方式改变它,我有两个页面,每个页面都有一个画布。
我的问题是,当我初始化GUI时需要花费很多时间,看起来它在另一个页面冻结了几秒钟而不是起始页面“ChooseSensor”。
我会很高兴任何建议,我如何正确地初始化它。
这是我的代码:
import Tkinter as tk
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import matplotlib.animation as animation
from matplotlib import style
#define LARGE_FONT
LARGE_FONT = ("Verdana", 12)
style.use('ggplot')
#define figures
#figure for canvas sensor1
f1 = Figure(figsize=(5,5), dpi = 100)
a1 = f1.add_subplot(111)
#figure for canvas sensor2
f2 = Figure(figsize=(5,5), dpi = 100)
a2 = f2.add_subplot(111)
class Sensors(tk.Tk):
def __init__(self,*args, **kwargs ):
tk.Tk.__init__(self,*args,**kwargs)
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 (ChooseSensor, Sensor1, Sensor2):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(ChooseSensor)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class ChooseSensor(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text = "Sensor Auswahl", font= LARGE_FONT)
label.pack(pady=10, padx=10)
button1 = tk.Button(self,text="Sensor 1",
command=lambda:controller.show_frame(Sensor1))
button1.pack()
button2 = tk.Button(self,text="Sensor 2",
command=lambda:controller.show_frame(Sensor2))
button2.pack()
class Sensor1(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text = "Sensor 1", font= LARGE_FONT)
label.pack(pady=10, padx=10)
button1 = tk.Button(self,text="back to home",
command=lambda:controller.show_frame(ChooseSensor))
button1.pack()
canvas = FigureCanvasTkAgg(f1, self)
canvas.show()
canvas.get_tk_widget().pack(side=tk.TOP, fill = tk.BOTH, expand = True)
class Sensor2(tk.Frame):
def __init__(self,parent,controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text = "Sensor 2", font= LARGE_FONT)
label.pack(pady=10, padx=10)
button1 = tk.Button(self,text="back to home",
command=lambda:controller.show_frame(ChooseSensor))
button1.pack()
canvas = FigureCanvasTkAgg(f2, self)
canvas.show()
canvas.get_tk_widget().pack(side=tk.TOP, fill = tk.BOTH, expand = True)
app = Sensors()
app.mainloop()