简体   繁体   中英

Python tkinter - Why does the entry box stay when I change tab?

I am new to using tkinter. I am basically trying to make a script that uses tabs/notebook tabs.

I have an entry box and it won't disappear when I change tabs, why?

Code:

import tkinter
from tkinter import ttk

win = tkinter.Tk()

win.geometry("500x500")

tab = ttk.Notebook(win)

page1 = tkinter.Frame(tab)
page2 = tkinter.Frame(tab)
page3 = tkinter.Frame(tab)

tab.add(page1, text="Page1")
tab.add(page2, text="Page2")
tab.add(page3, text="Page3")

tab.grid(sticky="W")

entry1 = tkinter.Entry(win)
entry1.insert(0, "Test Entry")
entry1.grid(row=1, column=0, sticky="W")

win.mainloop()

I don't want something like entry1.forget() I just want everything to disappear when I change tabs.

Can anybody help?

You have to create the Entry inside the tab and not the window. So just change the line:

entry1 = tkinter.Entry(win)

into:

entry1 = tkinter.Entry(page1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM