【问题标题】:Tkinter: How to put user input into a list to access later?Tkinter:如何将用户输入放入列表以供以后访问?
【发布时间】:2018-10-15 18:41:35
【问题描述】:

当我尝试访问由函数 stringToList 生成的列表时,它总是说 TypeError: 'NoneType' object is not subscriptable。

class MyApp(Tk):
    def __init__(self):
        super().__init__()
        # Many widgets defined and placed here
    def my_fx(self):
        def stringToList(input_str):
            number_list = []
            full_list = []
            if list(input_str)[-1].isnumeric:
                input_str += ","

            for item in list(input_str):
                if item.isnumeric():
                    number_list.append(item)
                if not item.isnumeric():
                    num_str = ""
                    for item in number_list:
                        num_str += item
                    if int(num_str) not in full_list:
                        try:
                            full_list.append(int(num_str))
                        except ValueError:
                            pass
                    number_list = []
            sorted_list = sorted(full_list).append(some_string)
            return sorted_list
        if True:
            sorted_list = stringToList(some_entry_widget.get())

        def do_stuff():
            from my_other_file import my_other_fx
            this_and_that = my_other_fx(sorted_list)

    # More functions defined here

if __name__ == '__main__':
    app = IntegratorApp()
    app.mainloop()

my_other_fx 中的这一行(在我的原始代码中正确命名)是我得到 NoneType is not subscriptable 错误的地方:

if sorted_list[-1] == some_value:

我尝试过修改 stringToList,使其使用 IntVar 和 StringVar 而不是常规的 int 和 str,但这并没有改变任何东西。为什么是 NoneType? 另外,如果有人有其他批评,那就太受欢迎了!我正在努力学习!

编辑:当我尝试将字符串附加到列表时,它似乎变成了“不”并变成了 NoneType。但是为什么呢??

【问题讨论】:

  • sorted_list = sorted(full_list).append(some_string) 中,您分配的结果是.append(),而不是sorted() - 那是None。

标签: python tkinter nonetype


【解决方案1】:

试试这个:

full_list.append(some_string)
sorted_list = sorted(full_list)

【讨论】:

    猜你喜欢
    • 2013-04-26
    • 1970-01-01
    • 2020-04-07
    • 1970-01-01
    • 2015-09-17
    • 1970-01-01
    • 2010-12-26
    • 2022-12-15
    • 1970-01-01
    相关资源
    最近更新 更多