@LayzyTeh

Изменение размера Canvas. Как исправить?

рисую графики и хочу сделать их прокручиваемыми, но последние графики сжимаются
import tkinter as tk
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import numpy as np
class Main(tk.Frame):
    def __init__(self, root):
        super().__init__(root)
        self.init_main()
        
        
    def init_main(self):
        toolbar = tk.Frame(bg='#d7d8e0', bd=2)
        toolbar.pack(side=tk.TOP, fill=tk.X)
        
        
        toolbarForCanvas=tk.Frame(bg='#d7d8e0', bd=2)
        toolbarForCanvas.pack(side=tk.LEFT, fill=tk.Y)
        scrollBarGraph=tk.Scrollbar(master=toolbarForCanvas,orient=tk.VERTICAL)
        scrollBarGraph.pack(fill=tk.Y, side=tk.RIGHT)
        canvasWithFrame=tk.Canvas(master=toolbarForCanvas,yscrollcommand=scrollBarGraph.set)
        canvasWithFrame.pack()
        self.toolbarWithCanvas=tk.Frame(master=canvasWithFrame, width=600,height=2000)
        self.toolbarWithCanvas.pack(side=tk.LEFT, fill=tk.BOTH,expand=False)
        scrollBarGraph.config(command=canvasWithFrame.yview)
        
        
        btnOpenDialog = tk.Button(toolbar, text='open', command=self.draw, bg='#d7d8e0', bd=0, compound=tk.TOP)
        btnOpenDialog.pack(side=tk.RIGHT)
        
        
        btnSaveDialog = tk.Button(toolbar, text='save', command=self.save, bg='#d7d8e0', bd=0, compound=tk.TOP)
        btnSaveDialog.pack(side=tk.LEFT)
        
        
        self.txtToEntryName=tk.Entry(toolbar,width=20)
        self.txtToEntryName.pack(side=tk.LEFT)
        
        
        self.lablTime=tk.Label(toolbar,text='time:......',padx=60)
        self.lablTime.pack(side=tk.LEFT)
        
        
    def save(self):
        from PIL import Image
        s=self.txtToEntryName.get()
        pic=[]
        for i in range(self.qua):
            plt.savefig(str(i)+'.jpg',dpi=300)
            pic.append ('img'+str(i))
            pic[i]=Image.open(str(i)+'.jpg')
        razmer=pic[0].size
        img = Image.new('RGB',(razmer[0],razmer[1]*self.qua))
        for i in range(self.qua):
            img.paste(pic[i], (0,razmer[1]*i))
        img.save(s)
        
        
    def draw(self):
       nazvania=['OH','IM','DMI','TMD','QMT','PMQ','SMP','AMS','EWI','NWD','TWT','TWQ','DWP','TWS','FR','PL']
       plot=['0H',"1M",'2C1','3C2','4C3','5C4','6C5','7C6','8w1','9w2','10w3','11w4','12w5','13w6','14R','15L']
       self.qua=len(plot)
       for i in range(16):
           exec(f"""
{nazvania[i]}=[]
with open("/home/biotech/Documents/EEG/gur1/sinh{plot[i]}.txt",'r') as q:
    for i in q.readlines():
        {nazvania[i]}.append(float(i[:-2]))
t=1/2000
fig{nazvania[i]}=plt.figure()  
plt.axhline(-0.1,0,2*t*len({nazvania[i]}),color="black")
Y=np.array({nazvania[i]})
X=np.linspace(0,t*len(Y),len(Y))
plt.scatter(X,Y,s=1,color="black")
self.y=str(t*len(Y))[:5]

fig{nazvania[i]}.set_size_inches(t*len(Y)*3,0.5)
canvas{nazvania[i]} = FigureCanvasTkAgg(fig{nazvania[i]}, master=self.toolbarWithCanvas)  # A tk.DrawingArea.

#canvas{nazvania[i]}.draw()
canvas{nazvania[i]}.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
""")
       self.lablTime.config(text='time:'+self.y)
       self.toolbarWithCanvas.config(width=600,height=2000)

if __name__ == "__main__":
    root = tk.Tk()
    app = Main(root)
    app.pack()
    root.title("EEG ver.gragh")
    root.geometry("1000x600")
    root.resizable(False, False)
    root.mainloop()

*внимание на нижний график размер окна не надо менять
5db01702dd29a866256419.png
  • Вопрос задан
  • 295 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы