Functions in Tkinter

元气小坏坏 提交于 2021-02-11 12:33:30

问题


So I am practicing using Tkinter with python, and I am just trying to learn the basics. My code right now is

import Tkinter as tk

class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        self.prompt = tk.Label(self, text="Press a button", anchor="w")
        self.button1 = tk.Button(self, text="Button 1", command = self.button1)
        self.button2 = tk.Button(self, text="Button 2", command = self.button2)
        self.output = tk.Label(self, text="")

        # lay the widgets out on the screen. 
        self.prompt.pack(side="top", fill="x")
        self.output.pack(side="top", fill="x", expand=True)
        self.button1.pack(side="left")
        self.button2.pack(side="right")

    def button1(self):
        result = "You just pressed button 1."
        self.output.configure(text=result)
    def button2(self):
        result = "You just pressed button 2."
        self.output.configure(text=result)

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()

This works fine, but I am trying to make it a little cleaner and only use one function. I tried this:

def button(self, string):
    result = string
    self.output.configure(text=result)

And on the buttons, I used

self.button1 = tk.Button(self, text="Button 1", command = self.button(
"You just pressed button 1"))
self.button2 = tk.Button(self, text="Button 2", command = self.button(
"You just pressed button 2"))

For some reason though, when I add the second argument to the button function, it stops working. If I use the exact same code, it works fine, but when I add a second argument I get this error:

line 31, in button
    self.output.configure(text=result)
AttributeError: Example instance has no attribute 'output'

What is the problem?


回答1:


The problem is that the command attribute expects a reference to a function. When you do command=self.button(...), you are immediately calling the function and then using the result of the function as the value for the command attribute.

If you want to pass arguments, you need to use lambda or functools.partial. This question has been asked many times on this site. See, for example, Calling functions with arguments on "command" and "bind" and python Tkinter: passing an argument to a function



来源:https://stackoverflow.com/questions/22263832/functions-in-tkinter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!