cdkeycdma 发表于 2020-9-10 21:38:26

请问如何把图传到Tkinter创建的窗口中?

用先Tkinter创建一个窗口:
a=tk.Tk()#建立窗口
然后把b()生成的图表传到a窗口中,请问如何现实?


def b():
import random
import matplotlib.pyplot as plt
fig = plt.figure()#创建窗口facecolor='black' 指定背景颜色
ax = plt.gca()   #修正坐标
ax.set_facecolor('black')#指定网格背景颜色
x = []
y = []
i=0
plt.ion()
while True:
    x.append(i)    #转入x轴坐标
    y.append(random.randint(1,1000)) ##转y轴坐标
    plt.xlim()#x轴的数值显示范围0-5000
    plt.ylim()#y轴的数值显示范围0-1500
    plt.title('Scatter Graph')#图表名称
    plt.grid(c='r')   #网格线设置
    plt.plot(x,y,'y',linewidth=1)
    plt.pause(0.05) #暂停0.05秒
    i += 50       #间距


疾风怪盗 发表于 2020-9-10 21:42:25

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from tkinter import *

class testme:
    def __init__(self,frame1):
      self.frame1=frame1
      self.button=Button(self.frame1,text="DRAWME",command=self.plot)
      self.button1=Button(self.frame1,text="CLEARME",command=self.clearme)
      self.button.pack()
      self.button1.pack()

    def plot(self):
      f=Figure(figsize=(5,1))
      aplt=f.add_subplot(111)
      aplt.plot()
      self.wierdobject = FigureCanvasTkAgg(f, master=self.frame1)
      self.wierdobject.get_tk_widget().pack()
      self.wierdobject.draw()

    def clearme(self):
       self.wierdobject.get_tk_widget().pack_forget()

root=Tk()
aframe=Frame(root)
testme(aframe)
aframe.pack()#packs a frame which given testme packs frame 1 in testme
root.mainloop()
看下这段代码,应该是你想要的效果吧,照着改改
页: [1]
查看完整版本: 请问如何把图传到Tkinter创建的窗口中?