1

我正在尝试制作一个可以添加无限房间的程序,因此我的所有代码都是围绕使用一个变量来构建的,以推断哪个房间是哪个房间。但是,当我运行它时,它给了我一个错误,它没有直接引用我的代码中的任何一行,而且由于我是一个相当新的程序员,我不知道这意味着什么。此外,我的代码到处都是而且不完整。谢谢你的帮助!

错误

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\SCA0023\AppData\Local\Programs\Python\Python38\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
TypeError: 'NoneType' object is not callable

编码

from tkinter import *

root = Tk()


class Room:
    def __init__(self, items):
        self.objects = []
        self.objects.append(items)

    def list(self):
        print(self.objects)


def addkitchenmenu(r): #add a new option menu attributed to a new room
    globals()[f'kitchenvar_{r}'] = StringVar(root)
    globals()[f'kitchenvar_{r}'].set("Add an appliance")
    globals()[f'kitchenvar_{r}'].trace('w', applianceadd(r))
    kitchenitems = ['Kettle', 'Toaster']
    globals()[f'appliancelist_{r}'] = OptionMenu(root, globals()[f'kitchenvar_{r}'], *kitchenitems).pack()
    addkitchen(r)


def applianceadd(r): #add a new room
    globals()[f'kobjects_{r}'] = []
    globals()[f'kobjects_{r}'].append(globals()[f'kitchenvar_{r}'].get())
    items = globals()[f'kobjects_{r}']
    globals()[f'kroom_{r}'] = Room(items)
    globals()[f'kroom_{r}'].list()


def addkitchen(r): #add an appliance
    globals()[f'addappliace{r}'] = Button(root, text='add appliance', command=lambda: applianceadd(r))


def newkitchencheck(): #find the next name for a room that isn't taken
    varnotfound = True
    a = 0

    while varnotfound:
        if f'kroom{a}' in globals():
            a += 1
        else:
            r = a
            varnotfound = False
            addkitchenmenu(r)


addroombutton = Button(root, text="add kitchen", command=newkitchencheck)
addroombutton.pack()

root.mainloop()
4

1 回答 1

0

您正在将 applianceadd(r) 的结果(无)传递给 .trace()。更改为 .trace("w", lambda *_: applianceaddr(r))。

于 2021-07-30T19:02:24.823 回答