有多个命令时,按钮在Tkinter的pssed与Python 2.7 $ P $ [英] Have multiple commands when button is pressed in Tkinter with python 2.7

查看:533
本文介绍了有多个命令时,按钮在Tkinter的pssed与Python 2.7 $ P $的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要运行多个功能,当我点击一个按钮。例如,我希望我的按钮,看起来像

I want to run multiple functions when I click a button. For example I want my button to look like

self.testButton = Button(self, text = "test", 
                         command = func1(), command = func2())

当我执行该语句,我得到一个错误,因为我不能分配东西的参数两次。我怎样才能使命令执行多种功能。

when I execute this statement I get an error because I cannot allocate something to an argument twice. How can I make command execute multiple functions.

推荐答案

您可以创建功能相结合的一个泛型函数,它可能是这个样子:

You could create a generic function for combining functions, it might look something like this:

def combine_funcs(*funcs):
    def combined_func(*args, **kwargs):
        for f in funcs:
            f(*args, **kwargs)
    return combined_func

然后,你可以这样创建按钮:

Then you could create your button like this:

self.testButton = Button(self, text = "test", 
                         command = combine_funcs(func1, func2))

这篇关于有多个命令时,按钮在Tkinter的pssed与Python 2.7 $ P $的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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