我想让用户为一种食物编写食谱,并选择它将进入哪个选项卡,我如何连接该选项卡,并且食谱将显示在所选选项卡上
代码:
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()
您将需要制作一个字典或其他类型的映射来将字符串与相应的帧相关联。您可以手动执行此操作,但备份并从列表中创建框架会更整洁,以便从创建过程中获取字典
顺便说一句,我强烈建议您备份并删除
place()
的任何用法。使用pack()
和/或grid()
来布局小部件。使用place()
将使您的代码非常有缺陷并且难以维护相关问题 更多 >
编程相关推荐