用tkinter中的OptionMenu更新窗口

问题描述 投票:0回答:1

我正在寻找一种基于您在OptionMenu中选择的选项来更改窗口内容的方法。它应该具有3个不同的选项,即“简介”,“加密”和“解密”。我已经创建了OptionMenu的代码,但是现在我想知道如何修改它们以显示不同的页面,具体取决于所选择的页面。有人可以帮我吗?我正在使用python 3

例如:

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()

这将产生一个带有OptionMenu的窗口,其中包含我上面写的三个选项(仅德语),现在我想根据OptionMenu当前选择的选项来更改页面

谢谢你们!

python tkinter window optionmenu
1个回答
0
投票

尽管这可能不是很有帮助,但我还是试图找到可行的方法。我还没有成功,但是到目前为止,当您查看我“编造”的代码时,也许您会遇到Geistesblitz(想法火花或对英语使用者来说有些东西:D):

#coding=utf-

import tkinter as tk
from tkinter import *
import time

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.func_call)
        opt.config(width=90, font=('Calbri', 12))
        opt.pack(side="top")

        self.random_label1 = Label(window, text="lol")
        self.random_label1.pack()
        self.random_label2 = Label(window, text="lel")
        self.random_label2.pack()
        self.random_label3 = Label(window, text="lul")
        self.random_label3.pack()
        self.label_info1 = self.random_label1.place_info()
        self.label_info2 = self.random_label2.place_info()
        self.label_info3 = self.random_label3.place_info()
        self.func_call()
        self.EF()
        self.EF_reverse()  # you gotta specify everything in the __init__
        self.VS()
        self.VS_reverse()
        self.ES()
        self.ES_reverse()


    def EF(self): # EF is short for Einführung
        print("jaaa") #testing
        self.random_label1.place(self.label_info1) # this SHOULD place the label 

    def EF_reverse(self): # while this one should hide it. which doesnt work
        print("neeein") #testing
        self.random_label1.place_forget()

# the following defs are just filled with pass so that python doesnt get a stroke
    def VS(self): 
        pass

    def VS_reverse(self):
        pass

    def ES(self):
        pass

    def ES_reverse(self):
        pass
# this should technically call all the needed func's but somehow it doesnt
#also, due to the class, they all get called once at the startup

    def func_call(self, *args):
        var = str(self.choice_var.get())
        if var == "Einführung":
            print(var)
            self.EF()
            self.VS_reverse()
            self.ES_reverse()
        elif var == "Verschlüsseln":
            print(var)
            self.EF_reverse()
            self.VS()
            self.ES_reverse()
        elif var == "Entschlüsseln":
            print(var)
            self.EF_reverse()
            self.VS_reverse()
            self.ES()


myApp = App(window)
window.mainloop()

这几乎是代码,即时通讯我意识到这一点,但我只是认为我给出了某种回应。如果有人有其他建议,请分享给他们:D

© www.soinside.com 2019 - 2024. All rights reserved.