0

So I have this program written in Python Tkinter, it has two frames on two different tabs. On the first there is a button, which should create a third tab with the same frame as the first tab. However, instead of doing that, its replacing the first frame. How do I fix this?

Here is my code so far:

# Imports
from tkinter import *
from tkinter import ttk



# Screen
root = Tk()
root.geometry("600x600")
root.title("Example")



# Create New Tab Function
def CreateANewTabFunc():
    TabControl.add(Frame1, text="Tab 3")


    
# Tab Control - Used for Placing the Tabs
TabControl = ttk.Notebook(root)
TabControl.pack()



# Frame 1 = Main Frame
Frame1 = Frame(TabControl, width=500, height=500, bg="Orange")
Frame1.pack()



# Frame 2 = Settings or Something Else
Frame2 = Frame(TabControl, width=500, height=500, bg="Green")
Frame2.pack()



TabControl.add(Frame1, text="Tab 1")
TabControl.add(Frame2, text="Tab 2")



ExampleButton = Button(Frame1, text="Click Here to Add New Tab to notebook", command=CreateANewTabFunc)
ExampleButton.pack()



# Mainloop
root.mainloop()
Skcoder
  • 299
  • 1
  • 9

1 Answers1

0

You can't use the same frame on two tabs. If you want to create a third tab, you need to create a third frame.

def CreateANewTabFunc():
    another_frame = Frame(TabControl, bg="pink")
    TabControl.add(another_frame, text="Tab 3")
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • What if I want that frame only? Why can I not use the same frame? – Skcoder May 31 '21 at 00:31
  • @Skcoder: why? I don't know. That's just how it's designed. There's no real point in having two tabs that open to the exact same content. – Bryan Oakley May 31 '21 at 00:33
  • So I am trying to make a code editor, and I want it to have multi tab editing, so the user can work with multiple files at once. How would I do that if I cannot use the same frame with the textbox and scrollbars? – Skcoder May 31 '21 at 00:34
  • @Skcoder: like my answer says: you make a new frame with new scrollbars and a new text widget inside that frame. That's precisely how the notebook was designed to work. If you used the same frame over and over, you would only be able to have a single file open, and it would be open in every tab. – Bryan Oakley May 31 '21 at 00:38
  • Ok, so in the CreateANewTabFunc can I use another_frame multiple times because its in a function? – Skcoder Jun 01 '21 at 15:37