繁体   English   中英

如何在新窗口中显示图形? 图形用户界面

[英]How can I display a graph in a new window? Tkinter GUI

这是我的图形函数的代码和打开新窗口的示例函数。 我找不到如何在新窗口上打印我的图表。 我尝试了替代方法,但只发现自己能够分别提交这两个命令。 知道如何在新窗口上打印我的图表吗?

def plot(): 
    # the figure that will contain the plot 
    fig = Figure(figsize = (5, 5), dpi = 100) 
    df = pd.DataFrame(np.random.randint(0,1000,size=(20, 2)), columns= 
    ('Storage Units (kWh)', 'Power Plants (kW)'))
    y = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
    fig, axes = plt.subplots(ncols=2, sharey=True)
    plt.suptitle('Optimal Actions')
    plt.yticks(np.arange(min(y), max(y)+1, 1.0))
    axes[0].invert_yaxis
    axes[0].xaxis.set_label_position('top') 
    axes[1].xaxis.set_label_position('top')
    axes[0].yaxis.set_label_coords(1.15,1.02)
    axes[0].barh(y, df['Storage Units (kWh)'], align='center', color='red')
    axes[1].barh(y, df['Power Plants (kW)'], align='center', color='blue')
    axes[0].invert_xaxis()
    axes[0].yaxis.tick_right()
    axes[0].set_xlabel('Storage Units (kWh)')
    axes[1].set_xlabel('Power Plants (kW)')
    axes[0].set_ylabel('Year')
    # creating the Tkinter canvas 
    # containing the Matplotlib figure 
    canvas = FigureCanvasTkAgg(fig, master = window)   
    canvas.draw() 
    # placing the canvas on the Tkinter window 
    canvas.get_tk_widget().pack() 
def create_window():
    newwindow = tk.Toplevel(window)
pbutton = tk.Button(frame4,
                    text = "Plot",
                    command = lambda:[create_window(), plot()]).pack(side = 
"right")

您可以将newwindow传递给plot()函数:

def plot(parent):
    ...
    canvas = FigureCanvasTkAgg(fig, master=parent)
    ...

def create_window():
    newwindow = tk.Toplevel(window)
    return newwindow

...
pbutton = tk.Button(frame4, text='Plot', lambda: plot(create_window()))
pbutton.pack(side='right')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM