tkinter中的按钮命令选项 [英] button command option in tkinter

查看:32
本文介绍了tkinter中的按钮命令选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的小GUI应用程序中.当我使用按钮的命令选项来调用函数时.它不是这样的: self.update(),而是它是这样的: self.update .为什么这样?按钮的命令选项是某些特殊的工作方式吗?我认为应该使用大括号()调用方法或函数,除非它是属性:

In the little GUI app below. When I use button's command option to call a function. It doesn't work like this: self.update() rather it works like this: self.update. Why so? Is is some special way that command option of a button works? I think a method or a function should be called with those braces (), unless it's a property:

     @name.setter:
     def setter(self, name): 
             self.name = name

     #main 
     object.name = "New_obj"

注意:以上只是一个模板,所以您可能会明白我的意思.我没有写完整的有效代码.包括课堂和一切.

Note: The above is just a template so you might get my point. I didn't write the complete valid code. Including class and everything.

from tkinter import *

class MuchMore(Frame):
    def __init__(self, master):
        super(MuchMore,self).__init__(master)
        self.count =0 
        self.grid()
        self.widgets()

    def widgets(self):
        self.bttn1 = Button(self, text = "OK")
        self.bttn1.configure(text = "Total clicks: 0")
        self.bttn1["command"] = self.update    # This is what I am taking about 
        self.bttn1.grid() 

    def update(self):
        self.count += 1
        self.bttn1["text"] = "Total clicks" + str(self.count)


#main

root = Tk()
root.title("Much More")
root.geometry("324x454")
app = MuchMore(root)

推荐答案

这是一个高阶函数,表示您将函数作为对象引用.您调用函数并将命令分配给函数的返回值.有关更多信息,请参见此处.

It is a high order function, meaning you are referencing a function as an object. You are not calling the function and assigning the command to the return value of the function. See here for more information.

这篇关于tkinter中的按钮命令选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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