简体   繁体   English

如何根据 tkinter 的下拉菜单结果使相同的按钮执行不同的操作?

[英]How to make same button do different actions depending on dropdown menu result with tkinter?

is there a way to make the same button do different actions depending on what the user chooses from drop down menu?有没有办法让同一个按钮根据用户从下拉菜单中选择的内容执行不同的操作?

I tried this for example, but it doesn't work:例如,我尝试了这个,但它不起作用:

from tkinter import *
from tkinter import ttk

root = Tk()
root.geometry("400x200")

def say_hello():
    hello_label = Label(root, text= "Hello")
    hello_label.grid(row=1, column=0, padx=10, pady=10)

def say_goodbye():
    bye_label = Label(root, text= "Goodbye")
    bye_label.grid(row=1, column=0, padx=10, pady=10)

do = ""
drop_table = ttk.Combobox(root, values=["Choose Action..", "Say Hello","Say Goodbye"])
drop_table.current(0)
drop_table.grid(row=0, column=0, padx=10, pady=10)

selected = drop_table.get()

if selected == "Say Hello":
    do = say_hello
if selected == "Say Goodbye":
    do = say_goodbye

button = Button(root, text= "Choose", command = do)
button.grid(row=0, column=2, padx=10, pady=10)

root.mainloop()

You would need to link your button to a function and include your if statements in the function.您需要将按钮链接到函数并将 if 语句包含在函数中。 Then use your function as the command for your button like this.然后像这样使用你的函数作为你的按钮的命令。

from tkinter import *
from tkinter import ttk

root = Tk()
root.geometry("400x200")

def say_hello():
    hello_label = Label(root, text= "Hello")
    hello_label.grid(row=1, column=0, padx=10, pady=10)

def say_goodbye():
    bye_label = Label(root, text= "Goodbye")
    bye_label.grid(row=1, column=0, padx=10, pady=10)

do = ""
drop_table = ttk.Combobox(root, values=["Choose Action..", "Say Hello","Say Goodbye"])
drop_table.current(0)
drop_table.grid(row=0, column=0, padx=10, pady=10)


def action():
    selected = drop_table.get()
    if selected == "Say Hello":
        say_hello()
    if selected == "Say Goodbye":
        say_goodbye()
        

button = Button(root, text= "Choose", command= action)
button.grid(row=0, column=2, padx=10, pady=10)

root.mainloop()

暂无
暂无

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

相关问题 将Python与Tkinter结合使用,如何根据选项菜单中选择的选项,使按钮按下做不同的事情? - Using Python with Tkinter, how can I make a button press do a different thing depending on which option is selected in the option menu? 如何根据文件类型使 tkinter 按钮调用两个不同的 function? - How to make tkinter button call two different function depending on filetype? 根据下拉菜单的选择更新的 Tkinter 窗口 - Tkinter window that updates depending on the selection of the dropdown menu Python tkinter:创建动态下拉菜单,并在选择后调用不同的操作 - Python tkinter: create a dynamic dropdown menu and call different actions after selection 如何根据 Python 中的值使用不同的操作进行列表迭代 - How to make a list iteration with different actions depending on the value in Python Tkinter - 点击下拉菜单按钮后如何更改文本? - Tkinter - How to change text after hitting dropdown menu button? 如何为不同的标签创建相同的下拉菜单? - How to create a same dropdown menu for different labels? tkinter:选择某个下拉值值时,如何使按钮做别的? - tkinter: When a certain dropdown value is selected, how to make a Button do something else? 如何在 tkinter 中制作具有不同数字的按钮 - how to make a button with a different number in tkinter 如何制作一个按钮,允许我将两个变量发送到 Tkinter 中的同一个函数中? - How do I make a button that allows me to send two variables into the same function in Tkinter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM