如何在画布中使用Tkinter滚动条

时间:2020-08-12 09:15:51

标签: python tkinter canvas

我们正在尝试使用tkinter设计表单。我们停留在无法链接表单滚动条的位置。

def init_Widget(self):
    #Second Info Frame
    self.CanvasFrame =Canvas(self, height=200,width=550,bg="red")
    self.scrollbar=Scrollbar(self.CanvasFrame)[enter image description here][1]
    self.scrollbar.pack(side=RIGHT,fill=Y)
    self.CanvasFrame.configure(yscrollcommand=self.scrollbar.set)
    self.OrderFrame = LabelFrame(self.CanvasFrame,text="Billing Info",padx=5,pady=5 ,highlightthickness=2 )
    self.Header1lbl = Label(self.OrderFrame,text="Item", foreground="white",font=("Courier", 10,"bold") , bg="#008080",relief="raised", borderwidth = 2, width=40,padx=1,pady=1)
    self.Header1lbl.grid(row=0,column=0)
    self.Header2lbl = Label(self.OrderFrame,text="Qty", foreground="white",font=("Courier", 10,"bold") , bg="#008080",relief="raised", borderwidth = 2,width= 10,padx=1,pady=1)
    self.Header2lbl.grid(row=0,column=1)
    self.Header3lbl = Label(self.OrderFrame,text="Rate", foreground="white",font=("Courier", 10,"bold") , bg="#008080",relief="raised", borderwidth = 2,width= 10,padx=1,pady=1)
    self.Header3lbl.grid(row=0,column=2)
    for i in range(1,8):
        self.ItemInput1 = Entry(self.OrderFrame,width=54)
        self.ItemInput1.grid(row=i,column=0)
        self.Qty1 = Entry(self.OrderFrame,width=14)
        self.Qty1.grid(row=i,column=1)
        self.Rate1 = Entry(self.OrderFrame,width=14)
        self.Rate1.grid(row=i,column=2) 
    self.CanvasFrame.configure(scrollregion=self.CanvasFrame.bbox("all"))
    self.OrderFrame.pack(padx=3,pady=3)
    self.scrollbar.config(command=self.CanvasFrame.yview)
    self.CanvasFrame.pack()
    self.CanvasFrame.pack_propagate(False)

预先感谢

1 个答案:

答案 0 :(得分:1)

首先,您不应该将self.scrollbar放在self.CanvasFrame内,而应该放在self中。

self.scrollbar=Scrollbar(self)

第二,您不应使用pack()self.OrderFrame放入self.CanvasFrame,而应使用self.CanvasFrame.create_window()

最后,在将所有小部件插入scrollregion并将self.OrderFrame添加到self.OrderFrame之后,您应该更新self.CanvasFrame

    #self.OrderFrame.pack(padx=3,pady=3)
    self.CanvasFrame.create_window(0, 0, window=self.OrderFrame, anchor='nw')
    self.CanvasFrame.update()
    self.CanvasFrame.configure(scrollregion=self.CanvasFrame.bbox("all"))

更新:您可以使用框架固定画布和滚动条:

def init_Widget(self):
    # frame for the canvas and scrollbar
    self.ScrollFrame = Frame(self)
    self.ScrollFrame.pack()

    self.CanvasFrame = Canvas(self.ScrollFrame, height=200,width=550,bg="red")
    self.CanvasFrame.pack(side=LEFT,fill=BOTH)

    self.scrollbar = Scrollbar(self.ScrollFrame,command=self.CanvasFrame.yview)
    self.scrollbar.pack(side=RIGHT,fill=Y)

    self.CanvasFrame.configure(yscrollcommand=self.scrollbar.set)

    self.OrderFrame=LabelFrame(self.CanvasFrame,text="Billing Info",padx=5,pady=5 ,highlightthickness=2 )
    self.CanvasFrame.create_window(0, 0, window=self.OrderFrame, anchor='nw')

    self.Header1lbl = Label(self.OrderFrame,text="Item", foreground="white",font=("Courier", 10,"bold") , bg="#008080",relief="raised", borderwidth = 2, width=40,padx=1,pady=1)
    self.Header1lbl.grid(row=0,column=0)
    self.Header2lbl = Label(self.OrderFrame,text="Qty", foreground="white",font=("Courier", 10,"bold") , bg="#008080",relief="raised", borderwidth = 2,width= 10,padx=1,pady=1)
    self.Header2lbl.grid(row=0,column=1)
    self.Header3lbl = Label(self.OrderFrame,text="Rate", foreground="white",font=("Courier", 10,"bold") , bg="#008080",relief="raised", borderwidth = 2,width= 10,padx=1,pady=1)
    self.Header3lbl.grid(row=0,column=2)

    for i in range(1,18):
        self.ItemInput1 = Entry(self.OrderFrame,width=54)
        self.ItemInput1.grid(row=i,column=0)
        self.Qty1 = Entry(self.OrderFrame,width=14)
        self.Qty1.grid(row=i,column=1)
        self.Rate1 = Entry(self.OrderFrame,width=14)
        self.Rate1.grid(row=i,column=2) 

    self.CanvasFrame.update()
    self.CanvasFrame.configure(scrollregion=self.CanvasFrame.bbox("all"))