Python Forum
[Tkinter] Notebook accessing a specific tab using a button from a toplevel frame
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Notebook accessing a specific tab using a button from a toplevel frame
#1
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()
Reply
#2
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
Reply
#3
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()
Reply
#4
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.
Reply
#5
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() 
Reply
#6
please always post entire error traceback without modification in BBCODE error tags (X in circle on toolbar)
your code goes between tags
Reply
#7
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
Reply
#8
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
Reply
#9
Thank you so much wuf!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pass a variable between tkinter and toplevel windows janeik 10 2,138 Jan-24-2024, 06:44 AM
Last Post: Liliana
  [Tkinter] TKinter Remove Button Frame Nu2Python 8 816 Jan-16-2024, 06:44 PM
Last Post: rob101
  [Tkinter] Open tkinter colorchooser at toplevel (so I can select/focus on either window) tabreturn 4 1,831 Jul-06-2022, 01:03 PM
Last Post: deanhystad
  [Tkinter] Toplevel window menator01 5 2,985 Apr-18-2022, 06:01 PM
Last Post: menator01
  [Tkinter] Not able to get image as background in a Toplevel window finndude 4 3,839 Jan-07-2022, 10:10 PM
Last Post: finndude
  [Tkinter] Images in Toplevel() finndude 4 4,225 Mar-09-2021, 09:39 AM
Last Post: finndude
  Create image on a Toplevel using tkinter ViktorWong 3 7,745 Jun-13-2020, 03:21 PM
Last Post: deanhystad
  [Tkinter] Connect Toplevel Radiobuttons to root Label/Entry widgets iconit 2 2,399 Apr-28-2020, 06:50 AM
Last Post: iconit
  tkinter button not accessing the command when clicked jhf2 1 3,500 Nov-23-2019, 10:17 PM
Last Post: DT2000
  [Tkinter] how can disable menu [About] when Toplevel is active balenaucigasa 0 2,642 Oct-25-2019, 09:49 PM
Last Post: balenaucigasa

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020