Python Forum

Full Version: Notebook accessing a specific tab using a button from a toplevel frame
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I really knew to python. I have made a notebook GUI with 7 tabs, with a button on tab 4 that opens up a new toplevel window. On the toplevel window, I want to have a button, when it is clicked it directs the user to the root windows notebook, tab 7.
Can anyone please help me. Here is my code.

from tkinter import *
from tkinter import ttk
import tkinter.messagebox
import tkinter as tk


class Root(Tk):
    def __init__(self):
        super(Root, self).__init__()
        self.title("Application")
        self.minsize(640, 400)
        self.configure(background="white")

        self.createMenu()

        tabControl = ttk.Notebook(self)
        self.tab1 = ttk.Frame(tabControl)
        tabControl.add(self.tab1, text="tab 1")

        self.tab2 = ttk.Frame(tabControl)
        tabControl.add(self.tab2, text="tab 2")

        self.tab3 = ttk.Frame(tabControl)
        tabControl.add(self.tab3, text="tab 3")

        self.tab4 = ttk.Frame(tabControl)
        tabControl.add(self.tab4, text="tab 4")
        self.addingTab4()

        self.tab5 = ttk.Frame(tabControl)
        tabControl.add(self.tab5, text="tab 5")


        self.tab6 = ttk.Frame(tabControl)
        tabControl.add(self.tab6, text="tab 6")


        self.tab7 = ttk.Frame(tabControl)
        tabControl.add(self.tab7, text="Tab 7")

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

    def startpressed(self):
        new = tk.Toplevel(self)
        new.minsize(640, 400)
        new.geometry('500x300')
        new.configure(background="white")
        tabControl1 = ttk.Notebook(new)
        new.tab1 = ttk.Frame(tabControl1)
        tabControl1.add(new.tab1, text="tab 1")
        tabControl1.pack(expand=1, fill="both")

    def createMenu(self):
        menubar = Menu(self)
        self.config(menu=menubar)

        file_menu = Menu(menubar, tearoff=0)
        menubar.add_cascade(label="File", menu=file_menu)
        file_menu.add_command(label="Exit")

        help_menu = Menu(menubar, tearoff=0)
        menubar.add_cascade(label="Help", menu=help_menu)
        help_menu.add_command(label="About Us")


    def addingTab4(self):
        Label(self.tab4, text= "Please Select your choice" ).place(x= 250, y= 20)
        submit = Button(self.tab4, text="Submit", command=lambda: self.submit()).place(x=520, y=320)


    def submit(self):
        newTop = Toplevel(self.master)
        display = Label(newTop, text="Review").pack()
        newTop.title("Review and Submit")
        newTop.focus_set()
        newTop.geometry("400x600")
        # WOULD LIKE: when this button is clicked it takes the user to tab 7 of the notebook window
        btnResult = Button(newTop, text="Tab 7").pack()
        btnBack = Button(newTop, text="Back").pack()


root = Root()
root.mainloop()
def tab7(self, event):
    notebook.select(self.tab7)
bind your button to call above method, or if you use inline control, remove event from args
Thank you for the prompt response, however this is the error I get when I try and do this.


TypeError: result1() missing 1 required positional argument: 'self'

Here is my code:
from tkinter import *
from tkinter import ttk
import tkinter.messagebox
import tkinter as tk


