简体   繁体   中英

Python3 and Tkinter - How can I check which Notebook Tab is open?

my window contains three main widgets, two buttons ("Cancel" and "Export") and a Notebook. The latter has two tabs with others widgets, and both of them must use the same buttons placed in the window. now, keeping in mind what I just wrote, how can I track in which Notebook tab the user are working in order to adapt the button's behaviour to the active tab? in other words, I want to change the "Export" button's behaviour depending on which Notebook tab is used. how can I reach my goal?


from tkinter import *
from tkinter import ttk, scrolledtext, filedialog

class MainWindow:

def __init__(self, window):

    ### MAIN WINDOW

    self.window = window
    self.window.title("My Software")
    self.window.configure(background="#f0f0f0")
    self.window.geometry("530x438+360+200")
    self.window.resizable (width=False, height=False)

    style = ttk.Style()
    style.configure("InWhiteFrame.TButton", background="#ffffff", padding = 0)
    style.configure("TButton", padding = 0)
    style.configure("BluText.TLabel", foreground="#99c1e8")
    style.configure("GreyBackground.TLabel", foreground="adadad", background="#e1e1e1", insertbackground="red", fieldbackground= 'blue')

    self.MyNotebook = ttk.Notebook(self.window)
    self.MyNotebook.place(x = 12, y = 12)

    Frame_1 = Frame(self.MyNotebook, background = "#ffffff", width = 506, height = 354, highlightbackground = "#d3d5d9", highlightthickness = 0)
    Frame_1.place(x = 12, y = 12)

    Frame_2 = Frame(self.MyNotebook, background = "#ffffff", width = 506, height = 354, highlightbackground = "#d3d5d9", highlightthickness = 0)
    Frame_2.place(x = 12, y = 12)

    self.MyNotebook.add(Frame_1, text = "Tab 1")
    self.MyNotebook.add(Frame_2, text = "Tab 2")

    self.ExportButton = ttk.Button(self.window, text = "Export", width = 16, command = self.__Save)
    self.ExportButton.place(x = 336, y = 403)
    self.ExportButton.config(state='disable')

    ttk.Button(self.window, text = "Cancel", width = 11, command = self.__Close).place(x = 445, y = 403)

    ### TAB 1

    self.SV1 = StringVar()
    ttk.Label(Frame_1, text = "File 1:", background = "#ffffff").place(x = 16, y = 80)
    self.Entry1 = ttk.Entry(Frame_1, text="", width = 52, textvariable=self.SV1)
    self.Entry1.place(x = 116, y = 80)
    ttk.Button(Frame_1, text="...", width = 4, style = "InWhiteFrame.TButton", command = self.__GetEntry1).place(x = 440, y = 79)

    self.SV2 = StringVar()
    ttk.Label(Frame_1, text = "File 2:", background = "#ffffff").place(x = 16, y = 111)
    self.Entry2 = ttk.Entry(Frame_1, text = "", width = 52, textvariable=self.SV2)
    self.Entry2.place(x = 116, y = 111)
    self.Entry2.config(state='disable')
    self.Entry2Button = ttk.Button(Frame_1, text="...", width = 4, style = "InWhiteFrame.TButton", command = self.__GetEntry2)
    self.Entry2Button.place(x = 440, y = 109)
    self.Entry2Button.config(state='disable')

    self.SV3 = StringVar()
    ttk.Label(Frame_1, text = "File 3:", background = "#ffffff").place(x = 16, y = 218)
    self.Entry3 = ttk.Entry(Frame_1, text = "", width = 52, textvariable=self.SV3)
    self.Entry3.place(x = 116, y = 218)
    self.Entry3.config(state='disable')
    self.Entry3Button = ttk.Button(Frame_1, text="...", width = 4, style = "InWhiteFrame.TButton", command = self.__GetEntry3)
    self.Entry3Button.place(x = 440, y = 217)
    self.Entry3Button.config(state='disable')

    self.window.protocol("WM_DELETE_WINDOW", self.__Close)

    self.SV1.trace("w", self.__Trace)
    self.SV2.trace("w", self.__Trace)
    self.SV3.trace("w", self.__Trace)
    self.window.bind("<<NotebookTabChanged>>", self.__GetActiveTab)

    ### TAB 2   

    self.LeftSide = scrolledtext.ScrolledText(Frame_2, wrap = WORD, width = 34, height = 11, font = ("Segoe UI", 9))
    self.LeftSide.place(x = 18, y = 42)
    Canvas(Frame_2, width = 207, height = 1, background = "#a0a0a0", highlightthickness = 0).place(x = 18, y = 210)

    RightSide = scrolledtext.ScrolledText(Frame_2, wrap = WORD, width = 34, height = 11, font = ("Segoe UI", 9), state="disable")
    RightSide.place(x = 262, y = 42)
    Canvas(Frame_2, width = 207, height = 1, background = "#a0a0a0", highlightthickness = 0).place(x = 262, y = 210)


    self.SV4 = StringVar()
    self.SV4.set("Option 1")        
    ExportOptions = ttk.Combobox(Frame_2, values = ("Option 1", "Option 2", "Option 3"), state="readonly", textvariable = self.SV4, width = 14)
    ExportOptions.place(x = 93, y = 292)

    self.SV5 = StringVar()
    self.SV5.set("1")
    SaveOptions = ttk.Combobox(Frame_2, values = ("1", "2"), state="readonly", textvariable = self.SV5, width = 8)
    SaveOptions.place(x = 299, y = 292)


