我想创建一个Tkinter应用程序,用户可以在其中输入数据(只需 Select Excel文件),并可以通过使用Tkinter复选按钮查看图表.所以我决定使用Matplotlib,但它会在另一个新窗口中打开.如果这些图表在同一窗口中打开,那将是非常好的.(我也更希望该库将图表显示为Ploly Express)

推荐答案

Please upload a minimum reproducible example.
Without seeing that, you need to create a FigureCanvasTkAgg canvas to add your plots to if you haven't (docs).

import tkinter

from matplotlib.backends.backend_tkagg import (
    FigureCanvasTkAgg, NavigationToolbar2Tk)
# Implement the default Matplotlib key bindings.
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure

import numpy as np


root = tkinter.Tk()
root.wm_title("Embedding in Tk")

fig = Figure(figsize=(5, 4), dpi=100)
t = np.arange(0, 3, .01)
ax = fig.add_subplot()
line, = ax.plot(t, 2 * np.sin(2 * np.pi * t))
ax.set_xlabel("time [s]")
ax.set_ylabel("f(t)")

canvas = FigureCanvasTkAgg(fig, master=root)  # A tk.DrawingArea.
canvas.draw()

# pack_toolbar=False will make it easier to use a layout manager later on.
toolbar = NavigationToolbar2Tk(canvas, root, pack_toolbar=False)
toolbar.update()

canvas.mpl_connect(
    "key_press_event", lambda event: print(f"you pressed {event.key}"))
canvas.mpl_connect("key_press_event", key_press_handler)

button_quit = tkinter.Button(master=root, text="Quit", command=root.quit)


def update_frequency(new_val):
    # retrieve frequency
    f = float(new_val)

    # update data
    y = 2 * np.sin(2 * np.pi * f * t)
    line.set_data(t, y)

    # required to update canvas and attached toolbar!
    canvas.draw()


slider_update = tkinter.Scale(root, from_=1, to=5, orient=tkinter.HORIZONTAL,
                              command=update_frequency, label="Frequency [Hz]")

# Packing order is important. Widgets are processed sequentially and if there
# is no space left, because the window is too small, they are not displayed.
# The canvas is rather flexible in its size, so we pack it last which makes
# sure the UI controls are displayed as long as possible.
button_quit.pack(side=tkinter.BOTTOM)
slider_update.pack(side=tkinter.BOTTOM)
toolbar.pack(side=tkinter.BOTTOM, fill=tkinter.X)
canvas.get_tk_widget().pack(side=tkinter.TOP, fill=tkinter.BOTH, expand=1)

tkinter.mainloop()

Python相关问答推荐

在Python中管理多个OpenGVBO和VAO实例

从今天起的future 12个月内使用Python迭代

由于瓶颈,Python代码执行太慢-寻求性能优化

在函数内部使用eval(),将函数的输入作为字符串的一部分

韦尔福德方差与Numpy方差不同

使用groupby Pandas的一些操作

有没有一种方法可以从python的pussompy比较结果中提取文本?

计算天数

如何在Pyplot表中舍入值

在不同的帧B中判断帧A中的子字符串,每个帧的大小不同

Numpyro AR(1)均值切换模型抽样不一致性

Flask运行时无法在Python中打印到控制台

并行编程:同步进程

Polars map_使用多处理对UDF进行批处理

Beautifulsoup:遍历一个列表,从a到z,并解析数据,以便将其存储在pdf中.

使用polars. pivot()旋转一个框架(类似于R中的pivot_longer)

如何提高Pandas DataFrame中随机列 Select 和分配的效率?

Django.core.exceptions.SynchronousOnlyOperation您不能从异步上下文中调用它-请使用线程或SYNC_TO_ASYNC

pytest、xdist和共享生成的文件依赖项

时长超过24小时如何从Excel导入时长数据