我如何在tkinter中使用下拉按钮插入笔记本

2024-04-20 07:51:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我想让用户为一种食物编写食谱,并选择它将进入哪个选项卡,我如何连接该选项卡,并且食谱将显示在所选选项卡上

代码:

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()

1条回答
网友
1楼 · 发布于 2024-04-20 07:51:53

您将需要制作一个字典或其他类型的映射来将字符串与相应的帧相关联。您可以手动执行此操作,但备份并从列表中创建框架会更整洁,以便从创建过程中获取字典

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()

顺便说一句,我强烈建议您备份并删除place()的任何用法。使用pack()和/或grid()来布局小部件。使用place()将使您的代码非常有缺陷并且难以维护

相关问题 更多 >