基于Tkinter OptionMenu实现窗口内容动态切换的技术求助
Got it, let's implement content switching using Frames—each frame will act as a distinct "page" for your three options, and we'll toggle their visibility when the OptionMenu selection changes. Here's the modified code with clear explanations:
from tkinter import * def switch_page(*args): # First, hide all frames to avoid overlapping content for frame in [intro_frame, encrypt_frame, decrypt_frame]: frame.pack_forget() # Show the frame that matches the selected option selected = variable.get() if selected == "Einführung": intro_frame.pack(fill="both", expand=True) elif selected == "Verschlüsseln": encrypt_frame.pack(fill="both", expand=True) elif selected == "Entschlüsseln": decrypt_frame.pack(fill="both", expand=True) # Initialize main window window = Tk() window.geometry('400x300') window.title("Verschlüsselungs-Tool") # Set up OptionMenu OptionList = [ "Einführung", "Verschlüsseln", "Entschlüsseln" ] variable = StringVar(window) variable.set(OptionList[0]) # Default to first option # Listen for changes to the selected option variable.trace_add("write", switch_page) opt = OptionMenu(window, variable, *OptionList) opt.config(width=90, font=('Calibri', 12)) opt.pack(side="top", padx=10, pady=10) # Create frames for each page # Introduction page intro_frame = Frame(window) Label(intro_frame, text="Willkommen!", font=('Calibri', 14)).pack(pady=20) Label(intro_frame, text="Wählen Sie eine Option oben, um fortzufahren.", font=('Calibri', 12)).pack() # Encrypt page encrypt_frame = Frame(window) Label(encrypt_frame, text="Text verschlüsseln", font=('Calibri', 14)).pack(pady=10) Label(encrypt_frame, text="Ihr Text:").pack(pady=5) encrypt_input = Entry(encrypt_frame, width=40) encrypt_input.pack(pady=5) Button(encrypt_frame, text="Verschlüsseln", command=lambda: print("Verschlüsselter Text:", encrypt_input.get())).pack(pady=10) # Decrypt page decrypt_frame = Frame(window) Label(decrypt_frame, text="Text entschlüsseln", font=('Calibri', 14)).pack(pady=10) Label(decrypt_frame, text="Verschlüsselter Text:").pack(pady=5) decrypt_input = Entry(decrypt_frame, width=40) decrypt_input.pack(pady=5) Button(decrypt_frame, text="Entschlüsseln", command=lambda: print("Entschlüsselter Text:", decrypt_input.get())).pack(pady=10) # Show the initial page intro_frame.pack(fill="both", expand=True) window.mainloop()
How It Works:
- Frames as Pages: Each option gets its own
Framethat holds all the UI elements (labels, inputs, buttons) for that function. This keeps your code clean and makes switching straightforward. - Variable Trace: The
variable.trace_add("write", switch_page)line listens for changes to the OptionMenu's selected value. Every time the user picks a new option, it triggers theswitch_pagefunction. - Toggle Visibility: The
switch_pagefunction first hides all frames withpack_forget(), then shows only the frame that matches the selected option usingpack(). Thefill="both", expand=Trueensures each page takes up the full window space when active.
You can easily customize each frame's content—add more widgets, styling, or your actual encryption/decryption logic—to fit your needs!
内容的提问来源于stack exchange,提问作者Dimilo
相关产品推荐
相关产品推荐

