简体   繁体   English

根据下拉菜单的选择更新的 Tkinter 窗口

[英]Tkinter window that updates depending on the selection of the dropdown menu

I want to make a GUI that updates once the choice in the dropdown menu is changed.我想制作一个 GUI,一旦下拉菜单中的选择发生变化,它就会更新。 The code below does this with a button, but the buttons and text that is called is not deleted every time the function is called.下面的代码使用一个按钮来完成此操作,但每次调用该函数时,不会删除所调用的按钮和文本。

Update window without an additional button无需附加按钮的更新窗口

from tkinter import *

app = Tk()

options = [
    "  ",
    "Expences",
    "Birds",
]


clicked = StringVar()
clicked.set(options[0])  # default value


drop = OptionMenu(app, clicked, *options)
drop.pack()


def show():
    click = clicked.get()

    if click == "Expences":
        def Expences():
            b2 = Button(app, text="YESSSS!!!")
            b2.pack()

        Expences()

    elif click == "Birds":
        def Birds():
            a3 = Label(app, text="Maybe")
            a3.pack()

        Birds()

    else:
        return


b1 = Button(app, text = "Click", command = show)
b1.pack()

app.minsize(400, 450)
app.mainloop()

You can use command= in OptionsMenu to run function when it changes selection.您可以在OptionsMenu使用command=在更改选择时运行功能。 Function will get selected value函数将获得选定的值

import tkinter as tk

def show(click):
    if click == "Expences":
        b2 = tk.Button(app, text="YESSSS!!!")
        b2.pack()
    elif click == "Birds":
        a3 = tk.Label(app, text="Maybe")
        a3.pack()

# --- main ---

options = [
    "  ",
    "Expences",
    "Birds",
]

app = tk.Tk()

clicked = tk.StringVar()
clicked.set(options[0])  # default value

drop = tk.OptionMenu(app, clicked, *options, command=show)
drop.pack()

app.mainloop()

You can use trace() with StringVar() to check if it changed value您可以使用trace()StringVar()来检查它是否更改了值

import tkinter as tk

def show(a, b, c):
    click = clicked.get()

    if click == "Expences":
        b2 = tk.Button(app, text="YESSSS!!!")
        b2.pack()
    elif click == "Birds":
        a3 = tk.Label(app, text="Maybe")
        a3.pack()

# --- main ---

options = [
    "  ",
    "Expences",
    "Birds",
]

app = tk.Tk()

clicked = tk.StringVar()
clicked.set(options[0])  # default value
clicked.trace('w', show)

drop = tk.OptionMenu(app, clicked, *options)
drop.pack()

app.mainloop()

Tkinter will not delete previous button or label. Tkinter不会删除上一个按钮或标签。 You would have to do it on your own.你必须自己做。 Or better create label and button at start but don't use pack() to show it .或者更好地在开始时创建标签和按钮,但不要使用pack()来显示它。 Later you can use pack() to show one of them and pack_forget() to remove other one.稍后您可以使用pack()显示其中之一,并使用pack_forget()删除另一个。

import tkinter as tk

def show(a, b, c):

    click = clicked.get()

    if click == "Expences":
        b2.pack()
        a3.pack_forget()
    elif click == "Birds":
        a3.pack()
        b2.pack_forget()

# --- main ---

options = [
    "  ",
    "Expences",
    "Birds",
]

app = tk.Tk()

clicked = tk.StringVar()
clicked.set(options[0])  # default value
clicked.trace('w', show)

drop = tk.OptionMenu(app, clicked, *options)
drop.pack()

b2 = tk.Button(app, text="YESSSS!!!")
a3 = tk.Label(app, text="Maybe")

app.mainloop()

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

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