tkinter canvas滚动条不适用于类

时间:2014-11-02 21:28:49

标签: canvas tkinter scrollbar ttk

我尝试使用ttk.Scrollbar滚动画布,但我无法成功。当我收缩我的应用程序窗口时,小部件正在缩小,scrollbar不起作用。我找到了一些关于scrollbar但未与python 3和/或我的代码匹配的示例。你能纠正我的代码吗?这是我的代码:

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()

2 个答案:

答案 0 :(得分:1)

使用packplacegrid添加到画布的小部件不会滚动。要使用画布滚动窗口小部件,必须使用create_window方法添加窗口小部件。

答案 1 :(得分:-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()