Search code examples
python-3.xtkintertrace

Tkinter - Spinbox - How can I get traced values into a variable?


How can I get the traced values from a Tkinter Spinbox in the below modified code (https://stackoverflow.com/a/59326732/7681357) into a variable so I can then use it as a timestamp?

I have added the print to get a visual confirmation that the change is actually being traced but when I added

b = tk.Button(text='get', command=App(root).trace_varhour)

b.pack()

just before the root.mainloop() it returned 13 which is the default value and not the traced.

import tkinter as tk

class App(tk.Frame):

    def __init__(self, parent):
        super().__init__(parent)
        self.hourstr = tk.StringVar(self, '13')
        self.hour = tk.Spinbox(self, from_=13, to=23, wrap=True, textvariable=self.hourstr, width=2, state='readonly')

        self.minstr = tk.StringVar(self, '30')
        self.min = tk.Spinbox(self, from_=0, to=59, wrap=True, textvariable=self.minstr, width=2)

        self.secstr = tk.StringVar(self, '00')
        self.sec = tk.Spinbox(self, from_=0, to=59, wrap=True,textvariable=self.secstr, width=2)

        self.last_valueSec = ''
        self.last_value = ''
        self.last_valuehour = ''

        self.hourstr.trace('w', self.trace_varhour)
        self.minstr.trace('w', self.trace_var)
        self.secstr.trace('w', self.trace_varsec)

        self.hour.grid()
        self.min.grid(row=0, column=1)
        self.sec.grid(row=0, column=2)

    def trace_varhour(self, *args):
        self.last_valuehour = self.hourstr.get()
        print(self.last_valuehour)
        return self.last_valuehour

    def trace_var(self, *args):
        self.last_value = self.minstr.get()
    #     return self.last_value

    def trace_varsec(self, *args):
        self.last_valueSec = self.secstr.get()
    # return self.last_valueSec
root = tk.Tk()
App(root).pack()
b = tk.Button(text='get', command=App(root).trace_varhour)
b.pack()
root.mainloop()

Solution

  • I found the solution to my problem, after a lot of trial and error, for anyone that it might help:

    import tkinter as tk
    
    class App(tk.Frame):
    
        def __init__(self, parent):
            super().__init__(parent)
            self.hourstr = tk.StringVar(self, '13')
            self.hour = tk.Spinbox(self, from_=13, to=23, wrap=True, textvariable=self.hourstr, width=2, state='readonly')
    
            self.minstr = tk.StringVar(self, '30')
            self.min = tk.Spinbox(self, from_=0, to=59, wrap=True, textvariable=self.minstr, width=2)
    
            self.secstr = tk.StringVar(self, '00')
            self.sec = tk.Spinbox(self, from_=0, to=59, wrap=True,textvariable=self.secstr, width=2)
    
            self.last_valueSec = ''
            self.last_value = ''
            self.last_valuehour = ''
    
            self.hourstr.trace('w', self.trace_varhour)
            self.minstr.trace('w', self.trace_var)
            self.secstr.trace('w', self.trace_varsec)
    
            self.hour.grid()
            self.min.grid(row=0, column=1)
            self.sec.grid(row=0, column=2)
    
        def trace_varhour(self, *args):
            self.last_valuehour = self.hourstr.get()
    
        def trace_var(self, *args):
            self.last_value = self.minstr.get()
    
        def trace_varsec(self, *args):
            self.last_valueSec = self.secstr.get()
    
    root = tk.Tk()
    app = App(root)
    app.pack()
    
    def get_time():
        get_time = app.last_valuehour + ":" + app.last_value + ":" + app.last_valueSec
        print(get_time)
    
    
    b = tk.Button(text='get', command=get_time)
    b.pack()
    root.mainloop()