本文介绍了Tkinter列表框如何判断是否选择了一个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个允许用户按下按钮的简单 GUI,这将从显示的列表框中删除一个条目.但是,如果未选择任何条目,则控制台将引发错误,因此我将如何确定用户是否选择了条目.这是我的代码:

I am trying to create a simple GUI that allows the user to press a button, which will delete an entry from a shown Listbox. However, the console throws an error if no entry is selected, so how would I determine if the user has selected an entry. Here's my code:

selection = self.recipe_list.curselection()
    if not selection is None:
        self.recipe_list.delete(selection)
    else:
        print("Nothing to delete!")

推荐答案

而不是像您要检查的那样返回 None ,它返回一个空字符串," .检查是否如下:

Instead of returning None like you're checking for, it returns an empty string, "". Check for that as follows:

if selection:
    self.recipe_list.delete(selection)
else:
    print("Nothing to delete!")

这篇关于Tkinter列表框如何判断是否选择了一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 06:33