0

I am trying to create a gui. But I stack in here, because whenever I clicked to button2 canvas show up but first image disappear. I want to see both of them, one of them on the right and the other one on the left. I tried to resize but it doesn't effect anything. For explanation, I upload .txt file and and transform it to image. Then I plot it in frame left and then plot first column of the array in the left frame.

class GUI(Frame):

    def __init__(self):
        self.array = None
        Frame.__init__(self)
        self.frame_left = tk.Frame(root, width=540, height=640, bd="2")
        self.frame_left.grid(row=0, column=0)
        self.frame_right = tk.Frame(root, width=540, height=640, bd="2")
        self.frame_right.grid(row=0, column=1)
        self.button = tk.Button(self.frame_left, text="Resim Seç", command=self.open_image)
        self.button.pack(side='top')

    # def imgResize(img):                               ### burasi image resize
    #     basewidth = 500
    #     wpercent = (basewidth / float(img.size[0]))
    #     hsize = int((float(img.size[1]) * float(wpercent)))
    #     img = img.resize((basewidth, hsize), Image.ANTIALIAS)
    #     return img

    def a_scan(self):

        if hasattr(self, 'canvas'):
            self.canvas.get_tk_widget().destroy()
        self.image = Image.open("output.png")
        self.im_np = np.array(self.image)
        self.img = np.array(self.im_np)
        self.imga = (self.img - np.min(self.img)) / (np.ptp(self.img).astype(int))
        self.a = self.imga[:, 0] ##ilk sutundaki degerler'''
        self.b=self.a.shape[0]   ##ilk sutunda kac tane deger oldugu'''
        self.b=np.arange(0,self.b)
        self.a=np.array(self.a)
        self.array = np.stack((self.a, self.b), axis=-1)
        fig, ax = plt.subplots()
        ax.plot(self.b, self.a)
        ax.set(xlabel='Index', ylabel='Intensity', title='A-Scan')
        ax.grid()
        self.canvas = FigureCanvasTkAgg(fig, self.frame_right)
        self.canvas.draw()
        self.canvas.get_tk_widget().pack(side=RIGHT, fill=Y)
    def open_image(self):

        filepath = filedialog.askopenfilename()

        if hasattr(self, 'canvas'):
            self.canvas.get_tk_widget().destroy()
        if hasattr(self, 'label'):
            self.label.destroy()
            self.button2.destroy()

        if filepath.endswith(".txt"):
            self.data = np.loadtxt(filepath)
            self.data = np.array(self.data)
            self.data = (255 * (self.data - np.min(self.data))) / (np.ptp(self.data).astype(int))
            self.data = np.transpose(self.data, axes=None)
            self.image = Image.fromarray(np.uint8(self.data))
            self.image.save("output.png")
            self.image = ImageTk.PhotoImage(self.image)
            self.label = Label(self.frame_left, image=self.image)
            self.label.pack(side=LEFT)
            self.button2 = tk.Button(self.frame_left, text="A scan goster", command=self.a_scan)
            self.button2.pack(side=RIGHT)

        else:
            image = Label(root, image=filepath)
            image.pack()


root = tk.Tk()
root.title('YNR')
root.geometry('1080x640')
app = GUI()
app.mainloop()

here is my code. Thanks.

  • `self.image` can store only 1 variable. And when that variable gets replaced by `self.image = Image.open(...)`, the last image gets destroyed. Look at [this](https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function) for more info – TheLizzard Jan 19 '23 at 12:34

0 Answers0