简体   繁体   English

如何使用 tkinter 中的下拉按钮连接笔记本

[英]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?我希望用户编写食物的食谱和 select 哪个选项卡将 go 在哪个选项卡上,我如何连接它并且食谱会出现在所选选项卡上?

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() .顺便说一句,我强烈建议您备份并删除对place()的任何使用。 Use pack() and / or grid() to layout your widgets.使用pack()和/或grid()来布局您的小部件。 Using place() will make your code very buggy and hard to maintain.使用place()将使您的代码非常错误且难以维护。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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