【发布时间】:2016-06-06 12:27:41
【问题描述】:
我在 Tkinter 中使用 matplotlib 时遇到问题。
这是计划:
1.使用 Tkinter 定义 Button 和 Frame (Frame_0)。
2. 点击Button,使用matplotlib绘制正弦函数,如Frame_0所示。
但我不知道该怎么做。
我只是一个初学者。但我只是认为最简单的方法可能是使用get_tk_widget()“选择”小部件,这可能吗?还是有其他命令可以做到这一点?
希望你能理解我糟糕的英语和我的初学者问题。这是代码。 谢谢大家。
# -*- coding: utf-8 -*-
from Tkinter import *
import matplotlib
matplotlib.use('TkAgg')
from numpy import arange, sin, pi
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
def command_0():
f = Figure(figsize=(5, 3), dpi=100)
a = f.add_subplot(111)
t = arange(0.0, 3.0, 0.01)
s = sin(2 * pi * t)
a.plot(t, s)
dataPlot = FigureCanvasTkAgg(f, master=root)
dataPlot.show()
dataPlot.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
root.mainloop()
def Main():
global root
root = Tk()
root.title("Program")
root['background']='gray'
w = root.winfo_screenwidth()
h = root.winfo_screenheight()
root.geometry("%dx%d" %(w, h))
root.state("zoomed")
global Frame_0
Frame_0 = Frame(root)
Frame_0.place(height=600, width=800, x=10, y=10)
Frame_1 = Frame(root)
Frame_1.place(width=120, height=80, x=1000, y=20)
Button_1 = Button(Frame_1, text = "Click", width = 120, height=80, bg='green', command = command_0)
Button_1.place(x=1000, y=20)
Button_1.grid(row=0, column=0)
Button_1.pack()
root.mainloop()
return
Main()
【问题讨论】:
标签: python matplotlib tkinter