我对Tkinter并不陌生,我试图用两个不同的FigureCanvasTkAgg(一个在另一个之上)构建一个窗口,但我只是想不出为什么其中一个不会出现。另外,当我尝试调整窗口大小时,它只会冻结。谁能给我一些见识?谢谢!
我尝试创建三个不同的框架并将比例尺放在顶部,两个画布分别放在中间和底部。
from tkinter import *
from tkinter.filedialog import askopenfilename
from Decoder import *
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import matplotlib.animation as animation
MOTEDecoder = Decoder()
#original data graph
f = Figure(figsize=(50,50))
a = f.add_subplot(111)
g = Figure(figsize=(50,50))
b = g.add_subplot(111)
def animate(i):
if MOTEDecoder.inputamp != []:
pullDatay = MOTEDecoder.inputamp[-100:]
pullDatax = MOTEDecoder.inputtime[-100:]
a.clear()
a.plot(pullDatax,pullDatay)
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.init_Window()
def init_Window(self):
self.master.title("MOTE Decoder")
#Create Top Frame
top = Frame(root, borderwidth=2, relief="solid", bg="green")
top.pack(side="top", expand=True, fill="both")
#Create Slider for Threshold
threshold_scale = Scale(top, from_=0, to=1.0, \
resolution = 0.01, orient=HORIZONTAL, label = "Threshold", \
command = self.change_threshold)
threshold_scale.pack()
#Create Middle Frame
middle = Frame(root, borderwidth=2, relief="solid", bg="yellow")
middle.pack(side="bottom", expand=True, fill="both")
canvas_1 = FigureCanvasTkAgg(f, middle)
canvas_1.get_tk_widget().pack()
canvas_1.draw()
#Create Bottom Frame
bottom = Frame(root, borderwidth=2, relief="solid", bg="blue")
bottom.pack(side="bottom", expand=True, fill="both")
canvas_2 = FigureCanvasTkAgg(g, bottom)
canvas_2.get_tk_widget().pack()
canvas_2.draw()
#Create Menu
menu = Menu(self.master)
self.master.config(menu=menu)
#Create File Tab in Menu
file = Menu(menu)
file.add_command(label="Import Data", command=self.importdata)
file.add_command(label="Exit", command=self.client_exit)
menu.add_cascade(label="File", menu=file)
#Create Menu Tab in Menu
edit = Menu(menu)
edit.add_command(label="Undo")
menu.add_cascade(label="Edit", menu=edit)
#Set MOTEDecoder's threshold to value of slider
def change_threshold(self, val):
MOTEDecoder.threshold = val
print(MOTEDecoder.threshold)
#Set MOTEDecoder's inputdata to opened file
def importdata(self):
filename = askopenfilename()
MOTEDecoder.get_input(filename)
# print(filename)
def client_exit(self):
exit()
root = Tk()
root.geometry("800x600")
app = Window(root)
# ani = animation.FuncAnimation(f, animate, interval=1)
root.mainloop()