2

I am building a Tkinter App that will display 40 entries and my code doesn't look good since I am writing the same 3 lines of code for each Entry.

self.entry_0_1 = GameEntry(self.background, relx=0.15, rely=0.185)
self.entry_0_1.insert(0, "0.0")
self.entry_0_1.config(validate="key", validatecommand=(reg, '%P'))

Custom GameEntry Class for customized Entry widget

class GameEntry(tk.Entry):
     def __init__(self, master=None, state='normal', font='helvetica', bg='white', fg='black', bd=7, selectborderwidth=7,relief="ridge", relx=0, rely=0, relwidth=0.175):
                  super().__init__(master=master, state=state, font=font, bg=bg, fg=fg, bd=bd, selectborderwidth=selectborderwidth, relief=relief)
                  self.place(relx=relx, rely=rely, relwidth=relwidth)

Code below is used for the .config

reg = self.root.register(callback)
def callback(input):
    regex = r"^\d*\.?\d+$"
    if re.match(regex, input):
        return True
    else:
        self.root.bell()
        return False

Code below is the function I wrote to attempted to condense the 3 lines of code that I mentioned at the top of the post

def create_insert_config_entry(root, x, y, insert_input, *args):
    cus_entry = GameEntry(root, relx=x, rely=y)
    cus_entry.insert(0, insert_input)
    cus_entry.config(validate=args[0], validatecommand=(args[1], args[2]))
    return cus_entry

Here is how I tried to use create_insert_config_entry function

self.entry_10_3 = create_insert_config_entry(self.background, 0.575, 0.885, "12.12", ["key", reg, "%P"])

Here is the error I got in the console

File "c:\Users\Mike\Desktop\code\py\main.py", line 71, in <module>
    run = gui.App()
  File "c:\Users\Mike\Desktop\code\py\gui.py", line 249, in __init__
    self.entry_10_3 = create_insert_config_entry(self.background, 0.575, 0.885, "12.12", ["key", reg, "%P"])
  File "c:\Users\Mike\Desktop\code\py\gui.py", line 244, in create_insert_config_entry
    cus_entry = GameEntry(root, relx=x, rely=y)
  File "c:\Users\Mike\Desktop\py\gui.py", line 32, in __init__
    super().__init__(master=master, state=state, font=font, bg=bg, fg=fg, bd=bd, selectborderwidth=selectborderwidth, relief=relief)
  File "C:\Users\Mike\Anaconda3\lib\tkinter\__init__.py", line 3035, in __init__
    Widget.__init__(self, master, 'entry', cnf, kw)
  File "C:\Users\Mike\Anaconda3\lib\tkinter\__init__.py", line 2572, in __init__
    self.tk.call(
_tkinter.TclError: can't invoke "entry" command: application has been destroyed
andrewJames
  • 19,570
  • 8
  • 19
  • 51
whenmoon
  • 21
  • 1
  • Please try to create a [mcve] that is a single block of code. I don't think we can piece this code together to get this error. This error is saying that the root window has been destroyed and then you try to create an entry widget. – Bryan Oakley Jul 16 '22 at 20:19
  • `self.entry_10_3 = create_insert_config_entry(self.background, 0.575, 0.885, "12.12", ["key", reg, "%P"])` will raise exception `IndexError: tuple index out of range`. It should be `self.entry_10_3 = create_insert_config_entry(self.background, 0.575, 0.885, "12.12", "key", reg, "%P")` instead. – acw1668 Jul 17 '22 at 00:55

0 Answers0