Tkinter - Spinbox - 如何将跟踪值放入变量中? [英] Tkinter - Spinbox - How can I get traced values into a variable?
问题描述
如何在以下修改后的代码中从 Tkinter Spinbox 获取跟踪值 (https://stackoverflow.com/a/59326732/7681357) 转换成一个变量,以便我可以将其用作时间戳?
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()
就在 root.mainloop()
之前,它返回了 13,这是默认值,而不是被跟踪的值.
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()
推荐答案
经过大量的反复试验,我找到了解决我的问题的方法,对于任何可能有帮助的人:
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()
这篇关于Tkinter - Spinbox - 如何将跟踪值放入变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!