Home > other >  How do i return a variable from a function that is called when a button is pressed?
How do i return a variable from a function that is called when a button is pressed?

Time:04-23

I have a simple tkinter app. I want to create a pop up window that allows the user to insert its group. After writing and pressing the enter button the value is validated and if acceptable then I would like to save it. The value of group is taken in the save_and_kil function which is "called" when the button is pressed. I am specifying the command to be called in the button arguments so how can I save the value of groups so that i can access it in other_function?

import tkinter as tk


class SomeClass:
    def __init__(self, root):
        self.root = root
        self.root.title("App")
        self.root.geometry("600x450")
        self.groups = ["1", "2", "3", "4", "5"]
        self.top_window()
        self.group = "nothing"


    def top_window(self):
        a = tk.Toplevel()
        a.geometry('400x150')
        a.title('Credentials Window')

        def save_and_kil():
            group = txt_entry.get()
            if group in self.groups:
                a.destroy()

        txt_label = tk.Label(a, text="please write your group")
        txt_entry = tk.Entry(a)
        enter_but = tk.Button(a,
                              text='Enter',
                              command=save_and_kil)
        txt_label.grid(column=0, row=0)
        txt_entry.grid(column=1, row=0)
        enter_but.grid(column=1, row=1)
        # somehow make redefine the value of self.group

       def other_function(self):
           print(self.group)
           pass


roott = tk.Tk()
app = SomeClass(roott)
roott.mainloop()

CodePudding user response:

save_and_kill closes over self, so you can set self.group inside the function.

    def save_and_kil():
        self.group = txt_entry.get()
        if self.group in self.groups:
            a.destroy()

CodePudding user response:

Edit Adding the example code:

import tkinter as tk


class SomeClass:
    def __init__(self, root):
        self.root = root
        self.root.title("App")
        self.root.geometry("600x450")
        self.groups = ["1", "2", "3", "4", "5"]
        self.top_window()
        self.group = "nothing"

    def save_and_kil(self):
        self.group = self.txt_entry.get()
        
        if self.group in self.groups:
            self.a.destroy()
            
        self.other_function()

    def other_function(self):
       print(f'self.group: {self.group}')
       pass

    def top_window(self):
        self.a = tk.Toplevel()
        self.a.geometry('400x150')
        self.a.title('Credentials Window')

        self.txt_label = tk.Label(self.a, text="please write your group")
        self.txt_entry = tk.Entry(self.a)
        self.enter_but = tk.Button(self.a,
                              text='Enter',
                              command=self.save_and_kil)
        self.txt_label.grid(column=0, row=0)
        self.txt_entry.grid(column=1, row=0)
        self.enter_but.grid(column=1, row=1)
        # somehow make redefine the value of self.group


roott = tk.Tk()
app = SomeClass(roott)
roott.mainloop()

You could set the entry directly to self.group or have a self.temp_group that you would save in save_and_kil and use it later in the other_function

  • Related