2014-11-02 58 views
0

我試圖用ttk.Scrollbar滾動畫布,但我無法成功。當我縮小應用程序窗口時,窗口小部件會縮小,並且scrollbar不起作用。我找到了一些關於scrollbar的例子,但是沒有與python 3和/或我的代碼匹配。你能糾正我的代碼嗎?這裏是我的代碼:被添加到畫布packplacegrid不會滾動tkinter畫布滾動條不能用於類

from tkinter import * 
from tkinter import ttk 


class Uygulama(ttk.Frame): 
    def __init__(self): 

     ttk.Frame.__init__(self) 
     self.master.geometry('600x500') 
     self.pack(expand="true", 
        fill="both" 
       ) 



     self.Cerceveler() 
     self.GuiAraclari() 
     self.AnaMenu() 


    def AnaMenu(self,master=None): 

     self.anamenu=Menu() 
     self.master.config(menu=self.anamenu) 
     self.dosya=Menu(self.anamenu) 
     self.anamenu.add_cascade(label="File", 
           menu=self.dosya) 
     self.dosya.add_command(label="Aç") 
     self.dosya.add_command(label="Kaydet") 
     self.dosya.add_command(label="Farklı Kaydet") 
     self.dosya.add_command(label="Çıkış", 
           command=self.master.quit) 

    def Cerceveler(self): 


     self.root_canvas=Canvas(self, 
           relief="groove", 
           borderwidth=0, 
           scrollregion=(0, 0, 1200, 800)) 
     self.root_canvas.pack(side="left", 
           fill="both", 
           expand="true", 
          ) 

     self.ust_cerceve=ttk.Frame(self.root_canvas, 
            relief="groove", 
            borderwidth=2, 
            ) 

     self.ust_cerceve.pack(expand="false", 
           fill="x", 
           side="top") 

     self.orta_cerceve=ttk.Frame(self.root_canvas, 
            relief="flat", 
            borderwidth=0, 
            ) 
     self.orta_cerceve.pack(side="top", 
           expand="true", 
           fill="both") 

     self.alt_cerceve=ttk.Frame(self.root_canvas, 
            relief="groove", 
            borderwidth=2, 
            ) 
     self.alt_cerceve.pack(side="bottom", 
           expand="false", 
           fill=X) 

     self.sol_cerceve=ttk.Frame(self.orta_cerceve, 
            width=200, 
            relief="groove", 
            borderwidth=2, 
            ) 

     self.sol_cerceve.pack(side="left", 
           fill="y", 
           expand="false") 

     self.sag_cerceve=ttk.Frame(self.orta_cerceve, 
            relief="flat", 
            borderwidth=2) 

     self.sag_cerceve.pack(side="left", 
           fill= "both", 
           expand="true") 

     self.kaydirma=ttk.Scrollbar(self, 
            orient="vertical", 
            command=self.root_canvas.yview) 
     self.kaydirma.pack(side="right", 
          expand="false", 
          fill="y") 

     self.root_canvas.configure(yscrollcommand=self.kaydirma.set, 
            ) 

    def GuiAraclari(self): 

     self.bilgiekrani=ttk.Entry(self.alt_cerceve) 
     self.bilgiekrani.pack(fill=X) 

     self.deneme1=ttk.Button(self.ust_cerceve, 
           text="Deneme1") 
     self.deneme1.pack(side="left") 

     self.deneme2=ttk.Button(self.ust_cerceve, 
           text="Deneme2") 
     self.deneme2.pack(side="left") 

     self.deneme3=ttk.Button(self.ust_cerceve, 
           text="Deneme") 
     self.deneme3.pack(side="left") 

     self.deneme4=ttk.Button(self.sag_cerceve, 
           text="Deneme4") 
     self.deneme4.pack() 

     self.deneme5=ttk.Button(self.sag_cerceve, 
           text="Deneme5") 
     self.deneme5.pack() 

     self.deneme6=ttk.Button(self.sag_cerceve, 
           text="Deneme6") 
     self.deneme6.pack() 

     self.deneme7=ttk.Button(self.sag_cerceve, 
           text="Deneme7") 
     self.deneme7.pack() 









