-1

I am trying to make a random shell in python... But the problem is i get an error with my code i have tried to remove __commands__[0] from my code but then nothing comes.

My error:

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

my code:

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()
Pymain3664
  • 53
  • 1
  • 8
  • 1
    Hi, perhaps this might be of interest https://stackoverflow.com/questions/216972/what-does-it-mean-if-a-python-object-is-subscriptable-or-not – IronMan Nov 07 '20 at 01:38
  • 1
    When you do `__commands__[0]`, what are you expecting the result to be? Why? – Karl Knechtel Nov 07 '20 at 01:56

1 Answers1

4

Change your set into a list:

__commands__ = ['-v', '-e', '+a', 'DATA']
Red
  • 26,798
  • 7
  • 36
  • 58
  • well your set to list works but what i get still is... my `else:print("NOPE")` no errors but just an not working code. – Pymain3664 Nov 07 '20 at 02:55
  • 1
    @Pymain3664 Hmm... `text` will have to be equal to either `'-'`, `'v'`, `'-v'` or `''` in order for the program to not print `'NOPE'`, right? – Red Nov 07 '20 at 03:09
  • 1
    @Pymain3664 I think its more tkinter error here, you have to say `text = cmd.get("1.0","end-1c")` or else it will include a new line with `get()`, so you have to minus that last character with `end-1c` – Delrius Euphoria Nov 07 '20 at 03:55
  • 1
    @Pymain3664 Oh, I don't know, tbh. – Red Nov 07 '20 at 04:28
  • @Cool Cloud you should turn your comment to an awnser so i can verify it! – Pymain3664 Nov 07 '20 at 13:40
  • @Pymain3664 I'm sorry, but closed questions receive more answers :( – Red Nov 07 '20 at 13:48
  • @Pymain3664 Just use `text = cmd.get("1.0","end-1c")` instead of `text = (cmd.get("1.0","end"))`. Because with `get()` you are getting everything till the `end` which includes a new line character too, there is a new line included there and hence it wont match in your `if` statement, so to get rid of that, youve to use `end-1c` as the end index for `get()`. So the last character is minus(-) with `-1c` (minus 1 character). – Delrius Euphoria Nov 07 '20 at 14:11
  • @Pymain3664 @? - – Red Nov 07 '20 at 23:31