Home > Software design >  Getting variable from class that uses input
Getting variable from class that uses input

Time:06-16

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)

CodePudding user response:

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}')
  • Related