Python Forum
Returning a value from a tkinter.button call
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Returning a value from a tkinter.button call
#1
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)
Reply
#2
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")
Reply
#3
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...]
Reply
#4
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.
Reply
#5
Again, thank you. How do I assign the returned value back to a variable ? Is it as simple as

variable = self.some_method
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] TKinter Remove Button Frame Nu2Python 8 977 Jan-16-2024, 06:44 PM
Last Post: rob101
  tkinter - touchscreen, push the button like click the mouse John64 5 842 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  Centering and adding a push button to a grid window, TKinter Edward_ 15 4,717 May-25-2023, 07:37 PM
Last Post: deanhystad
  simple tkinter question function call not opening image gr3yali3n 5 3,425 Aug-02-2022, 09:13 PM
Last Post: woooee
  Can't get tkinter button to change color based on changes in data dford 4 3,417 Feb-13-2022, 01:57 PM
Last Post: dford
  Creating a function interrupt button tkinter AnotherSam 2 5,523 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 5,006 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  tkinter showing image in button rwahdan 3 5,617 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  tkinter button image Nick_tkinter 4 4,030 Mar-04-2021, 11:33 PM
Last Post: deanhystad
  tkinter python button position problem Nick_tkinter 3 3,557 Jan-31-2021, 05:15 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020