python - 在 for 循环中定义函数

标签 python python-3.x tkinter

我对 Python 和 tkinter 比较陌生。我正在尝试为列表中的每个项目创建一个按钮。

from tkinter import *

x = ['d', 'f']

class GUI:

    def __init__(self, master):

        self.master = master
        master.title("window")

        self.label = Label(master, text="Window")
        self.label.grid(columnspan=5, sticky=W+E)

        self.close_button = Button(master, text="Close", command=master.destroy)
        self.close_button.grid(columnspan=5, sticky=W+E)

        for s in x:
            self.s_button = Button(master, text=s, command=self.s)
            self.s_button.grid(columnspan=5, sticky=W+E)

    for a in x:
        def a(self):
            print (a)     

root = Tk()
gui = GUI(root)
root.mainloop()

每当我运行代码时,我都会收到错误 AttributeError: 'GUI' object has no attribute 's'

最佳答案

第一个错误:command=self.s 查找名为“s”的属性。如果要查找名称为 s 变量内容的属性,则应编写 command=getattr(self, s)

第二个错误:def a(self)定义了一个名为“a”的类方法。如果您想以编程方式生成命名方法,您可以在创建类之后执行此操作,例如:

for a in x:
    def fun(self):
        print (a)
    setattr(GUI, a, fun)

编辑:这仍然是错误的,因为两个函数使用相同的变量a

无论如何,以这种方式生成方法看起来很奇怪,我宁愿有一个将要打印的内容作为参数的方法,例如:

from tkinter import *

x = ['d', 'f']

class GUI:
    def __init__(self, master):
        self.master = master
        master.title("window")

        self.label = Label(master, text="Window")
        self.label.grid(columnspan=5, sticky=W+E)

        self.close_button = Button(master, text="Close", command=master.destroy)
        self.close_button.grid(columnspan=5, sticky=W+E)

        for s in x:
            self.s_button = Button(master, text=s, command=(lambda: self.print_(s)))
            self.s_button.grid(columnspan=5, sticky=W+E)

    def print_(self, a):
        print (a)

root = Tk()
gui = GUI(root)
root.mainloop()

编辑:这仍然是错误的,因为 s 变量是共享的,因为 for 循环不会创建新的作用域。只有函数才能创建新的作用域。这是一些工作代码:

from tkinter import *

x = ['d', 'f']

class GUI:
    def __init__(self, master):
        self.master = master
        master.title("window")

        self.label = Label(master, text="Window")
        self.label.grid(columnspan=5, sticky=W+E)

        self.close_button = Button(master, text="Close", command=master.destroy)
        self.close_button.grid(columnspan=5, sticky=W+E)

        for s in x:
            self.s_button = Button(master, text=s, command=self.make_printer(s))
            self.s_button.grid(columnspan=5, sticky=W+E)

    def make_printer(self, a):
        def fun():
            print (a)
        return fun

root = Tk()
gui = GUI(root)
root.mainloop()

关于python - 在 for 循环中定义函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42590461/

相关文章:

python - 无法重复迭代 "csv.reader"- 第二次迭代的结果为空

python - 如何使 Tkinter 支持 PNG 透明度?

python - 如何在 python tkinter 中停止此函数

python - 具有自定义路径的 Pip、Git 和 ssh key

python - Robot Framework 中的 tail 日志文件

python - 属性错误 : 'Cycler' object has no attribute 'change_key'

python - 尝试使用 urllib.retrieve : 'module' object has no attribute 'retrieve' 通过 URL 下载文件

python - 如何让蛇超出屏幕后,它会回到屏幕的另一边

python - 用 python 保存 MS ACCESS 附件

Python Tkinter 文本背景