0

I am making a window in tkinter that requires additional input for a name. To get this input I created a special messagebox that gets the input and has a button to confirm the input in the entry. I need to then get that variable from the class as the return value. The problem is that I do not know how to get such a value when the user could take however long they wish. What should I do? (The value I need to return is extension_name)

My current code:

class ExtensionNamer(Toplevel):
    def __init__(self, master, x, y, *args, **kwargs):
        #Initialize the window
        Toplevel.__init__(self, master, *args, **kwargs)
        #Set basic window properties
        self.title("Extension Naming")
        self.geometry("300x100+{}+{}".format(x, y))
        self.attributes("-topmost", True)
        self.resizable(False, False)
        self.grab_set()
        #Create the widgets
        self.extension_name_label = Label(self, text="Extension Name:", font=medium_font)
        self.extension_name_label.place(relx=0.5, rely=.15, anchor=CENTER)
        self.extension_name_entry = Entry(self, font=normal_font, width=20, justify=CENTER)
        self.extension_name_entry.place(relx=0.5, rely=.5, anchor=CENTER)
        self.confirm_button = Button(self, text="Confirm", font=normal_font, command=self.confirm)
        self.confirm_button.pack(anchor=S, side=LEFT)
        self.cancel_button = Button(self, text="Cancel", font=normal_font, command=self.cancel)
        self.cancel_button.pack(anchor=S, side=RIGHT)
        self.protocol("WM_DELETE_WINDOW", self.cancel)

    def confirm(self):
        #Confirm the extension name
        self.extension_name = self.extension_name_entry.get()
        if ".xt" not in self.extension_name:
            self.extension_name += ".xt"
        self.destroy()

    def cancel(self):
        #Cancel the extension name
        self.extension_name = "NewXT.xt"
        self.destroy()
...
    def add_tab(self, event=None):
        #Adds a new tab
        tab_name = ExtensionNamer(self, self.winfo_x(), self.winfo_y(), color_mode=self.color_mode)
2
  • 2
    The ExtensionNamer class should take a callback parameter. The confirm() method can call this when it confirms the entry. Commented Jun 15, 2022 at 21:59
  • @Barmar What do you mean? How do I do that? Commented Jun 15, 2022 at 22:10

1 Answer 1

1

Add a callback function parameter to the class. When the user confirms the extension, this function will be called with the extension name.

This is similar to the way Tkinter widgets take a command parameter, and you use it the same way. When calling ExtensionNamer(), add a callback= argument that specifies the function that will use the extension name after the user has confirmed it.

class ExtensionNamer(Toplevel):
    def __init__(self, master, x, y, callback, *args, **kwargs):
        #Initialize the window
        super().__init__(self, master, *args, **kwargs)
        self.callback = callback
        #Set basic window properties
        self.title("Extension Naming")
        self.geometry("300x100+{}+{}".format(x, y))
        self.attributes("-topmost", True)
        self.resizable(False, False)
        self.grab_set()
        #Create the widgets
        self.extension_name_label = Label(self, text="Extension Name:", font=medium_font)
        self.extension_name_label.place(relx=0.5, rely=.15, anchor=CENTER)
        self.extension_name_entry = Entry(self, font=normal_font, width=20, justify=CENTER)
        self.extension_name_entry.place(relx=0.5, rely=.5, anchor=CENTER)
        self.confirm_button = Button(self, text="Confirm", font=normal_font, command=self.confirm)
        self.confirm_button.pack(anchor=S, side=LEFT)
        self.cancel_button = Button(self, text="Cancel", font=normal_font, command=self.cancel)
        self.cancel_button.pack(anchor=S, side=RIGHT)
        self.protocol("WM_DELETE_WINDOW", self.cancel)

    def confirm(self):
        #Confirm the extension name
        self.extension_name = self.extension_name_entry.get()
        if ".xt" not in self.extension_name:
            self.extension_name += ".xt"
        self.destroy()
        self.callback(self.extension_name)

You would use it something like this:

    def add_tab(self, event=None):
        #Adds a new tab
        tab_name = ExtensionNamer(self, self.winfo_x(), self.winfo_y(), callback=self.display_name, color_mode=self.color_mode)

    def display_name(self, name):
        print(f'Extension name is {name}')
Sign up to request clarification or add additional context in comments.

9 Comments

When I call the class how do I enable the callback? This currently gives the error TypeError: ExtensionNamer.__init__() missing 1 required positional argument: 'callback'
You pass the function that wants to use the name as another argument. I explained that in the answer, it's similar to the way you use command=confirm when creating the button widget.
ExtensionNamer(master, x, y, callback=function_that_uses_extension_name)
So callback=confirm?
No. confirm() is the function that calls the callback.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.