简体   繁体   中英

Update the window with a OptionMenu in tkinter

I am looking for a way to change the content of the window based on what option you select in the OptionMenu. It should have 3 different options, namely "Introduction", "Encrypt" and "Decrypt". I've the code to create an OptionMenu but now I wanna know how can I modify them to show a different page, depending on the one who is selected. Could someone help me with that? I am using python 3

so for example:

from tkinter import *

OptionList = [
"Einführung",
"Verschlüsseln",
"Entschlüsseln"
] 


window = Tk()

window.geometry('200x200')

variable = StringVar(window)
variable.set(OptionList[0])

opt = OptionMenu(window, variable, *OptionList)
opt.config(width=90, font=('Calbri', 12))
opt.pack(side="top")


window.mainloop()

This will produce a window with a OptionMenu with the three options I wrote above (just in German) and now I'd like to change the page depending on the current chosen option of the OptionMenu

Thanks guys!

This is now the forth edit or something, but thats the final solution i've come up with.

#coding=utf-

import tkinter as tk
from tkinter import *


window = Tk()

window.geometry('200x200')

OptionList = ["Einführung", "Verschlüsseln", "Entschlüsseln"] 


class App:

    def __init__(self, master):

        self.choice_var = tk.StringVar()
        self.choice_var.set(OptionList[0])

        opt = OptionMenu(window, self.choice_var, *OptionList, command=self.switch)
        opt.config(width=90, font=('Calbri', 12))
        opt.pack(side="top")

        self.random_label1 = tk.Label(window, text="Welcome content here")
        self.random_label2 = tk.Label(window, text="Encrypt content here")
        self.random_label3 = tk.Label(window, text="Decrypt content here")

        self.random_label1.pack()
        self.random_label2.pack()
        self.random_label3.pack()

        self.label_info1 = self.random_label1.pack_info()
        self.label_info2 = self.random_label2.pack_info()
        self.label_info3 = self.random_label3.pack_info()

        self.switch()

    def switch(self, *args):
        var = str(self.choice_var.get())
        if var == "Einführung":
            self.random_label1.pack(self.label_info1)
            self.random_label2.pack_forget()
            self.random_label3.pack_forget()

        if var == "Verschlüsseln":
            self.random_label2.pack(self.label_info2)
            self.random_label1.pack_forget()
            self.random_label3.pack_forget()

        if var == "Entschlüsseln":
            self.random_label3.pack(self.label_info3)
            self.random_label2.pack_forget()
            self.random_label1.pack_forget()


myApp = App(window)
window.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM