Home > Blockchain >  Tkinter: entry.configure(state="normal") not workinfg in my program
Tkinter: entry.configure(state="normal") not workinfg in my program

Time:05-07

I am beginner and I'm trying to make a Wordle game in python to practice. enter image description here

Each character is in a different entry, so I made it so that only the focused entry is enabled and the four other are disabled. I created a "Backspace" button to delete the last character that the user typed. This button is supposed to enable the entry of the character that the use want to delete, then deletes the character, then focus this entry, then finally disable the entry that was previously focused.

The problem is that whenever I want to use the backspace button it doesn't enable the entry, it just deletes its content and disable the entry that is after the one where the character has been deleted.

Video of the problem

Here's the code:

from tkinter import *
from tkinter import ttk
import tkinter as tk
def callback1(var, index, mode):
    root.box2.configure(state="normal")
    root.box2.focus()
    root.box1.configure(state="disabled")
    root.hasfocus = "box2"

def callback2(var, index, mode):
    root.box3.configure(state="normal")
    root.box3.focus()
    root.box2.configure(state="disabled")
    root.hasfocus = "box3"

def callback3(var, index, mode):
    root.box4.configure(state="normal")
    root.box4.focus()
    root.box3.configure(state="disabled")
    root.hasfocus = "box4"

def callback4(var, index, mode):
    root.box5.configure(state="normal")
    root.box5.focus()
    root.box4.configure(state="disabled")
    root.hasfocus = "box5"
    
class root(Tk):
    def __init__(self):
        super(root, self).__init__()
        
        self.title("Wordle")
        self.minsize(530,400)
        self.configure(bg='#121213')

        self.hasfocus = ""

        self.createBoxes()
        self.createEntry()
        self.button = Button(self, text="Backspace", width=8, font ="Calibri 18", command=self.backspace)
        self.button.place(x=420, y=31)

    def createBoxes(self):
        b=0 ; d=0
        while b<6:
            a=0 ; c=0
            while a<5:
                tk.Label(height=2, width=5, bg="white").place(x=115 c, y=30 d)
                a=a 1
                c=c 60
            d=d 50 ; b=b 1

    def backspace(self):
        if self.hasfocus == "box1":
            pass
        elif self.hasfocus == "box2":
            self.box1.configure(state="normal")
            self.box1.delete(0, 'end')
            self.box1.focus()
            self.hasfocus="box1"
            self.box2.configure(state="disabled")
        elif self.hasfocus == "box3":
            self.box2.configure(state="normal")
            self.box2.delete(0, 'end')
            self.box2.focus()
            self.hasfocus="box2"
            self.box3.configure(state="disabled")
        elif self.hasfocus == "box4":
            self.box3.configure(state="normal")
            self.box3.delete(0, 'end')
            self.box3.focus()
            self.hasfocus="box3"
            self.box4.configure(state="disabled")
        elif self.hasfocus == "box5":
            if len(str(self.name5.get())) > 0:
                self.box5.delete(0, 'end')
            else:
                self.box4.configure(state="normal")
                self.box4.delete(0, 'end')
                self.box4.focus()
                self.hasfocus="box4"
                self.box5.configure(state="disabled")
        

    def createEntry(self):
        self.name1 = StringVar()
        self.box1 = ttk.Entry(self, width=2, textvariable = self.name1, font="Calibri 15")
        self.box1.place(x=128, y=31)

        self.name2 = StringVar()
        self.box2 = ttk.Entry(self, width=2, textvariable = self.name2, font="Calibri 15")
        self.box2.place(x=188, y=31)
        self.box2.configure(state="disabled")

        self.name3 = StringVar()
        self.box3 = ttk.Entry(self, width=2, textvariable = self.name3, font="Calibri 15")
        self.box3.place(x=248, y=31)
        self.box3.configure(state="disabled")

        self.name4 = StringVar()
        self.box4 = ttk.Entry(self, width=2, textvariable = self.name4, font="Calibri 15")
        self.box4.place(x=308, y=31)
        self.box4.configure(state="disabled")

        self.name5 = StringVar()
        self.box5 = ttk.Entry(self, width=2, textvariable = self.name5, font="Calibri 15")
        self.box5.place(x=368, y=31)
        self.box5.configure(state="disabled")

        self.box1.focus() ; hasfocus = "box1"
        self.name1.trace_add("write", callback1)
        self.name2.trace_add("write", callback2)
        self.name3.trace_add("write", callback3)
        self.name4.trace_add("write", callback4)


        
root=root()
root.mainloop()

CodePudding user response:

Solution from @furas in comments:

state="normal" works correctly but when you click button backspace and you delete value in Entry then it automatically changes value in StringVar and this automatically runs callback (assigned by trace_add) which moves to next Entry and this callback sets state="disabled" for deleted Entry. You would have to turn off callback, or in callback you would have to check if it adds or deletes value and skip state="disabled"

  • Related