[英]How can i interligate a notebook with a drop down button in tkinter
我希望用戶編寫食物的食譜和 select 哪個選項卡將 go 在哪個選項卡上,我如何連接它並且食譜會出現在所選選項卡上?
代碼:
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()
您將需要制作字典或其他類型的映射以將字符串與相應的框架相關聯。 您可以手動執行此操作,但從列表中備份和創建框架會更整潔,以便您從創建過程中獲取字典。
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()
將使您的代碼非常錯誤且難以維護。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.