Как я могу построить график matplotlib, который был встроен в tkinter по значениям gettin от пользователя? - PullRequest
0 голосов
/ 12 марта 2020

Я пытаюсь построить график imshow, используя matplotlib в Gui. Однако я не могу получить входные данные от пользователя для ввода координат. Я хотел бы получить координаты от пользователей и создать изображение imshow после нажатия кнопки. Я думаю, что проблема в том, что он пытается построить график, не имея всех входов. Однако я не знаю, как решить эту проблему.

У меня возникли некоторые проблемы с этой программой.


import matplotlib
from matplotlib import pyplot as plt
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure

import numpy as np
import numpy.ma as ma
import cv2
from mpl_toolkits.mplot3d import axes3d
from os import listdir
from os.path import isfile, join
import ellipse as el

import tkinter as tk
from tkinter import ttk


LARGE_FONT= ("Verdana", 12)


class SeaofBTCapp(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)

        tk.Tk.wm_title(self, "Sea of BTC Client")

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

        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 = ttk.Label(self, text="Start Page", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        button = ttk.Button(self, text="Visit Page 1",
                            command=lambda: controller.show_frame(PageOne))
        button.pack()


class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = ttk.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()


        labelText=tk.StringVar()
        labelText.set("Enter x")
        labelx=ttk.Label(self, textvariable=labelText)
        labelx.pack(side=tk.TOP)

        directory=tk.StringVar(None)
        x_coordinate=ttk.Entry(self,textvariable=directory)
        x_coordinate.pack(side=tk.TOP)
        #a = f.add_subplot(111)
        #a.plot([1,2,3,4,5,6,7,8],[5,6,1,3,8,9,3,5])


        mypath='C:\\Users\\mehmet\\Desktop\\a1'
        onlyfiles = [ f for f in listdir(mypath) if isfile(join(mypath,f)) ]
        images = np.empty(len(onlyfiles), dtype=object)
        for n in range(0, len(onlyfiles)):
          images[n] = cv2.imread( join(mypath,onlyfiles[n]),cv2.IMREAD_GRAYSCALE)

        image = np.stack([images[i] for i in range(13,299)])

        arr_size = (265,490,286)
        sphere_center = (int(directory.get()),238,76)
        a=11
        b=10
        c=12
        sphere = el.create_bin_sphere(arr_size,sphere_center, a,b,c)
        sphere1=255*sphere.astype(np.uint8)
        sphere2=np.swapaxes(sphere1,0,2)


        dst = cv2.bitwise_or(sphere2, image)

        img_p=dst[:,:,120]

        f = Figure(figsize=(5,5), dpi=100)
        a = f.add_subplot(111)
        a.imshow(img_p,cmap='gray')


        canvas = FigureCanvasTkAgg(f, self)
        canvas.draw()
        canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)



app = SeaofBTCapp()
app.mainloop()```
...