Bottom Page

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()
Quote
#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
Quote
#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()
Quote
#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.
Quote
#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() 
Quote
#6
please always post entire error traceback without modification in BBCODE error tags (X in circle on toolbar)
your code goes between tags
Quote
#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
Quote
#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
Quote
#9
Thank you so much wuf!!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 243 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] Toplevel window and global widgets? KevinBrown 3 460 Apr-25-2019, 06:02 PM
Last Post: Yoriz
  [Tkinter] create and insert a new frame on top of another frame atlass218 4 495 Apr-18-2019, 05:36 PM
Last Post: atlass218
  [Tkinter] Close Toplevel after clicking button jollydragon 3 2,517 Jul-12-2018, 06:22 AM
Last Post: jollydragon
  Using a class to create instances of Tkinter Toplevel() windows nortski 2 2,909 Mar-27-2018, 11:44 AM
Last Post: nortski
  [Tkinter] Frame size only works if frame is empty(Solved) Tuck12173 7 1,677 Jan-29-2018, 10:44 PM
Last Post: Larz60+
  [Tkinter] Selected radio button in push button in Tkinter prashantfunde91 1 5,890 Jun-22-2017, 05:27 PM
Last Post: DeaD_EyE
  [Tkinter] I have a Toplevel button in tkinker that I want to close the window and then perform Bloodypizza17 2 2,990 Jan-06-2017, 07:18 PM
Last Post: Bloodypizza17

Forum Jump:


Users browsing this thread: 1 Guest(s)