class Root(Tk):
    def __init__(self):
        super(Root, self).__init__()
        self.title("Application")
        self.minsize(640, 400)
        self.configure(background="white")

        self.createMenu()

        tabControl = ttk.Notebook(self)
        self.tab1 = ttk.Frame(tabControl)
        tabControl.add(self.tab1, text="tab 1")

        self.tab2 = ttk.Frame(tabControl)
        tabControl.add(self.tab2, text="tab 2")

        self.tab3 = ttk.Frame(tabControl)
        tabControl.add(self.tab3, text="tab 3")

        self.tab4 = ttk.Frame(tabControl)
        tabControl.add(self.tab4, text="tab 4")
        self.addingTab4()

        self.tab5 = ttk.Frame(tabControl)
        tabControl.add(self.tab5, text="tab 5")


        self.tab6 = ttk.Frame(tabControl)
        tabControl.add(self.tab6, text="tab 6")


        self.tab7 = ttk.Frame(tabControl)
        tabControl.add(self.tab7, text="Tab 7")

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

    def startpressed(self):
        new = tk.Toplevel(self)
        new.minsize(640, 400)
        new.geometry('500x300')
        new.configure(background="white")
       # tabControl1 = ttk.Notebook(new)
        #new.tab1 = ttk.Frame(tabControl1)
        #tabControl1.add(new.tab1, text="tab 1")
        #tabControl1.pack(expand=1, fill="both")

    def createMenu(self):
        menubar = Menu(self)
        self.config(menu=menubar)

        file_menu = Menu(menubar, tearoff=0)
        menubar.add_cascade(label="File", menu=file_menu)
        file_menu.add_command(label="Exit")

        help_menu = Menu(menubar, tearoff=0)
        menubar.add_cascade(label="Help", menu=help_menu)
        help_menu.add_command(label="About Us")


    def addingTab4(self):
        Label(self.tab4, text= "Please Select your choice" ).place(x= 250, y= 20)
        submit = Button(self.tab4, text="Submit", command=lambda: self.submit()).place(x=520, y=320)


    def submit(self):
        newTop = Toplevel(self.master)
        display = Label(newTop, text="Review").pack()
        newTop.title("Review and Submit")
        newTop.focus_set()
        newTop.geometry("400x600")
        # WOULD LIKE: when this button is clicked it takes the user to tab 7 of the notebook window
        btnResult = Button(newTop, text="Tab 7",command=lambda: self.result1()).pack()
        btnBack = Button(newTop, text="Back").pack()

    def result1(event, self):
        ttk.Notebook.select(self.tab7)

root = Root()
root.mainloop()
Change:
btnResult = Button(newTop, text="Tab 7", command=self.result1).pack()
Note no parenthesis on command name. you only need lambda if you are passing arguments.
and
    def result1(self):
        ttk.Notebook.select(self.tab7)
you don't need event if not using bind.
even if you were using bind, self always goes first.
Thank you, This is now the new error:
line 892, in select
return self.tk.call(self._w, "select", tab_id)
_tkinter.TclError: bad command "select": must be configure, cget, instate, state, or identify

If tried to replace it with "configure" and state "state", it gives no error but doesnt work when the button is clicked.

With cget, instate and identify i dont know what the other arguemnts would be.
I am using python version 3.7, if that helps. Thank you again.

My Current code:

from tkinter import *
from tkinter import ttk
import tkinter.messagebox
import tkinter as tk


class Root(Tk):
    def __init__(self):
        super(Root, self).__init__()
        self.title("Application")
        self.minsize(640, 400)
        self.configure(background="white")

        self.createMenu()

        tabControl = ttk.Notebook(self)
        self.tab1 = ttk.Frame(tabControl)
        tabControl.add(self.tab1, text="tab 1")


        self.tab2 = ttk.Frame(tabControl)
        tabControl.add(self.tab2, text="tab 2")

        self.tab3 = ttk.Frame(tabControl)
        tabControl.add(self.tab3, text="tab 3")

        self.tab4 = ttk.Frame(tabControl)
        tabControl.add(self.tab4, text="tab 4")
        self.addingTab4()

        self.tab5 = ttk.Frame(tabControl)
        tabControl.add(self.tab5, text="tab 5")


        self.tab6 = ttk.Frame(tabControl)
        tabControl.add(self.tab6, text="tab 6")


        self.tab7 = ttk.Frame(tabControl)
        tabControl.add(self.tab7, text="Tab 7")

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

    def startpressed(self):
        new = tk.Toplevel(self)
        new.minsize(640, 400)
        new.geometry('500x300')
        new.configure(background="white")
       # tabControl1 = ttk.Notebook(new)
        #new.tab1 = ttk.Frame(tabControl1)
        #tabControl1.add(new.tab1, text="tab 1")
        #tabControl1.pack(expand=1, fill="both")

    def createMenu(self):
        menubar = Menu(self)
        self.config(menu=menubar)

        file_menu = Menu(menubar, tearoff=0)
        menubar.add_cascade(label="File", menu=file_menu)
        file_menu.add_command(label="Exit")

        help_menu = Menu(menubar, tearoff=0)
        menubar.add_cascade(label="Help", menu=help_menu)
        help_menu.add_command(label="About Us")


    def addingTab4(self):
        Label(self.tab4, text= "Please Select your choice" ).place(x= 250, y= 20)
        submit = Button(self.tab4, text="Submit", command=lambda: self.submit()).place(x=520, y=320)


    def submit(self):
        newTop = Toplevel(self.master)
        display = Label(newTop, text="Review").pack()
        newTop.title("Review and Submit")
        newTop.focus_set()
        newTop.geometry("400x600")
        # WOULD LIKE: when this button is clicked it takes the user to tab 7 of the notebook window
        btnResult = Button(newTop, text="Tab 7",command= self.result1).pack()
        btnBack = Button(newTop, text="Back").pack()

    def result1(self):
        ttk.Notebook.select(self.tab7)

