简体   繁体   English

使用 tkinter 中的 OptionMenu 更新 window

[英]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.我正在寻找一种方法来更改 window 的内容,基于您在 OptionMenu 中的 select 选项。 It should have 3 different options, namely "Introduction", "Encrypt" and "Decrypt".它应该有 3 个不同的选项,即“介绍”、“加密”和“解密”。 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.我有创建 OptionMenu 的代码,但现在我想知道如何修改它们以显示不同的页面,具体取决于所选的页面。 Could someone help me with that?有人可以帮我吗? I am using python 3我正在使用 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这将产生一个带有 OptionMenu 的 window 和我上面写的三个选项(只是用德语),现在我想根据 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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