Home > Enterprise >  How to add commands only usable after using a certain command in tkinter?
How to add commands only usable after using a certain command in tkinter?

Time:03-08

I am trying to make some sort of command box with tkinter text boxes here is an example

import tkinter as tk

screen = tk.Tk()
screen.geometry('900x700')

texte = tk.Text(screen, height="5")

texte.place(x = 100, y = 0)
texte.pack()

label = tk.Label(screen)
label.pack()


def on_button(event=None): 

    inp = texte.get(1.0, "end-1c")

    if inp in ("/example", "/Example"):
        # change text in existing label
        label['text'] = "Loading Tournament".format(inp)

    else:
        label['text'] = "{0} is not a valid command".format(inp)

btn = tk.Button(screen, text="Enter", command=on_button)
btn.pack()

#texte.bind('<Return>', on_button)

texte.focus()

screen.mainloop()

when I type /example it prints the a command. I want to make it so I can type another command after and only after I type /example but using the same text box.

CodePudding user response:

Your question is somewhat vague about what you want to happen each time a command it typed in, so that part of this is just a guess. As for the main topic of changing what allowable after some other command, I suggest you define a variable to keep track of what processing has occurred which I've named state in the code below. This way of processing input is called using a state machine.

The state variable is an integer and is initialized to 0. As first valid command is entered by the the user, the state variable is assigned the value 1 to indicate that has happened and begin allowing other commands to be entered. In that state the code below will except just about anything at that point although it does check for a special "restart" command to reset the state back to 0 and start things over. You could, of course expand, on this theme and have many more states with associated command as desired.

import tkinter as tk

screen = tk.Tk()
screen.geometry('900x700')

texte = tk.Text(screen, height="5")

texte.place(x=100, y=0)
texte.pack()

label = tk.Label(screen)
label.pack()

state = 0
def on_button(event=None):
    global state

    inp = texte.get(1.0, 'end-1c')

    if state == 0:
        if inp.lower() == "/example":
            label['text'] = f"Loading Tournament {inp}"
            state = 1
        else:
            label['text'] = f"{inp} is not a valid command"
            # Stay in this state...
    elif state == 1:
        if inp.lower() == "restart":
            label['text'] = "restarted"
            state = 0
        else:
            label['text'] = f"processing command {inp}"
            # Stay in this state...

    texte.delete('1.0', 'end')  # Clear.
    texte.mark_set("insert", '1.0')  # Move insertion cursor to beginning.
    return 'break'  # Prevent further processing of event.


btn = tk.Button(screen, text="Enter", command=on_button)
btn.pack()

texte.bind('<Return>', on_button)

texte.focus()

  • Related