You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Tkinter OptionMenu实现窗口内容动态切换的技术求助

Solution for Tkinter OptionMenu Content Switching

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 Frame that 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 the switch_page function.
  • Toggle Visibility: The switch_page function first hides all frames with pack_forget(), then shows only the frame that matches the selected option using pack(). The fill="both", expand=True ensures 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:27:15