if __name__ == '__main__': 
    app=Uygulama() 
    app.update() 
    app.mainloop() 

回答

-1

最後,我找到了適用於scroll-able canvas的解決方案。我縮短了我的代碼,使其易於理解。這是我的新代碼。

from tkinter import * 
from tkinter import ttk 


class Uygulama(ttk.Frame): 
    def __init__(self): 

     ttk.Frame.__init__(self) 
     self.master.geometry('600x500') 
     self.pack(expand="true", 
        fill="both" 
       ) 



     self.Cerceveler() 
     self.GuiAraclari() 
     self.AnaMenu() 


    def AnaMenu(self,master=None): 

     self.anamenu=Menu() 
     self.master.config(menu=self.anamenu) 
     self.dosya=Menu(self.anamenu) 
     self.anamenu.add_cascade(label="File", 
           menu=self.dosya) 
     self.dosya.add_command(label="Aç") 
     self.dosya.add_command(label="Kaydet") 
     self.dosya.add_command(label="Farklı Kaydet") 
     self.dosya.add_command(label="Çıkış", 
           command=self.master.quit) 

    def Cerceveler(self): 


     self.kaydirma=ttk.Scrollbar(self, 
            orient="vertical", 
            ) 
     self.kaydirma.pack(side="right", 
          expand="false", 
          fill="y") 
     self.root_canvas=Canvas(self, 
           relief="groove", 
           borderwidth=0, 
           yscrollcommand=self.kaydirma.set) 
     self.root_canvas.pack(side="left", 
           fill="both", 
           expand="true", 
          ) 

     self.kaydirma.configure(command=self.root_canvas.yview, 
            ) 
     self.root_canvas.xview_moveto(0) 
     self.root_canvas.yview_moveto(0) 
     self.canv_fr = canv_fr=Frame(self.root_canvas) 
     canv_fr_id = self.root_canvas.create_window(0, 0, window=canv_fr, 
              anchor=NW) 
     def _configure_canv_fr(event): 
      size_w, size_h = (canv_fr.winfo_reqwidth(), canv_fr.winfo_reqheight()) 
      self.root_canvas.configure(scrollregion="0 0 {} {}".format(size_w, size_h)) 
      if canv_fr.winfo_reqwidth() != self.root_canvas.winfo_width(): 
       self.root_canvas.config(width=canv_fr.winfo_reqwidth()) 
     canv_fr.bind('<Configure>', _configure_canv_fr) 

     def _configure_canvas(event): 
      if canv_fr.winfo_reqwidth() != self.root_canvas.winfo_width(): 
       self.root_canvas.itemconfigure(canv_fr_id, width=self.root_canvas.winfo_width()) 
     self.root_canvas.bind('<Configure>', _configure_canvas) 




     self.alt_cerceve=ttk.Frame(self.root_canvas, 
            relief="groove", 
            borderwidth=2, 
            ) 
     self.alt_cerceve.pack(side="bottom", 
           expand="false", 
           fill=X) 


    def GuiAraclari(self): 

     self.bilgiekrani=ttk.Entry(self.alt_cerceve) 
     self.bilgiekrani.pack(fill=X) 


     self.deneme4=ttk.Button(self.canv_fr, 
           text="Deneme4") 
     self.deneme4.pack() 

     self.deneme5=ttk.Button(self.canv_fr, 
           text="Deneme5") 
     self.deneme5.pack() 

     self.deneme6=ttk.Button(self.canv_fr, 
           text="Deneme6") 
     self.deneme6.pack() 

     self.deneme7=ttk.Button(self.canv_fr, 
           text="Deneme7") 
     self.deneme7.pack() 

if __name__ == '__main__': 
    app=Uygulama() 
    app.update() 
    app.mainloop() 
1

小工具。要使用畫布滾動窗口小部件,必須使用create_window方法添加窗口小部件。

相關問題