@LayzyTeh

Как подружить Canvas и Scrollbar в pythоn tknter?

создаются графики но не прокручиваются, как сделать так чтобы прокрутка работала?
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='#d0d8e0', bd=2)
        toolbar.place(x=0,y=0,relwidth=1.0,height=30)
        
        toolbarForCanvas=tk.Frame(bg='#d7d8e8', bd=2)
        toolbarForCanvas.place(x=0,y=31,width=600,height=570)
        
        scrollBarGraph=tk.Scrollbar(master=toolbarForCanvas,orient=tk.VERTICAL)
        scrollBarGraph.place(x=585,y=0,width=15,relheight=1.0)
        
        canvasWithFrame=tk.Canvas(master=toolbarForCanvas,scrollregion=(0,0,585,960))
        canvasWithFrame.config(yscrollcommand=scrollBarGraph.set)
        
        canvasWithFrame.place(x=0,y=0,width=585,height=570)
        scrollBarGraph.config(command=canvasWithFrame.yview)
        self.toolbarWithCanvas=tk.Frame(master=canvasWithFrame, bg='#eeeeee')
        canvasWithFrame.create_window(0, 0, window=self.toolbarWithCanvas, width=585, height=960, anchor=N+W)
        self.toolbarWithCanvas.place(x=0,y=0,relwidth=1.0,height=960)
      

        toolbarForLabel=tk.Frame(bg='#d7d0e0',bd=2, width=600,height=2000)
        toolbarForLabel.place(x=601,y=31,width=400,height=570)
        
        AM=tk.Label( toolbarForLabel,text='Amplitude:......')
        AM.place(x=0,y=0,width=150,height=30)
        
        FR=tk.Label( toolbarForLabel,text='Frecuency:......')
        FR.place(x=0,y=31,width=150,height=30)
        
        btnOpenDialog = tk.Button(toolbar, text='open', command=self.draw, bg='#d7d8e0', bd=0, compound=tk.TOP)
        btnOpenDialog.place(x=900,y=0,width=50,height=30)
        
        btnSaveDialog = tk.Button(toolbar, text='save', command=self.save, bg='#d7d8e0', bd=0, compound=tk.TOP)
        btnSaveDialog.place(x=0,y=0,width=40,height=30)
        
        self.txtToEntryName=tk.Entry(toolbar,width=20)
        self.txtToEntryName.place(x=45,y=0,width=100,height=30)
        
        self.lablTime=tk.Label(toolbar,text='time:......',padx=60)
        self.lablTime.place(x=150,y=0,width=100,height=30)
        
    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 w in q.readlines():
        {nazvania[i]}.append(float(w[:-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]

canvas{nazvania[i]} = FigureCanvasTkAgg(fig{nazvania[i]}, master=self.toolbarWithCanvas)  # A tk.DrawingArea.

canvas{nazvania[i]}.draw()
canvas{nazvania[i]}.get_tk_widget().place(in_= self.toolbarWithCanvas,x=0,y=(60*int(i)),relwidth=1.0,height=60)
""")
       self.lablTime.config(text='time:'+self.y)

if __name__ == "__main__":
    root = tk.Tk()
    app = Main(root)
    app.place()
    root.title("EEG ver.gragh.1")
    root.geometry("1000x600")
    root.resizable(False, False)
    root.mainloop()
  • Вопрос задан
  • 242 просмотра
Пригласить эксперта
Ваш ответ на вопрос

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

Похожие вопросы
23 нояб. 2024, в 01:31
1000 руб./за проект
23 нояб. 2024, в 00:16
2000 руб./за проект