root = Root()
root.mainloop() 
please always post entire error traceback without modification in BBCODE error tags (X in circle on toolbar)
your code goes between tags
Sorry, really new to all this.
Error:
Exception in Tkinter callback Traceback (most recent call last): File "C:\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1702, in __call__ return self.func(*args) File "C:\PycharmProjects/scrollbar/.idea/scrollbar.py", line 83, in result1 ttk.Notebook.select(self.tab7) File "C:\AppData\Local\Programs\Python\Python37-32\lib\tkinter\ttk.py", line 892, in select return self.tk.call(self._w, "select", tab_id) _tkinter.TclError: bad command "select": must be configure, cget, instate, state, or identify
Hi honestie

Try this modified script:
from tkinter import *
from tkinter import ttk
import tkinter.messagebox
import tkinter as tk
 
 
class Root(Tk):
    def __init__(self):
        super(Root, self).__init__()
        self.title("Application")
        self.minsize(640, 400)
        self.configure(background="white")
 
        self.createMenu()
 
        tabControl = ttk.Notebook(self)
        self.tab1 = ttk.Frame(tabControl)
        tabControl.add(self.tab1, text="tab 1")
 
        self.tab2 = ttk.Frame(tabControl)
        tabControl.add(self.tab2, text="tab 2")
 
        self.tab3 = ttk.Frame(tabControl)
        tabControl.add(self.tab3, text="tab 3")
 
        self.tab4 = ttk.Frame(tabControl)
        tabControl.add(self.tab4, text="tab 4")
        self.addingTab4()
 
        self.tab5 = ttk.Frame(tabControl)
        tabControl.add(self.tab5, text="tab 5")
 
 
        self.tab6 = ttk.Frame(tabControl)
        tabControl.add(self.tab6, text="tab 6")
 
 
        self.tab7 = ttk.Frame(tabControl)
        tabControl.add(self.tab7, text="Tab 7")
 
        tabControl.pack(expand=1, fill="both")
        
        self.tab_control = tabControl
        
    def startpressed(self):
        new = tk.Toplevel(self)
        new.minsize(640, 400)
        new.geometry('500x300')
        new.configure(background="white")
        tabControl1 = ttk.Notebook(new)
        new.tab1 = ttk.Frame(tabControl1)
        tabControl1.add(new.tab1, text="tab 1")
        tabControl1.pack(expand=1, fill="both")
 
    def createMenu(self):
        menubar = Menu(self)
        self.config(menu=menubar)
 
        file_menu = Menu(menubar, tearoff=0)
        menubar.add_cascade(label="File", menu=file_menu)
        file_menu.add_command(label="Exit")
 
        help_menu = Menu(menubar, tearoff=0)
        menubar.add_cascade(label="Help", menu=help_menu)
        help_menu.add_command(label="About Us")
 
 
    def addingTab4(self):
        Label(self.tab4, text= "Please Select your choice" ).place(x= 250, y= 20)
        submit = Button(self.tab4, text="Submit", command=lambda: self.submit()).place(x=520, y=320)
 
 
    def submit(self):
        newTop = Toplevel(self.master)
        display = Label(newTop, text="Review").pack()
        newTop.title("Review and Submit")
        newTop.focus_set()
        newTop.geometry("400x600")
        # WOULD LIKE: when this button is clicked it takes the user to tab 7 of the notebook window
        btnResult = Button(newTop, text="Tab 7",command=self.result1).pack()
        btnBack = Button(newTop, text="Back").pack()
 
    def result1(self):
        #ttk.Notebook.select(self.tab7)
        self.tab_control.select(self.tab7)
 
root = Root()
root.mainloop()
Greetings wuf
Thank you so much wuf!!