对于两个按钮的Python Tkinter的一个回叫功能 [英] Python Tkinter one callback function for two buttons

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

问题描述

我一直在寻找了很长一段时间,这个问题的答案,但仍然没有发现任何东西。我创建使用Tkinter的一个GUI,我有两个按钮,除了他们收到来自不同部件的信息大多是做同样的事情。一个按钮是用于输入构件,另一个是一个列表框控件。
这两个按钮的回调函数是长(约200行),所以我不想为每个按键独立的功能。我有if语句在这个回调函数的开始,检查被点击的按钮,然后在codeS将采取相应的值。但我不知道,如果以下code表明要做到这一点,因为显然它并不完全在我的程序工作的正确途径。回调函数只在第一次工作,如果我点击另一个按钮,我会收到一个错误。这里是我创造来说明这个想法样本code。请注意,我要检查,如果单击该按钮,我不想检查,如果价值的存在。请帮助。

 从Tkinter的进口*根= TK()高清DoSomething的():
    #这是检查被点击的按钮的正确方法?
    如果BUTTON1:
        值= user_input.get()
    ELIF按钮2:
        值= choice.get(choice.curselection()[0])    #那么更多的codeS是采取价值作为输入。
按钮1 =按钮(主,文字='搜索',命令= DoSomething的)
button1.pack()
按钮2 =按钮(主,文字='搜索',命令= DoSomething的)
button2.pack()USER_INPUT =条目(主)
user_input.pack()
选择=列表框(主,selectmode =单)
choice.pack()
#assume有ListBox中的项目,我跳过这部分root.mainloop()


解决方案

如果你想实际构件传递到回调,你可以做这样的:

 按钮1 =按钮(主,文本=搜索)
button1.configure(命令=拉姆达部件=按钮1:DoSomething的(部件))
按钮2 =按钮(主,文本=搜索)
button2.configure(命令=拉姆达部件=按钮2:DoSomething的(部件))

另一种选择是简单地在一个字符串传递,如果你并不真的需要到窗口小部件的引用:

 按钮1 =按钮(...,命令=拉姆达部件=按钮1:DoSomething的(部件))
按钮2 =按钮(...,命令=拉姆达部件=按钮2:DoSomething的(部件))

另一个选择是给每个按钮一个独特的回调,并有回调只做这是唯一的那个按钮的东西:

 按钮1 =按钮(...,命令= ButtonOneCallback)
按钮2 =按钮(...,命令= ButtonTwoCallback)高清ButtonOneCallback():
    值= user_input.get()
    DoSomething的(值)高清ButtonTwoCallback():
    值= choice.get(choice.curselection()[0])
    DoSomething的(值)高清DoSomething的(值):
    ...

有其他的方法来解决同样的问题,但希望这会给你如何值传递到一个按钮的回调,或者你如何避免需要做摆在首位的总体思路。

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天全站免登陆