0

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()
Anshika Singh
  • 994
  • 12
  • 20
bloo
  • 306
  • 4
  • 13
  • 2
    Your question is a bit unclear. Your code already gets the value of the traced variables and puts the value in a variable. By the way, `command=App(root)` _immediately_ calls `App(root)` before the button press. See http://stackoverflow.com/q/5767228/7432 – Bryan Oakley Jul 25 '20 at 13:50
  • As you said, I do indeed have a function with a variable that traces the change e.g self.last_valuehour but I do not know how I can make a change in the spinbox, say the hour is now 20 and then get that value in a new variable e.g get_time = self.last_valuehour + " :" + last_value + ":" + last_valueSec so that I can then use that variable as a timestamp for another function. – bloo Jul 25 '20 at 13:59
  • Your problem isn't in getting the value -- you're clearly doing that. The problem is in how you are trying to use the value. I don't see anywhere where you're trying to use that value. – Bryan Oakley Jul 25 '20 at 14:03
  • I tried getting it with the button (was planning to add a label) but as you also said in your first comment, that wouldn't work because the button calls the App before the changes, thus returning the default value and not the traced, so I am stuck at this point and need help getting to the next step. I would use the value of say 14:25:00 to convert it to a datetime object and then filter a pandas dataframe (I haven't gotten around that part because I haven't overcome the obstacle described above). – bloo Jul 25 '20 at 14:07
  • giv a try to `b = tk.Button(text='get', command= lambda: App(root).trace_varhour) b.pack()` ? – Delrius Euphoria Jul 25 '20 at 14:47
  • It doesn't return anything. I can see from the print inside the class that it traces the change but the button doesn't return anything. – bloo Jul 25 '20 at 14:50
  • Bound functions don't return anything anywhere. However, you're setting properties on an object so you can get to them via the object. – Bryan Oakley Jul 25 '20 at 14:55

1 Answers1

0

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()

bloo
  • 306
  • 4
  • 13