【问题标题】:TypeError: 'set' object is not subscriptable? (tkinter) [duplicate]TypeError:'set'对象不可下标? (tkinter)[重复]
【发布时间】:2021-02-19 17:15:11
【问题描述】:

我正在尝试在 python 中创建一个随机 shell...但问题是我的代码出现错误我试图从我的代码中删除 __commands__[0] 但没有任何结果。

我的错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "C:\Users\User1\Downloads\Stuff\untitled2.py", line 14, in commands
    if text in __commands__[0]:
TypeError: 'set' object is not subscriptable

我的代码:

from tkinter import *

def tk_shell(Name=None):
    def commands():
        __commands__ = {'-v',
                           '-e',
                           '+a',
                           'DATA'}
        
        
        text = (cmd.get("1.0","end"))
        
        
        if text in __commands__[0]:
            print("version 1.11")
        
        else:
            print("NOPE")
        
        
    tk = Tk()
    tk.resizable(width=False, height=False)
    tk.geometry('700x490')
    
    tk.title('Server Prompt-'+str(Name))
    
    #main
    cmd = Text(tk,bg='black',fg='#00FF00')
    cmd.config(height=600,width=480)
    cmd.pack()
    
    
    r_button = Button(tk,width=12,height=2,text='Run',bg='#00FF00',command=commands)
    r_button.place(x=610,y=0)
    
    
    
    
    tk.mainloop()

tk_shell()

【问题讨论】:

标签: python tkinter


【解决方案1】:

将您的集合更改为列表:

__commands__ = ['-v', '-e', '+a', 'DATA']

【讨论】:

  • 好吧,您列出的设置有效,但我得到的仍然是......我的else:print("NOPE") 没有错误,只是一个不工作的代码。
  • @Pymain3664 嗯...text 必须等于'-''v''-v''',以便程序不打印'NOPE'对吧?
  • @Pymain3664 我认为这里的 tkinter 错误更多,你必须说 text = cmd.get("1.0","end-1c") 否则它将包含一个带有 get() 的新行,所以你必须用 end-1c 减去最后一个字符
  • @Pymain3664 哦,我不知道,tbh。
  • @Cool Cloud 你应该把你的评论变成一个遮阳篷,这样我就可以验证它了!
猜你喜欢
  • 1970-01-01
  • 2018-03-08
  • 2014-02-26
  • 2015-02-27
  • 1970-01-01
  • 2015-12-18
  • 2020-03-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多