简体   繁体   中英

How can i interligate a notebook with a drop down button in tkinter

I would like the user to write the recipe for a food and select which tab it will go on, how can I connect this and the recipe appears on the selected tab?

Code:

from tkinter import *
from tkinter import Tk
from tkinter import ttk
import tkinter as tk

class RecipesScreen():
    def __init__(self):
        self.root = tk.Tk()
        self.window_recipes()
        self.window_frame_recipes()
        self.widget_frame_recipes()
        self.root.mainloop()
    
    def window_recipes(self):
        self.root.title("Bom de Prato")
        self.root.geometry("800x600")
        self.root.resizable(False, False)

    def window_frame_recipes(self):
        self.tabs = ttk.Notebook(self.root) # Abas
        self.tabs.place(x = 0, y = 0, width = 800, height = 600)

        # Create Frame
        self.frame_healthy = Frame(self.tabs)
        self.frame_vegetarian = Frame(self.tabs)
        self.frame_vegan = Frame(self.tabs)
        self.frame_fastFood = Frame(self.tabs)
        self.frame_diabetics = Frame(self.tabs)
        self.frame_add_recipes = Frame(self.tabs)

        self.tabs.add(self.frame_healthy, text='healthy')
        self.tabs.add(self.frame_vegetarian, text='Vegetarian')
        self.tabs.add(self.frame_vegan, text='Vegan')
        self.tabs.add(self.frame_fastFood, text='Fast Food')
        self.tabs.add(self.frame_diabetics, text='Diabetics')
        self.tabs.add(self.frame_add_recipes, text='Add New Recipe')

    def widget_frame_recipes(self):
        # Create Label
        self.lb_add_new_recipe = Label(self.frame_add_recipes, text='Add New Recipe', font='arial 20')
        self.lb_where = Label(self.frame_add_recipes, text='Where do you like to add this recipe?', font='arial 10')

        self.lb_add_new_recipe.place(relx=0.36, rely=0)
        self.lb_where.place(relx=0, rely=0.10)

        # Drop Down Button
        self.Tipvar = StringVar(self.frame_add_recipes)
        self.TipV = ("Healthy", "Vegetarian", "Fast Food", "Diabetics")
        self.Tipvar.set("Healthy")
        self.popupMenu = OptionMenu(self.frame_add_recipes, self.Tipvar, *self.TipV)
        self.popupMenu.place(relx= 0.30, rely= 0.10, relwidth= 0.15, relheight= 0.05)

RecipesScreen()

You will need to make a dictionary or some other kind of mapping to associate the string with the corresponding Frame. You could do this manually, but it would be neater to back up and create the Frames from the list so that you get the dictionary from the creation process.

from tkinter import ttk
import tkinter as tk

TipV = ("Healthy", "Vegetarian", 'Vegan', "Fast Food", "Diabetics")

class RecipesScreen():
    def __init__(self):
        self.root = tk.Tk()
        self.window_recipes()
        self.window_frame_recipes()
        self.widget_frame_recipes()
        self.root.mainloop()

    def window_recipes(self):
        self.root.title("Bom de Prato")
        self.root.geometry("800x600")
        self.root.resizable(False, False)

    def window_frame_recipes(self):
        self.tabs = ttk.Notebook(self.root) # Abas
        self.tabs.place(x = 0, y = 0, width = 800, height = 600)

        # Create Frames
        self.frames = {}
        for catagory in TipV:
            f = tk.Frame(self.tabs)
            self.tabs.add(f, text=catagory)
            self.frames[catagory] = f

        self.frame_add_recipes = tk.Frame(self.tabs)
        self.tabs.add(self.frame_add_recipes, text='Add New Recipe')

    def widget_frame_recipes(self):
        # Create Label
        self.lb_add_new_recipe = tk.Label(self.frame_add_recipes, text='Add New Recipe', font='arial 20')
        self.lb_where = tk.Label(self.frame_add_recipes, text='Where do you like to add this recipe?', font='arial 10')

        self.lb_add_new_recipe.place(relx=0.36, rely=0)
        self.lb_where.place(relx=0, rely=0.10)

        # Drop Down Button
        self.Tipvar = tk.StringVar(self.frame_add_recipes)
        self.Tipvar.set("Healthy")
        self.popupMenu = tk.OptionMenu(self.frame_add_recipes, self.Tipvar, *TipV)
        self.popupMenu.place(relx= 0.30, rely= 0.10, relwidth= 0.15, relheight= 0.05)

        btn = tk.Button(self.frame_add_recipes, text="Add", command=self.add)
        btn.pack()

    def add(self):
        selected_frame = self.frames[self.Tipvar.get()]
        lbl = tk.Label(selected_frame, text="you added a recipe")
        lbl.pack()

RecipesScreen()

BTW, I highly recommend you back up and remove any use of place() . Use pack() and / or grid() to layout your widgets. Using place() will make your code very buggy and hard to maintain.

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