在tkinter中添加滚动条

时间:2019-02-25 17:26:28

标签: tkinter scrollbar

我试图在我的tkinter界面中添加一个滚动条,但是我不知道如何正确地进行操作。我查看了有关该主题的各种现有主题,但找不到所需的帮助;因此这篇文章。

有人可以告诉我哪些滚动条没有添加到我的底框吗?

代码如下:

import tkinter as tk
import webbrowser

class Pierre:
    def __init__(self, master, url, chk_lbl):
        self.url = url
        self.chk_lbl = chk_lbl
        self.counter = 0
        self.vars = []
        _, i = master.grid_size() # get the current row number

        lbl = tk.Label(master, text=url, fg="blue", cursor="hand2")
        lbl.grid(row=i, column=1)
        lbl.bind("<Button-1>", self.callback)

        ic = 2
        for lbl in self.chk_lbl:
            var = tk.IntVar()
            chk = tk.Checkbutton(master, text=lbl, variable=var)
            chk.grid(row=i, column=ic)
            self.vars.append(var)
            ic+=1

    def state(self):
        return map((lambda var: var.get()), self.vars)

    def callback(self, event):
        webbrowser.open_new(self.url)

class TestClass(tk.Tk):
    def __init__(self, **kwargs):
        root = tk.Tk.__init__(self, **kwargs)
        self.title('Test')
        self.canvas = tk.Canvas(root)
        self.canvas.pack(side=tk.LEFT)
        self.scrollbar = tk.Scrollbar(root, command=self.canvas.yview)
        self.scrollbar.pack(side=tk.LEFT, fill='y')
        self.canvas.configure(yscrollcommand = self.scrollbar.set)
        self.canvas.bind('<Configure>', self.canvas.configure(scrollregion=self.canvas.bbox('all')))

        self.topframe = tk.Frame(self.canvas)
        self.topframe.pack( side = tk.TOP, pady=30)

        self.bottomframe = tk.Frame(self.canvas)
        self.canvas.create_window(0,0, window=self.bottomframe, anchor='nw')

        self.bottomframe.pack( side = tk.BOTTOM )

        self.button = tk.Button(self.topframe, text='Click', command = self.output_value)
        self.button.pack(side="left", fill="both", expand=True)

    def output_value(self):
        urls = ["http://www.google.com", "http://www.facebook.com", "http://bbc.co.uk", ]
        chk_lbl = ['select']
        for url in urls:
            Pierre(self.bottomframe, url, chk_lbl)

if __name__ == "__main__":
    root = TestClass()
    root.mainloop()

1 个答案:

答案 0 :(得分:0)

您的代码中存在一些问题。我已经修复了它们,现在它应该可以按照您的要求工作。检查更改的代码区域:

import tkinter as tk
import webbrowser

class Pierre:
    def __init__(self, master, url, chk_lbl):
        self.url = url
        self.chk_lbl = chk_lbl
        self.counter = 0
        self.vars = []
        _, i = master.grid_size()   # get the current row number

        lbl = tk.Label(master, text=url, fg="blue", cursor="hand2")
        lbl.grid(row=i, column=1)
        lbl.bind("<Button-1>", self.callback)

        ic = 2
        for lbl in self.chk_lbl:
            var = tk.IntVar()
            chk = tk.Checkbutton(master, text=lbl, variable=var)
            chk.grid(row=i, column=ic)
            self.vars.append(var)
            ic += 1

    def state(self):
        return map((lambda var: var.get()), self.vars)

    def callback(self, event):
        webbrowser.open_new(self.url)

class TestClass(tk.Tk):
    def __init__(self, **kwargs):
        root = tk.Tk.__init__(self, **kwargs)
        self.title('Test')

        # --- Changes starts from here --- #
        def onFrameConfigure(canvas):
            canvas.configure(scrollregion=canvas.bbox('all'))

        self.topframe = tk.Frame(root)
        self.topframe.pack(side=tk.TOP, pady=30)

        self.canvas = tk.Canvas(root)
        self.bottomframe = tk.Frame(self.canvas)
        # self.bottomframe.pack( side = tk.BOTTOM )
        self.scrollbar = tk.Scrollbar(root, orient='vertical', command=self.canvas.yview)
        self.canvas.configure(yscrollcommand=self.scrollbar.set)
        self.canvas.pack(side=tk.LEFT, fill='y')
        self.scrollbar.pack(side=tk.LEFT, fill='y')
        self.canvas.create_window(0, 0, window=self.bottomframe, anchor='nw')
        self.bottomframe.bind('<Configure>', lambda event, canvas=self.canvas: onFrameConfigure(self.canvas))
        # --- Changes ends here --- #

        self.button = tk.Button(self.topframe, text='Click', command=self.output_value)
        self.button.pack(side="left", fill="both", expand=True)

    def output_value(self):
        urls = ["http://www.google.com", "http://www.facebook.com", "http://bbc.co.uk", ]
        chk_lbl = ['select']
        for url in urls:
            Pierre(self.bottomframe, url, chk_lbl)

if __name__ == "__main__":
    root = TestClass()
    root.mainloop()