滚动笔记本选项卡Tkinter

时间:2018-06-29 16:11:40

标签: python tkinter

我想制作许多笔记本标签,并希望将它们放在画布中并添加水平滚动条,以便可以滚动它们。 我设置了画布大小,但是当我添加新标签时,画布大小会不断变化。另外,滚动条不起作用,您能告诉我我在做什么错吗?

程序没有显示任何错误。这是代码:

from tkinter import *
from tkinter import ttk

myApp = Tk()
myApp.title(" Program ")                         
myApp.geometry("900x500")

CanvasTabs = Canvas(myApp, width=50, height=50)
CanvasTabs.grid(row=0,column=0)

tabs = ttk.Notebook(CanvasTabs, width=100, height=100)

tab1 = ttk.Frame(tabs)
tabs.add(tab1,text="  Tab 1  ")

tab2 = ttk.Frame(tabs)
tabs.add(tab2,text="  Tab 2  ")

tab3 = ttk.Frame(tabs)
tabs.add(tab3,text="  Tab 3  ")

tab4 = ttk.Frame(tabs)
tabs.add(tab4,text="  Tab 4  ")


hbar=Scrollbar(CanvasTabs,orient=HORIZONTAL)
hbar.pack(side=TOP,fill=X)
hbar.config(command=CanvasTabs.xview)

CanvasTabs.config(xscrollcommand=hbar.set)


tabs.pack(expand=1, fill="both")   

myApp.mainloop()

2 个答案:

答案 0 :(得分:2)

我编写了一个小部件来解决该问题。这是一个真正的解决方案: https://github.com/muhammeteminturgut/ttkScrollableNotebook

Demonstration

IN

答案 1 :(得分:0)

this post上的布莱恩(Bryan)示例为例,并对其进行修改以包含您的Notebook代码,我们获得了一个功能正常的滚动条,如果滚动条超出了窗口的限制,您可以在该滚动条上滚动。

Bryan的示例使用了pack()几何管理器,但是我个人认为grid()更易于可视化,因此在示例中我将包替换为grid()

更新:

import tkinter as tk
import tkinter.ttk as ttk


class Example(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.canvas = tk.Canvas(self, borderwidth=0)
        self.frame = tk.Frame(self.canvas)

        self.vsb = tk.Scrollbar(self, orient="horizontal", command=self.canvas.xview)
        self.vsb.grid(row=1, column=0, sticky="nsew")

        self.canvas.configure(xscrollcommand=self.vsb.set)
        self.canvas.grid(row=0, column=0, sticky="nsew")
        self.canvas.create_window((3,2), window=self.frame, anchor="nw", tags="self.frame")

        self.frame.bind("<Configure>", self.frame_configure)
        self.populate()

    def populate(self):
        tabs = ttk.Notebook(self.frame, width=100, height=100)
        for tab in range(50):
            tabs.add(ttk.Frame(tabs), text=" Tab {}  ".format(tab))
        tabs.grid(row=0, column=0, sticky="ew")


    def frame_configure(self, event):
        self.canvas.configure(scrollregion=self.canvas.bbox("all"))

if __name__ == "__main__":
    app = Example()
    app.mainloop()

更新结果:

enter image description here

根据您在注释中的请求,这里是一个非OOP示例:

import tkinter as tk
import tkinter.ttk as ttk


root = tk.Tk()
canvas = tk.Canvas(root, borderwidth=0)
frame = tk.Frame(canvas)

vsb = tk.Scrollbar(root, orient="horizontal", command=canvas.xview)
vsb.grid(row=1, column=0, sticky="nsew")

canvas.configure(xscrollcommand=vsb.set)
canvas.grid(row=0, column=0, sticky="nsew")
canvas.create_window((3,2), window=frame, anchor="nw", tags="frame")

tabs = ttk.Notebook(frame, width=100, height=100)
for tab in range(50):
    tabs.add(ttk.Frame(tabs), text=" Tab {}  ".format(tab))
tabs.grid(row=0, column=0, sticky="ew")

def frame_configure(event):
    global canvas
    canvas.configure(scrollregion=canvas.bbox("all"))

frame.bind("<Configure>", frame_configure)

root.mainloop()