Python Tkinter 两个按钮的一个回调函数 [英] Python Tkinter one callback function for two buttons

查看:50
本文介绍了Python Tkinter 两个按钮的一个回调函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找这个问题的答案很长时间,但仍然没有找到任何东西.我正在使用 Tkinter 创建一个 GUI,并且我有两个按钮,除了它们从不同的小部件接收信息外,它们的作用大致相同.一个按钮用于 Entry 小部件,另一个按钮用于 Listbox 小部件.这两个按钮的回调函数很长(大约200行),所以我不想每个按钮都有单独的函数.我在这个回调函数的开头有if语句来检查点击了哪个按钮,然后代码会取相应的值.但我不确定以下代码是否显示了正确的方法,因为显然它在我的程序中不能完美运行.回调函数仅在第一次起作用,如果我单击另一个按钮,我将收到错误消息.这是我创建的示例代码来说明这个想法.请注意,我想检查按钮是否被点击,我不想检查值"是否存在.请帮忙.

I have been looking around for a long time for answers to this question but still hasn't find anything. I am creating a GUI using Tkinter, and I have two buttons that do mostly the same thing except they receive information from different widgets. One button is for an Entry widget and the other is for a Listbox widget. The callback function for these two buttons is long (about 200 lines), so I don't want to have separate functions for each button. I have if-statements in the beginning of this callback function to check which button is clicked, and then the codes will take the corresponding value. But I am not sure if the following code shows the right way to do this because apparently it doesn't work perfectly in my program. The callback function would only work for the first time, and if I click the other button I will receive an error. Here is a sample code that I created to illustrate the idea. Note that I want to check if the button is clicked, I do not want to check if the 'value' exists. Please help.

from Tkinter import *

root = Tk()

def DoSomething():
    # is this the right way to check which button is clicked?
    if button1:
        value = user_input.get()
    elif button2:
        value = choice.get(choice.curselection()[0])

    # then more codes that take 'value' as input.


button1 = Button(master,text='Search',command=DoSomething)
button1.pack()
button2 = Button(master,text='Search',command=DoSomething)
button2.pack()

user_input = Entry(master)
user_input.pack()
choice = Listbox(master,selectmode=SINGLE)
choice.pack()
#assume there are items in the listbox, I skipped this portion

root.mainloop()

推荐答案

如果你想将实际的小部件传递给回调,你可以这样做:

If you want to pass the actual widget into the callback, you can do it like this:

button1 = Button(master, text='Search')
button1.configure(command=lambda widget=button1: DoSomething(widget))
button2 = Button(master, text='Search')
button2.configure(command=lambda widget=button2: DoSomething(widget))

如果您真的不需要对小部件的引用,另一种选择是简单地传入一个文字字符串:

Another choice is to simply pass in a literal string if you don't really need a reference to the widget:

button1 = Button(..., command=lambda widget="button1": DoSomething(widget))
button2 = Button(..., command=lambda widget="button2": DoSomething(widget))

另一种选择是给每个按钮一个唯一的回调,让这个回调只做那个按钮特有的事情:

Another choice is to give each button a unique callback, and have that callback do only the thing that is unique to that button:

button1 = Button(..., command=ButtonOneCallback)
button2 = Button(..., command=ButtonTwoCallback)

def ButtonOneCallback():
    value = user_input.get()
    DoSomething(value)

def ButtonTwoCallback():
    value=choice.get(choice.curselection()[0])
    DoSomething(value)

def DoSomething(value):
    ...

还有其他方法可以解决同样的问题,但希望这能让您大致了解如何将值传递给按钮回调,或者首先如何避免需要这样做.

There are other ways to solve the same problem, but hopefully this will give you the general idea of how to pass values to a button callback, or how you can avoid needing to do that in the first place.

这篇关于Python Tkinter 两个按钮的一个回调函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