如何将滚动条仅附加到画布?

时间:2015-06-25 16:59:24

标签: python tkinter scrollbar

我正在尝试构建一个仪表板。

我想保留滚动条仅用于画布区域,但相反,它只是坚持其他框架。

我写的代码就是这个

numpy

如何只为Canvas窗口获取滚动条,"到蓝色背景"?

2 个答案:

答案 0 :(得分:0)

我猜你的意思是你要留下画布周围的所有空白区域,但是你希望滚动条“粘住”到画布的一侧。

实现这一目标的最简单方法是将画布和滚动条放在一个框架中,以便它们作为一个单元。

例如:

class Dashboard():
    def __init__(self, root):
        ....
        canvasContainer = tk.Frame(root)
        self.canvas = tk.Canvas(canvasContainer, ...)
        vsb = AutoScrollbar(canvasContainer, ...)
        hsb = AutoScrollbar(canvasContainer, ...)
        vsb.grid(row=0, column=0, sticky="ns")
        hsb.grid(row=1, column=1, sticky="ew")
        self.canvas.grid(row=0, column=1, sticky="nsew")
        canvasContainer.grid_rowconfigure(0, weight=1)
        canvasContainer.grid_columnconfigure(1, weight=0)
        ...
        canvasContainer.grid(row=0, column=3)
        ...

答案 1 :(得分:0)

@Bryan,我想要的只是将滚动条粘贴到画布区域,左侧是带有按钮的侧边栏。通过对您的建议稍作修改,我可以设法实现。这就是我所做的。

    # python dashboard

import tkinter as tk
from tkinter import *

class AutoScrollbar(Scrollbar):
    def set(self, lo, hi):
        if float(lo) <= 0.0 and float(hi) >= 1.0:
            self.tk.call("grid", "remove", self)
        else:
            self.grid()
        Scrollbar.set(self, lo, hi)
    def pack(self, **kw):
        raise TclError("cannot use pack with this widget")
    def place(self, **kw):
        raise TclError("cannot use place with this widget")

class Dashboard():
    def __init__(self, root):
        self.root=root
        root.title("Dashboard View")

        canvasContainer = tk.Frame(root)
        self.canvas=tk.Canvas(canvasContainer,background='blue')

        fhandle = open("dashboard_content.txt")
        lines = fhandle.read()
        fhandle.close()

        text1=self.canvas.create_text(400, 400, fill="white")
        self.canvas.itemconfig(text1, text=lines)

        vsb = AutoScrollbar(canvasContainer,orient=VERTICAL)
        hsb = AutoScrollbar(canvasContainer, orient=HORIZONTAL)
        vsb.grid(row=0, column=2, sticky="ns")
        hsb.grid(row=1, column=1, sticky="ew")
        self.canvas.grid(row=0,column=1,sticky="news")
        self.canvas.config(yscrollcommand=vsb.set, xscrollcommand=hsb.set, scrollregion=self.canvas.bbox("all"))


        vsb.config(command=self.canvas.yview)
        hsb.config(command=self.canvas.xview)

        canvasContainer.grid_rowconfigure(0, weight=1)
        canvasContainer.grid_columnconfigure(0, weight=1)

        canvasContainer.grid(row=0,column=3)
        frame = Frame(self.root, bd=2, relief=SUNKEN)
        frame.grid(row=0,column=0, sticky="nw")
        Button1=Button(frame,text="Status").grid(row = 0,column = 0, sticky = "we")
        Button2=Button(frame,text="Processes").grid(row = 0,column = 1, sticky = "we")
        Button3=Button(frame,text="Links").grid(row = 1,column = 0, sticky = "we")
        Button4=Button(frame,text="Traffic").grid(row = 1,column = 1, sticky = "we")
        Button5=Button(frame,text="App Version").grid(row = 2,column = 0, sticky = "we")
        Button5=Button(frame,text="Archive/Purge").grid(row = 2,column = 1, sticky = "we")

        self.canvas.update_idletasks()
#        Grid.columnconfigure(self.root,1,weight=1, minsize=100)

if __name__== '__main__':
    root=tk.Tk()
    board=Dashboard(root)
    root.mainloop()

任何想法,我怎样才能使整个窗口可扩展。