在一个风中使用两个图形

2024-04-25 22:43:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Tkinter很陌生,我正在尝试用两个不同的图形来构建一个窗口Canvastkagg(一个在另一个上面),我只是不明白为什么其中一个不会出现。另外,当我试图调整窗口大小时,它只是冻结。有人能给我一些见解吗?谢谢您!你知道吗

我试着创造三个不同的框架,把比例放在顶部,两个画布分为中间和底部。你知道吗

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

Tags: fromimportselfmasteraddthresholddefcreate