Python Forum
Returning a value from a tkinter.button call - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: Returning a value from a tkinter.button call (/thread-24463.html)



Returning a value from a tkinter.button call - markr0804 - Feb-15-2020

I am trying to code without using globals. I am using the tkinter.button (see below) to enter some numbers into a variable. Ideally I want the tkinter.button call to return the number entered so that I don't have to make use of the global.

Anyone any ideas ?

    window_button = tkinter.Button(universe_window,text="0",font=(font,font_size_small),command=partial(Get_Number, "Display_Commands","0"))
    window_button.place(x=654,y=310)



RE: Returning a value from a tkinter.button call - Gribouillis - Feb-15-2020

People often use class instances to hold values manipulated by tkinter callbacks. For example

class App:
    def __init__(self):
        ... # some code
        window_button = tkinter.Button(universe_window,text="0",font=(font,font_size_small),command=self.get_number)
        window_button.place(x=654,y=310)

    def get_number(self):
        self.value = Get_Number("Display_Commands", "0")



RE: Returning a value from a tkinter.button call - markr0804 - Feb-15-2020

Thanks for the prompt response. I'm new to Python but slowly getting there I think. I pass parameters to the get_number function, will you amendment still allow this? [ You can see my code says command:partial...]


RE: Returning a value from a tkinter.button call - Gribouillis - Feb-15-2020

You can use partial with instance methods as in partial(self.some_method, param1, param2). The advantage is that the method can store data in the self object.


RE: Returning a value from a tkinter.button call - markr0804 - Feb-16-2020

Again, thank you. How do I assign the returned value back to a variable ? Is it as simple as

variable = self.some_method