• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Getting variable from class that uses input

Resolved: Getting variable from class that uses input

0
By Isaac Tonny on 16/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

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)

Answer:

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

If you have better answer, please add a comment about this, thank you!

python tkinter
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: In a Pinescript v5 Strategy why is my bool not working?

02/04/2023

Resolved: net::ERR_HTTP2_PROTOCOL_ERROR by http get request angular 15.2

02/04/2023

Resolved: How do I stop the command from happening if the requirements for it to work aren’t sert Discord.js

02/04/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.