栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Python中的Tkinter主窗口上放置图

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Python中的Tkinter主窗口上放置图

主要变化:

  1. 使用matplolib.figure而不是matplotlib.pyplot(并重命名所有相关函数)
  2. 添加一个self.window变量
  3. 到处都使用pack()(不能将网格混合并打包在一个容器中。)

结果:

__author__ = 'Dania'import matplotlibmatplotlib.use('TkAgg')import numpy as npfrom matplotlib.backends.backend_tkagg import FigureCanvasTkAggfrom matplotlib.figure import Figurefrom Tkinter import *class mclass:    def __init__(self,  window):        self.window = window        self.box = Entry(window)        self.button = Button (window, text="check", command=self.plot)        self.box.pack ()        self.button.pack()    def plot (self):        x=np.array ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])        v= np.array ([16,16.31925,17.6394,16.003,17.2861,17.3131,19.1259,18.9694,22.0003,22.81226])        p= np.array ([16.23697,     17.31653,     17.22094,     17.68631,     17.73641 ,    18.6368, 19.32125,     19.31756 ,    21.20247  ,   22.41444   ,  22.11718  ,   22.12453])        fig = Figure(figsize=(6,6))        a = fig.add_subplot(111)        a.scatter(v,x,color='red')        a.plot(p, range(2 +max(x)),color='blue')        a.invert_yaxis()        a.set_title ("Estimation Grid", fontsize=16)        a.set_ylabel("Y", fontsize=14)        a.set_xlabel("X", fontsize=14)        canvas = FigureCanvasTkAgg(fig, master=self.window)        canvas.get_tk_widget().pack()        canvas.draw()window= Tk()start= mclass (window)window.mainloop()


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/645472.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号