Home > Software engineering >  Tkinter-How to add a Canvas/Frame in ttk.Notebook()
Tkinter-How to add a Canvas/Frame in ttk.Notebook()

Time:09-26

After searching a lot on youtube and google, I learned how to add a fullscreen scrollbar in Canvas. It looks like this. But now I want to Add this Frame/Canvas in ttk.Notebook() as a Tab, like this test project. But when I type book.add(main,text='Tab 1') It shows this error _tkinter.TclError: can't add .!frame.!canvas.!frame as slave of .!notebook. Can anyone know how to show a canvas as a frame and add it to notebook() as tabs??

Here is the main Code:

from tkinter import *
from tkinter import ttk

root=Tk()
root.title('Scroll Bar Test')
root.geometry('500x400')

#Create Main Frame
main_frame=Frame(root)
main_frame.pack(fill=BOTH,expand=1)
#Create Canvas to add scrollbar
my_canvas=Canvas(main_frame)
my_canvas.pack(side=LEFT,fill=BOTH,expand=1)
#Create ScrollBar
my_scrollbar=ttk.Scrollbar(main_frame,orient=VERTICAL,command=my_canvas.yview)
my_scrollbar.pack(side=RIGHT,fill=Y)
#Adding ScrollBar to Canvas
my_canvas.configure(yscrollcommand=my_scrollbar.set)
my_canvas.bind('<Configure>',lambda e: my_canvas.configure(scrollregion=my_canvas.bbox("all")))
#Creating Frame to show the Canvas
main=Frame(my_canvas)
#Showing Canvas into/as a Frame
my_canvas.create_window((0,0),window=main,anchor="nw")

for i in range(100):
      Button(main, text=f'Button {1 i} Yoo!',font='arial 15').pack()

#Create NoteBook
book=ttk.Notebook(root)
book.pack()
#
book.add(main,text='Tab 1')

root.mainloop()

CodePudding user response:

Two frames created as Tab1 and Tab2, then one with a label and another one with a vertical-scrollable canvas.

from tkinter import *
from tkinter import ttk

def scroll(event, canvas):
    if canvas.yview() == (0.0, 1.0):
        return
    if event.num == 5 or event.delta < 0:
        canvas.yview_scroll(1, "unit")
    elif event.num == 4 or event.delta > 0:
        canvas.yview_scroll(-1, "unit")

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

root = Tk()

book = ttk.Notebook(root)

tab1 = Frame(book, width=160, height=120)
tab2 = Frame(book, width=160, height=120)

tab1.pack_propagate(0)
tab2.pack_propagate(0)

book.add(tab1, text='TAB 1')
book.add(tab2, text='TAB 2')
book.pack(expand=1, fill=BOTH)

canvas = Canvas(tab1)
vscroll = Scrollbar(tab1, orient=VERTICAL, command=canvas.yview)
vscroll.pack(side=RIGHT, fill=Y, expand=0)
canvas.configure(yscrollcommand=vscroll.set)
canvas.pack(side=LEFT, fill=BOTH, expand=1)

frame = Frame(canvas)
canvas.create_window((0, 0), window=frame, anchor=CENTER)
labels = [Label(frame, text=f'Label {i:0>2d}', bg='green', fg='white', width=15) for i in range(20)]
for label in labels:
    label.pack(side=TOP)
    label.bind('<4>', lambda event, canvas=canvas:scroll(event, canvas))
    label.bind('<5>', lambda event, canvas=canvas:scroll(event, canvas))
    label.bind('<MouseWheel>', lambda event, canvas=canvas:scroll(event, canvas))

canvas.bind('<Configure>', configure)
canvas.bind('<4>', lambda event, canvas=canvas:scroll(event, canvas))
canvas.bind('<5>', lambda event, canvas=canvas:scroll(event, canvas))
canvas.bind('<MouseWheel>', lambda event, canvas=canvas:scroll(event, canvas))

label = Label(tab2, text="This is Tab 2", bg='green', fg='white', width=15)
label.pack(side=LEFT, fill=NONE, expand=1)

root.mainloop()

CodePudding user response:

There are few issues in your code:

  • should add main_frame (outer frame) instead of main (internal frame) to book
  • should bind <Configure> on main instead of my_canvas
  • create main_frame as a child of book instead of root

Below is modified code:

import tkinter as tk
from tkinter import ttk

root = tk.Tk()
root.title('Scroll Bar Test')
root.geometry('500x400')

# Create NoteBook
book = ttk.Notebook(root)
book.pack(fill=tk.BOTH, expand=1)

# Create Main Frame
main_frame = tk.Frame(book)
# put the main frame into notebook
book.add(main_frame, text='Tab 1')

# Create canvas inside main frame
my_canvas = tk.Canvas(main_frame)
my_canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)

# Create ScrollBar inside main frame
my_scrollbar = ttk.Scrollbar(main_frame, orient=tk.VERTICAL, command=my_canvas.yview)
my_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)

my_canvas.configure(yscrollcommand=my_scrollbar.set)

# Creating internal frame for the buttons
main = tk.Frame(my_canvas)
main.bind('<Configure>', lambda e: my_canvas.configure(scrollregion=my_canvas.bbox("all")))
my_canvas.create_window((0,0), window=main, anchor="nw")

for i in range(100):
    tk.Button(main, text=f'Button {1 i} Yoo!', font='arial 15').pack()

root.mainloop()

Note that I have changed from tkinter import * to import tkinter as tk because wild card import is not recommended.

  • Related