在同一窗口内以单独的 tkinter 图形绘制多个图形

编程入门 行业动态 更新时间:2024-10-18 07:53:39
本文介绍了在同一窗口内以单独的 tkinter 图形绘制多个图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我正在尝试使用 tkinter 创建一个 GUI,该 GUI 将在出现提示时绘制多个图.现在我的代码可以正常工作,它将所有图形绘制到一个图形上.我想在同一个 GUI 中在不同的图形上绘制图形.

I'm trying to create a GUI using tkinter that will plot multiple plots when prompted. Right now my code works and it plots all of the graphs onto one figure. I would like to plot the graphs on separate figures, in the same GUI.

这是我在同一个 tkinter 图形中绘制所有图形的代码:

This is my code that plots all of the graphs in the same tkinter figure:

import myModule
from myModule import *
import SA_apis
from SA_apis import *
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg,   NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import tkinter as tk
from tkinter import ttk
from matplotlib import pyplot as plt

LARGE_FONT= ("Verdana", 12)
NORM_FONT= ("Verdana", 10)
SMALL_FONT= ("Verdana", 8)
plt.style.use("dark_background")
plt.style.use("seaborn-bright")

def popupmsg(msg):
    popup = tk.Tk()
    popup.wm_title("Menu")
    label = ttk.Label(popup, text=msg, font=NORM_FONT)
    label.pack(side="top", fill="x", pady=10)
    B1 = ttk.Button(popup, text="Okay", command = popup.destroy)
    B1.pack()
    popup.mainloop()


class TkGraph(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.wm_title(self, "TkGraph")

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        menubar = tk.Menu(container)
        filemenu = tk.Menu(menubar, tearoff=0)
        filemenu.add_command(label="New", command = lambda: popupmsg("This is not yet supported"))
        filemenu.add_command(label="Add", command = lambda: popupmsg("This is not yet supported"))
        filemenu.add_command(label="Exit", command=quit)
        menubar.add_cascade(label="File", menu=filemenu)
        tk.Tk.config(self, menu=menubar)

        parameter = tk.Menu(menubar, tearoff=0)
        parameter.add_command(label="Selection", command = lambda: popupmsg("This is not yet supported"))
        menubar.add_cascade(label="Parameter", menu=parameter)
        tk.Tk.config(self, menu=menubar)

        timemenu = tk.Menu(menubar, tearoff=0)
        timemenu.add_command(label="Selection", command = lambda: popupmsg("This is not yet supported"))
        menubar.add_cascade(label="Time", menu=parameter)
        tk.Tk.config(self, menu=menubar)

        helpmenu = tk.Menu(menubar, tearoff=0)
        helpmenu.add_command(label="Selection", command = lambda: popupmsg("This is not yet supported"))
         menubar.add_cascade(label="Help", menu=parameter)
        tk.Tk.config(self, menu=menubar)

         self.frames = {}
        for F in (StartPage, PageOne):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")
         self.show_frame(StartPage)
    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

class StartPage(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = tk.Label(self, text="Home", font=LARGE_FONT)
        label.pack(pady=10,padx=10)
        button1 = ttk.Button(self, text="Graph",
        command=lambda: controller.show_frame(PageOne))
        button1.pack()


 class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Graph", font=LARGE_FONT)
        label.pack(pady=10,padx=10)
        button1 = ttk.Button(self, text="Back to Home",
                        command=lambda: controller.show_frame(StartPage))
        button1.pack()

    """This is the code for the plotted graph, for each parameter you wish to plot you need to have a seperate x, y coordinate system.
    You got the amount of points for each parameter on SA_apis now you just paste that number in the code."""

    #Graph Code
    x = (g[0].time[:111673])
    y = (g[0].data.f[:111673])
    x2 = (h[0].time[:142642])
    y2 = (h[0].data.f[:142642])
    x3 = (j[0].time[:134970])
    y3 = j[0].data.f[:134970]
    x4 = (p[0].time[:147542])
    y4 = (p[0].data.f[:147542])
    plt.subplot()
    fig = plt.figure()

    """For each parameters x,y coordinates you will need a seperate  plt.plot()"""
    plt.plot(x, y)
    plt.plot(x2, y2)
    plt.plot(x3, y3)
    plt.plot(x4, y4)
    #descr = (g[0].descr)
    plt.title('(descr)', fontsize = 15)
    plt.xlabel('Time', fontsize=12)
    plt.ylabel('Data', fontsize=12)
    plt.grid(linestyle = 'dashed')   

    canvas = FigureCanvasTkAgg(fig, self)
    canvas.show()
    canvas.get_tk_widget().pack(side=tk.BOTTOM, fill=tk.BOTH, expand=True)

    toolbar = NavigationToolbar2TkAgg(canvas, self)
    toolbar.update()
    canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=True)

app = TkGraph()
app.mainloop()

我试着像这样分离图表:

I tried separating the graphs like this:

    x = (g[0].time[:111673])
    y = (g[0].data.f[:111673])
    plt.plot(x, y)
    plt.title('(descr)', fontsize = 15)
    plt.xlabel('Time', fontsize=12)
    plt.ylabel('Data', fontsize=12)
    plt.grid(linestyle = 'dashed')
    fig = plt.figure()

    x2 = (h[0].time[:142642])
    y2 = (h[0].data.f[:142642])
    plt.plot(x2, y2)
    plt.title('(descr)', fontsize = 15)
    plt.xlabel('Time', fontsize=12)
    plt.ylabel('Data', fontsize=12)
    plt.grid(linestyle = 'dashed')
    fig = plt.figure()

    x3 = (j[0].time[:134970])
    y3 = (j[0].data.f[:134970])
    plt.plot(x3, y3)
    plt.title('(descr)', fontsize = 15)
    plt.xlabel('Time', fontsize=12)
    plt.ylabel('Data', fontsize=12)
    plt.grid(linestyle = 'dashed')
    fig = plt.figure()

    x4 = (p[0].time[:147542])
    y4 = (p[0].data.f[:147542])
    plt.plot(x4, y4)
    plt.title('(descr)', fontsize = 15)
    plt.xlabel('Time', fontsize=12)
    plt.ylabel('Data', fontsize=12)
    plt.grid(linestyle = 'dashed')
    fig = plt.figure()

但这很乏味,并且不会导致在同一个 tkinter 窗口中绘制所有四个图形.这会导致空的 tkinter.

But this is tedious, and doesn't result in all four graphs being plotted in the same tkinter window. This causes and empty tkinter.

我的问题是:如何在同一个 tkinter 窗口中绘制多个图形,但使用不同的图形.理想情况下,它们会彼此相邻绘制,我不想创建另一个窗口.我需要同时查看所有图表以进行比较.

MY QUESTION IS THIS: How can I plot multiple graphs in the same tkinter window but in different figures. Ideally they would be plotted next to each other I don't want to create another window. I need the graphs to all be viewed at the same time for comparison.

推荐答案

您只需要使用 Frame 小部件.

You simply need to use the Frame widgets.

您可以在同一个窗口中拥有多个框架,因此您可以为每个图形创建所需的每个框架,并以这种方式处理它们.

You can have more than one frame in the same window so you create each frame you need for each graph and work with them that way.

看到您已经在使用框架,设置起来应该不难.

Seeing that you are already using frames it should not be hard for you to set up.

这篇关于在同一窗口内以单独的 tkinter 图形绘制多个图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 06:16:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1390689.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图形   多个   窗口   在同一   tkinter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!