我使用Glade、GTK和Julia构建了一个GUI。我想在GUI中添加一个绘图(在我的窗口/布局中),但无法找到将一个绘图作为小部件添加的方法。如何使用Plots.jl在图形用户界面中实现一个绘图?
添加以下内容不会使GUI布局中出现任何内容
x = 1:10; y = rand(10, 3) #
plot(x, y)发布于 2022-06-10 19:31:33
您可以让绘图将图像写入内存中的MIME映像,然后在画布中绘制更新,让Gtk加载该图像:
using Cairo
using Gtk
using Plots
const io = PipeBuffer()
histio(n) = show(io, MIME("image/png"), histogram(randn(n)))
function plotincanvas(h = 900, w = 800)
win = GtkWindow("Normal Histogram Widget", h, w) |> (vbox = GtkBox(:v) |> (slide = GtkScale(false, 1:500)))
Gtk.G_.value(slide, 250.0)
can = GtkCanvas()
push!(vbox, can)
set_gtk_property!(vbox, :expand, can, true)
@guarded draw(can) do widget
ctx = getgc(can)
n = Int(Gtk.GAccessor.value(slide))
histio(n)
img = read_from_png(io)
set_source_surface(ctx, img, 0, 0)
paint(ctx)
end
id = signal_connect((w) -> draw(can), slide, "value-changed")
showall(win)
show(can)
end
plotincanvas()我确信,如果您知道底层的数据格式,也有一种方法可以直接传输图像数据,而不需要show()。该方法避免了任何显式转换的需要。
https://stackoverflow.com/questions/72567498
复制相似问题