def __Trace(self, *args):
    if self.SV1.get() != "":
        self.ExportButton.config(state = "normal")
        self.Entry2.config(state = "normal")
        self.Entry2Button.config(state = "normal")
        self.Entry3.config(state = "normal")
        self.Entry3Button.config(state = "normal")            
        if self.SV2.get() != "":
            self.Entry3.config(state = "disable")
            self.Entry3Button.config(state = "disable")
        elif self.SV3.get() != "":
            self.Entry2.config(state = "disable")
            self.Entry2Button.config(state = "disable")               
    else:
        self.ExportButton.config(state = "disable")            
        self.Entry2.delete(-1, END)    
        self.Entry2.config(state = "disable")
        self.Entry2Button.config(state = "disable")            
        self.Entry3.delete(-1, END)            
        self.Entry3.config(state = "disable") 
        self.Entry3Button.config(state = "disable")


def __GetEntry1(self):
    path = filedialog.askopenfilename(parent = self.window)
    if path != "":
        self.Entry1.delete(-1, END)
        self.Entry1.insert(0, path)

def __GetEntry2(self):
    path = filedialog.askopenfilename(parent = self.window)
    if path != "":
        self.Entry2.delete(-1, END)
        self.Entry2.insert(0, path)

def __GetEntry3(self):
    path = filedialog.askopenfilename(parent = self.window)
    if path != "":
        self.Entry3.delete(-1, END)
        self.Entry3.insert(0, path)

def __GetActiveTab(self, event):
    self.tab = self.MyNotebook.index(self.MyNotebook.select())

def __Save(self):
    print("The user clicked the \"Export\" button in the tab :", self.tab)
    print("It means that I will export the information specified in the tab ", self.tab)

def __Close(self):
    self.window.destroy()


def main():
root = Tk()
app = MainWindow(root)
root.mainloop()

if __name__ == "__main__":
main()

ttk.Notebook has an event called <<NotebookTabChanged>> .

Here is an minimal example:

import tkinter as tk
from tkinter import ttk


def tab_changed(event):
    print(notebook.select())  # get the instance of tab
    print(notebook.index(notebook.select()))  # get the index of the tab.


root = tk.Tk()

notebook = ttk.Notebook(root)
notebook.pack()

tab1 = tk.Frame()
notebook.add(tab1, text="tab1")

tab2 = tk.Frame()
notebook.add(tab2, text="tab2")

root.bind("<<NotebookTabChanged>>", tab_changed)

root.mainloop()

Reference: ttk document

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