Python tab切换面板下事件,button获取对应tab下的数据

import tkinter as tk # imports
from tkinter import ttk
from tkinter import *
class initGUI():
    currentContent = ""
    confMedication = "Medication"
    confScheme = "Scheme"
    confTreatment = "Treatment"
    confRadiation = "Radiation"
    confDisease = "Disease"
    confAll = [confTreatment, confScheme, confMedication, confRadiation, confDisease]
    tabNames = []
    # 存输入框
    tabInputs = []
    for x in range(len(confAll)):
        tabNames.append(confAll[x])
        tabInputs.append("")
    print(tabInputs)
    win = tk.Tk()  # Create instance
    win.geometry('500x300')
    win.resizable(0, 0)
    win.title("")  # Add a title
    tabControl = ttk.Notebook(win)  # Create Tab Control
    def __init__(self):
        self.createInput()
        initGUI.win.mainloop()  # Start GUI
    def handle(self,i):
        if initGUI.tabInputs[i].get() == "":
            print("空值")
            return
        print(initGUI.confAll[i])
        print(initGUI.tabInputs[i].get())
    def createInput(self):
        for x in range(len(initGUI.tabNames)):
            n = initGUI.tabNames[x]
            initGUI.tabNames[x] = ttk.Frame(initGUI.tabControl) # Create a tab
            initGUI.tabControl.add(initGUI.tabNames[x], text=initGUI.confAll[x])  # Add the tab
            initGUI.tabControl.pack(expand=1, fill="both")  # Pack to make visible
            initGUI.tabInputs[x] = StringVar()
            ttk.Entry(initGUI.tabNames[x],textvariable = initGUI.tabInputs[x]).pack()
            initGUI.tabInputs.append(initGUI.tabInputs[x])
            ttk.Button(initGUI.tabNames[x], text='添加'+n,command=lambda index=x,n=n:self.handle(index)).pack()
initGUI()

主要是n = initGUI.tabNames[x]ttk.Button(initGUI.tabNames[x], text=’添加’+n,command=lambda index=x,n=n:self.handle(index)).pack()这两句

参考链接

你可能感兴趣的:(python入门学习)